|
!'離散WAVELET変換(多重解像度解析)
OPTION BASE 0
PUBLIC NUMERIC XSIZE,YSIZE,DAUBECHIES,P(7),Q(7)
FILE GETNAME F$, "BMP,JPG,GIFファイル|*.BMP;*.JPG;*.GIF"
IF F$="" THEN STOP
CALL PICTURELOAD(F$,XSIZE,YSIZE)
LET LEV=2 !'等分割 (4^LEV 分割)
!'16分割時の帯域分割(L:低周波 H:高周波)
!'LLLL LLHL HLLL HLHL
!'LLLH LLHH HLLH HLHH
!'LHLL LHHL HHLL HHHL
!'LHLH LHHH HHLH HHHH
LET DAUBECHIES=2
LET SIZEX=INT((XSIZE+2^LEV-1)/2^LEV)*2^LEV !'補正
LET SIZEY=INT((YSIZE+2^LEV-1)/2^LEV)*2^LEV
DIM R1(SIZEX,SIZEY),G1(SIZEX,SIZEY),B1(SIZEX,SIZEY) !'大量のメモリーが必要
DIM R2(SIZEX,SIZEY),G2(SIZEX,SIZEY),B2(SIZEX,SIZEY) !'再帰呼び出しも行っている
SELECT CASE DAUBECHIES
!'ドペシィーの数列
CASE 1
LET P(0) = SQR(2)/2
LET P(1) = SQR(2)/2
CASE 2
LET P(0) = 0.482962913144534143374871599864
LET P(1) = 0.836516303737807905575293780917
LET P(2) = 0.22414386804201338102597276224
LET P(3) = -0.129409522551260381174449418812
CASE 3
LET P(0) = 0.332670552950082615997608286301
LET P(1) = 0.806891509311092576493086842682
LET P(2) = 0.459877502118491570095951630688
LET P(3) = -0.135011020010254588694583301022
LET P(4) = -0.854412738820266616927155548839E-1
LET P(5) = 0.352262918857095366023408204452E-1
END SELECT
FOR I=0 TO 2*DAUBECHIES-1
LET Q(I)=(-1)^I*P(2*DAUBECHIES-I-1)
NEXT I
PRINT "画像データ読み込み"
FOR X=0 TO XSIZE-1
FOR Y=0 TO YSIZE-1
CALL GETPOINT(X,Y,R1(X,Y),G1(X,Y),B1(X,Y))
NEXT Y
NEXT X
PRINT "ウェーブレット変換"
CALL WAVELET(LEV,0,0,SIZEX,SIZEY,R1,G1,B1,R2,G2,B2)
PRINT "マウスでクリアしたい帯域を左クリック"
PRINT "右クリックで再構成します"
DO
DO
MOUSE POLL XX,YY,L,R
LOOP UNTIL L<>0 OR R<>0
IF L<>0 THEN CALL CLEARAREA(LEV,XX,YY,SIZEX,SIZEY,R1,G1,B1) !'クリックされた帯域内をクリア
LOOP UNTIL R<>0
!'再構成(逆ウェーブレット変換)
LET SIZEX=INT((XSIZE+2^LEV-1)/2^LEV)*2^LEV !'補正
LET SIZEY=INT((YSIZE+2^LEV-1)/2^LEV)*2^LEV
MAT R2=ZER !'データクリア
MAT G2=ZER
MAT B2=ZER
PRINT "再構成"
CALL INVWAVELET(LEV,0,0,SIZEX,SIZEY,R1,G1,B1,R2,G2,B2) !'逆ウェーブレット変換
END
EXTERNAL SUB WAVELET(N,XS,YS,SIZEX,SIZEY,R1(,),G1(,),B1(,),R2(,),G2(,),B2(,))
IF N>0 THEN
FOR Y=0 TO SIZEY-1 !'X方向に帯域分割
FOR X=0 TO SIZEX-1 STEP 2
LET R2(XS+X/2,Y+YS)=0
LET G2(XS+X/2,Y+YS)=0
LET B2(XS+X/2,Y+YS)=0
LET R2(XS+X/2+SIZEX/2,Y+YS)=0
LET G2(XS+X/2+SIZEX/2,Y+YS)=0
LET B2(XS+X/2+SIZEX/2,Y+YS)=0
FOR NN=0 TO DAUBECHIES-1
LET R2(XS+X/2,Y+YS)=R2(XS+X/2,Y+YS)+P(2*NN)*R1(X+XS,Y+YS)+P(2*NN+1)*R1(X+1+XS,Y+YS) !'低周波
LET G2(XS+X/2,Y+YS)=G2(XS+X/2,Y+YS)+P(2*NN)*G1(X+XS,Y+YS)+P(2*NN+1)*G1(X+1+XS,Y+YS)
LET B2(XS+X/2,Y+YS)=B2(XS+X/2,Y+YS)+P(2*NN)*B1(X+XS,Y+YS)+P(2*NN+1)*B1(X+1+XS,Y+YS)
LET R2(XS+X/2+SIZEX/2,Y+YS)=R2(XS+X/2+SIZEX/2,Y+YS)+Q(2*NN)*R1(X+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,YY)=0 !'データクリア
LET G(XX,YY)=0
LET B(XX,YY)=0
IF XX<=XSIZE-1 AND YY<=YSIZE-1 THEN CALL PSET(XX,YY,0,0,0) !'画像もクリア
NEXT YY
NEXT XX
END SUB
EXTERNAL SUB RGB(X,R,G,B)
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)
ASK PIXEL VALUE (X,Y) C
CALL RGB(C,R,G,B)
END SUB
EXTERNAL SUB GINIT(XSIZE,YSIZE)
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)
SET COLOR COLORINDEX(MOD(INT(R)+256,256)/255,MOD(INT(G)+256,256)/255,MOD(INT(B)+256,256)/255)
PLOT POINTS: X , Y
END SUB
EXTERNAL SUB PICTURELOAD(N$,XSIZE,YSIZE)
SET COLOR MODE "NATIVE"
CLEAR
SET POINT STYLE 1
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
|
|