画像DCT

 投稿者:しばっち  投稿日:2017年12月31日(日)16時40分31秒
  離散コサイン変換を行います。
DCTを画像サイズで行います。

このプログラムは実行に時間がかかります。
ご注意ください。

OPTION ARITHMETIC NATIVE
FILE GETNAME N$,"BMP,JPG,GIF,PNGファイル|*.BMP;*.JPG;*.GIF;*.PNG"
CALL PICTURELOAD(N$,XSIZE,YSIZE)
OPTION BASE 0
DIM MODE$(3)
DATA "LOWPASS","BANDPASS","HIGHPASS","BANDSTOP"
MAT READ MODE$
LET N1=XSIZE
LET N2=YSIZE
DIM BB(N1,N2),GG(N1,N2),RR(N1,N2)
FOR Y=0 TO YSIZE-1
   FOR X=0 TO XSIZE-1
      CALL GETPOINT(X,Y,RR(X,Y),GG(X,Y),BB(X,Y))
   NEXT X
NEXT Y
CALL DCT2D(N1,N2,BB)
CALL DCT2D(N1,N2,GG)
CALL DCT2D(N1,N2,RR)
LOCATE CHOICE(MODE$) : MD
SELECT CASE MD
CASE 1
   LOCATE VALUE ,RANGE 2 TO N1-1,AT N1/2:FSX1
   LOCATE VALUE ,RANGE 2 TO N2-1,AT N2/2:FSY1
   FOR Y=0 TO N2-1
      FOR X=0 TO N1-1
         IF X>=FSX1 OR Y>=FSY1 THEN
            LET RR(X,Y)=0
            LET GG(X,Y)=0
            LET BB(X,Y)=0
         END IF
      NEXT  X
   NEXT  Y
CASE 2
   LOCATE VALUE ,RANGE 2 TO N1-1,AT N1/2:FSX1
   LOCATE VALUE ,RANGE FSX1 TO N1-1,AT (FSX1+N1)/2:FSX2
   LOCATE VALUE ,RANGE 2 TO N2-1,AT N2/2:FSY1
   LOCATE VALUE ,RANGE FSY1 TO N2-1,AT (FSY1+N2)/2:FSY2
   FOR Y=0 TO N2-1
      FOR X=0 TO N1-1
         IF (X>=FSX1 AND X<=FSX2) OR (Y>=FSY1 AND Y<=FSY2) THEN
         ELSE
            LET RR(X,Y)=0
            LET GG(X,Y)=0
            LET BB(X,Y)=0
         END IF
      NEXT  X
   NEXT  Y
CASE 3
   LOCATE VALUE ,RANGE 2 TO N1-1,AT N1/2:FSX1
   LOCATE VALUE ,RANGE 2 TO N2-1,AT N2/2:FSY1
   FOR Y=0 TO N2-1
      FOR X=0 TO N1-1
         IF X<=FSX1 OR Y<=FSY1 THEN
            LET RR(X,Y)=0
            LET GG(X,Y)=0
            LET BB(X,Y)=0
         END IF
      NEXT  X
   NEXT  Y
CASE 4
   LOCATE VALUE ,RANGE 2 TO N1-1,AT N1/2:FSX1
   LOCATE VALUE ,RANGE FSX1 TO N1-1,AT (FSX1+N1)/2:FSX2
   LOCATE VALUE ,RANGE 2 TO N2-1,AT N2/2:FSY1
   LOCATE VALUE ,RANGE FSY1 TO N2-1,AT (FSY1+N2)/2:FSY2
   FOR Y=0 TO N2-1
      FOR X=0 TO N1-1
         IF (X>=FSX1 AND X<=FSX2) OR (Y>=FSY1 AND Y<=FSY2) THEN
            LET RR(X,Y)=0
            LET GG(X,Y)=0
            LET BB(X,Y)=0
         END IF
      NEXT  X
   NEXT  Y
END SELECT
CALL IDCT2D(N1,N2,BB)
CALL IDCT2D(N1,N2,GG)
CALL IDCT2D(N1,N2,RR)
CLEAR
FOR Y=0 TO YSIZE-1
   FOR X=0 TO XSIZE-1
      CALL PSET(X,Y,RR(X,Y),GG(X,Y),BB(X,Y))
   NEXT X
NEXT Y
END

EXTERNAL FUNCTION C(X,N)
OPTION ARITHMETIC NATIVE
IF X=0 OR X=N THEN LET C=SQR(.5) ELSE LET C=1
END FUNCTION

EXTERNAL SUB DCT2(A(),N,B())
OPTION ARITHMETIC NATIVE
FOR I=0 TO N-1
   LET S=0
   FOR K=0 TO N-1
      LET S=S+A(K)*COS((2*K+1)*I*PI/2/N)
   NEXT K
   LET B(I)=S*SQR(2/N)*C(I,N)
NEXT I
END SUB

EXTERNAL SUB DCT3(A(),N,B())
OPTION ARITHMETIC NATIVE
FOR I=0 TO N-1
   LET S=0
   FOR K=0 TO N-1
      LET S=S+C(K,N)*A(K)*COS((2*I+1)*K*PI/2/N)
   NEXT K
   LET B(I)=INT(S*SQR(2/N)+.5)
NEXT I
END SUB

EXTERNAL SUB DCT2D(M,N,RR(,))
OPTION ARITHMETIC NATIVE
OPTION BASE 0
LET NN=MAX(M,N)
DIM XR(NN),YR(NN)
FOR Y=0 TO N-1
   FOR X=0 TO M-1
      LET XR(X)=RR(X,Y)
   NEXT X
   CALL DCT2(XR,M,YR)
   FOR X=0 TO M-1
      LET RR(X,Y)=YR(X)
   NEXT X
NEXT Y
FOR X=0 TO M-1
   FOR Y=0 TO N-1
      LET XR(Y)=RR(X,Y)
   NEXT Y
   CALL DCT2(XR,N,YR)
   FOR Y=0 TO N-1
      LET RR(X,Y)=YR(Y)
   NEXT Y
NEXT X
END SUB

EXTERNAL SUB IDCT2D(M,N,RR(,))
OPTION ARITHMETIC NATIVE
OPTION BASE 0
LET NN=MAX(M,N)
DIM XR(NN),YR(NN)
FOR Y=0 TO N-1
   FOR X=0 TO M-1
      LET XR(X)=RR(X,Y)
   NEXT X
   CALL DCT3(XR,M,YR)
   FOR X=0 TO M-1
      LET RR(X,Y)=YR(X)
   NEXT X
NEXT Y
FOR X=0 TO M-1
   FOR Y=0 TO N-1
      LET XR(Y)=RR(X,Y)
   NEXT Y
   CALL DCT3(XR,N,YR)
   FOR Y=0 TO N-1
      LET RR(X,Y)=YR(Y)
   NEXT Y
NEXT X
END SUB

EXTERNAL SUB RGB(X,R,G,B)
OPTION ARITHMETIC NATIVE
LET B=MOD(INT(X/65536),256)
LET G=MOD(INT(X/256),256)
LET R=MOD(X,256)
END SUB

EXTERNAL SUB GETPOINT(X,Y,R,G,B)
OPTION ARITHMETIC NATIVE
ASK PIXEL VALUE(X,Y) C
CALL RGB(C,R,G,B)
END SUB

EXTERNAL SUB GINIT(XSIZE,YSIZE)
OPTION ARITHMETIC NATIVE
SET BITMAP SIZE XSIZE,YSIZE
SET COLOR MODE "NATIVE"
CLEAR
SET POINT STYLE 1
SET WINDOW 0,XSIZE-1,YSIZE-1,0
END SUB

EXTERNAL SUB PSET(X,Y,R,G,B)
OPTION ARITHMETIC NATIVE
LET RR=MIN(255,MAX(0,INT(R)))
LET GG=MIN(255,MAX(0,INT(G)))
LET BB=MIN(255,MAX(0,INT(B)))
SET COLOR COLORINDEX(RR/255,GG/255,BB/255)
PLOT POINTS:X,Y
END SUB

EXTERNAL SUB PICTURELOAD(N$,XSIZE,YSIZE)
OPTION ARITHMETIC NATIVE
SET COLOR MODE "NATIVE"
CLEAR
SET POINT STYLE 1
IF N$="" THEN STOP
GLOAD N$
LET XSIZE=PIXELX(1)+1
LET YSIZE=PIXELY(1)+1
SET BITMAP SIZE XSIZE,YSIZE
SET WINDOW 0,XSIZE-1,YSIZE-1,0
END SUB

 

画像DCT

 投稿者:しばっち  投稿日:2017年12月31日(日)16時44分23秒
 
離散コサイン変換を行います。
DCTを8×8~32×32程度の小プロックで計算します。

フィルタ係数を入力し、それを掛けてフィルタ処理します。
前半が低域部分で後半が高域部分になります。
係数は「0」(遮断)か「1」(通過)です。
(※「0,1」以外を入力するとイコライザー的になります)

8×8ではフィルタ係数は8個で
1,1,1,1,0,0,0,0なら低域通過、高域遮断でLow Pass Filter
0,0,0,0,1,1,1,1なら低域遮断、高域通過でHigh Pass Filter
1,0,1,0,1,0,1,0ならコムフィルタ(くし形)
になります。

このプログラムは実行に時間がかかります。
ご注意ください。

OPTION ARITHMETIC NATIVE
OPTION BASE 0
DECLARE EXTERNAL FUNCTION C
DIM COL$(1)
MAT READ COL$
DATA "RGB","GRAY"
LOCATE CHOICE(COL$) :COLORMODE
INPUT  PROMPT "SIZE (3 - 5) =":SIZE
LET N=2^SIZE
DIM B(N,N),G(N,N),R(N,N),M(N)
FILE GETNAME N$,"BMP,JPG,GIF,PNGファイル|*.BMP;*.JPG;*.GIF;*.PNG"
CALL PICTURELOAD(N$,XSIZE,YSIZE)
DIM VM(XSIZE,YSIZE)
ASK PIXEL ARRAY(0,0) VM
FOR I=0 TO N-1
   IF I<N/2 THEN LET MES$="低域 " ELSE LET MES$="高域 "
   INPUT PROMPT "フィルタ係数 "& MES$ & STR$(I)&" (遮断(0) or 通過(1)) =":M(I)
NEXT I
FOR Y=0 TO YSIZE-1 STEP N
   FOR X=0 TO XSIZE-1 STEP N
      FOR J=0 TO N-1
         FOR I=0 TO N-1
            IF X+I<=XSIZE-1 AND Y+J<=YSIZE-1 THEN
               LET CC=VM(X+I,Y+J)
               CALL RGB(CC,RR,GG,BB)
               LET B(I,J)=BB
               LET G(I,J)=GG
               LET R(I,J)=RR
            END IF
            IF MODE=2 THEN LET B(I,J)=(151*B(I,J)+77*G(I,J)+28*R(I,J))/256
         NEXT I
      NEXT J
      IF COLORMODE=1 THEN
         CALL DCT2(B,N)
         CALL DCT2(G,N)
         CALL DCT2(R,N)
         FOR I=0 TO N-1
            FOR J=0 TO N-1
               LET B(J,I)=B(J,I)*M(I)*M(J)
               LET G(J,I)=G(J,I)*M(I)*M(J)
               LET R(J,I)=R(J,I)*M(I)*M(J)
            NEXT J
         NEXT I
         CALL DCT3(B,N)
         CALL DCT3(G,N)
         CALL DCT3(R,N)
         FOR I=0 TO N-1
            FOR J=0 TO N-1
               IF X+J<=XSIZE-1 AND Y+I<=YSIZE-1 THEN
                  CALL PSET(X+J,Y+I,R(J,I),G(J,I),B(J,I))
               END IF
            NEXT J
         NEXT I
      ELSE
         CALL DCT2(B,N)
         FOR I=0 TO N-1
            FOR J=0 TO N-1
               LET B(J,I)=B(J,I)*M(I)*M(J)
            NEXT J
         NEXT I
         CALL DCT3(B,N)
         FOR I=0 TO N-1
            FOR J=0 TO N-1
               CALL PSET(X+J,Y+I,B(J,I),B(J,I),B(J,I))
            NEXT J
         NEXT I
      END IF
   NEXT X
NEXT Y
END

EXTERNAL SUB DCT1(NN,X(,)) !'変換/逆変換
OPTION ARITHMETIC NATIVE
OPTION BASE 0
DIM Y(NN,NN)
FOR V=0 TO NN
   FOR U=0 TO NN
      LET YY=2/NN*C(U,NN)*C(V,NN)
      FOR M=0 TO NN
         FOR N=0 TO NN
            LET Y(U,V)=Y(U,V)+YY*C(M,NN)*C(N,NN)*X(M,N)*COS(U*M*PI/NN)*COS(V*N*PI/NN)
         NEXT  N
      NEXT M
   NEXT U
NEXT V
MAT X=Y
END SUB

EXTERNAL SUB DCT2(NN,X(,)) !'変換
OPTION ARITHMETIC NATIVE
OPTION BASE 0
DIM Y(NN-1,NN-1)
FOR V=0 TO NN-1
   FOR U=0 TO NN-1
      LET YY=2/NN*C(U,NN)*C(V,NN)
      FOR M=0 TO NN-1
         FOR N=0 TO NN-1
            LET Y(U,V)=Y(U,V)+YY*X(M,N)*COS((2*M+1)*U*PI/2/NN)*COS((2*N+1)*V*PI/2/NN)
         NEXT N
      NEXT M
   NEXT U
NEXT V
MAT X=Y
END SUB

EXTERNAL SUB DCT3(NN,X(,)) !'逆変換
OPTION ARITHMETIC NATIVE
OPTION BASE 0
DIM Y(NN-1,NN-1)
FOR M=0 TO NN-1
   FOR N=0 TO NN-1
      LET YY=2/NN
      FOR V=0 TO NN-1
         FOR U=0 TO NN-1
            LET Y(M,N)=Y(M,N)+YY*C(U,NN)*C(V,NN)*X(U,V)*COS((2*M+1)*U*PI/2/NN)*COS((2*N+1)*V*PI/2/NN)
         NEXT U
      NEXT V
   NEXT N
NEXT M
MAT X=Y
END SUB

EXTERNAL SUB DCT4(NN,X(,)) !'変換/逆変換
OPTION ARITHMETIC NATIVE
OPTION BASE 0
DIM Y(NN,NN)
FOR V=0 TO NN-1
   FOR U=0 TO NN-1
      FOR M=0 TO NN-1
         FOR N=0 TO NN-1
            LET Y(U,V)=Y(U,V)+X(M,N)*COS((2*M+1)*(2*U+1)*PI/4/NN)*COS((2*N+1)*(2*V+1)*PI/4/NN)
         NEXT N
      NEXT M
      LET Y(U,V)=Y(U,V)*2/NN
   NEXT U
NEXT V
MAT X=Y
END SUB

EXTERNAL FUNCTION C(X,N)
OPTION ARITHMETIC NATIVE
IF X=0 OR X=N THEN LET C=SQR(2)/2 ELSE LET C=1
END FUNCTION

EXTERNAL SUB RGB(X,R,G,B)
OPTION ARITHMETIC NATIVE
LET B=MOD(INT(X/65536),256)
LET G=MOD(INT(X/256),256)
LET R=MOD(X,256)
END SUB

EXTERNAL SUB GINIT(XSIZE,YSIZE)
OPTION ARITHMETIC NATIVE
SET BITMAP SIZE XSIZE,YSIZE
CLEAR
SET COLOR MODE "NATIVE"
SET POINT STYLE 1
SET WINDOW 0,XSIZE-1,YSIZE-1,0
END SUB

EXTERNAL SUB PSET(X,Y,R,G,B)
OPTION ARITHMETIC NATIVE
LET RR=MIN(255,MAX(0,INT(R)))
LET GG=MIN(255,MAX(0,INT(G)))
LET BB=MIN(255,MAX(0,INT(B)))
SET COLOR COLORINDEX(RR/255,GG/255,BB/255)
PLOT POINTS:X,Y
END SUB

EXTERNAL SUB PICTURELOAD(N$,XSIZE,YSIZE)
OPTION ARITHMETIC NATIVE
CLEAR
SET COLOR MODE "NATIVE"
SET POINT STYLE 1
IF N$="" THEN STOP
GLOAD N$
LET XSIZE=PIXELX(1)
LET YSIZE=PIXELY(1)
SET BITMAP SIZE XSIZE,YSIZE
SET WINDOW 0,XSIZE-1,YSIZE-1,0
END SUB

 

戻る