|
ヒストグラムを平坦化しコンストラストを改善します。
モノクロ画像では濃度値に対して平坦化しますが
カラー画像では、R,G,B各々に対してではなくHSVに変換して
彩度(S)又は明度(V)に対して平坦化します。
https://qiita.com/Dason08/items/1b28e24d12630182fd69
https://algorithm.joho.info/image-processing/histogram-equalization/
PUBLIC NUMERIC XSIZE,YSIZE
OPTION BASE 0
DIM HIST(255)
FILE GETNAME N$,"BMP,JPG,GIF,PNGファイル|*.BMP;*.JPG;*.GIF;*.PNG"
CALL PICTURELOAD(N$,XSIZE,YSIZE)
DIM M(XSIZE-1,YSIZE-1)
ASK PIXEL ARRAY(0,0) M
DIM OUT(XSIZE,YSIZE),IN(XSIZE,YSIZE)
LET MODE=0
FOR Y=0 TO YSIZE-1
FOR X=0 TO XSIZE-1
CALL RGB(M(X,Y),R,G,B)
CALL RGB2HSV(R,G,B,H,S,V)
SELECT CASE MODE
CASE 0
LET IN(X,Y)=S
LET HIST(S)=HIST(S)+1
CASE 1
LET IN(X,Y)=V
LET HIST(V)=HIST(V)+1
END SELECT
NEXT X
NEXT Y
CALL PLANE(OUT,IN,HIST,XSIZE,YSIZE)
FOR Y=0 TO YSIZE-1
FOR X=0 TO XSIZE-1
CALL RGB(M(X,Y),R,G,B)
CALL RGB2HSV(R,G,B,H,S,V)
SELECT CASE MODE
CASE 0
CALL HSV2RGB(R,G,B,H,OUT(X,Y),V)
CASE 1
CALL HSV2RGB(R,G,B,H,S,OUT(X,Y))
END SELECT
LET M(X,Y)=SETRGB(R,G,B)
NEXT X
NEXT Y
MAT PLOT CELLS , IN 0,0; XSIZE-1,YSIZE-1:M
END
EXTERNAL SUB PLANE(OUT(,),IN(,),HIST(),XSIZE,YSIZE)
FOR Y=0 TO YSIZE-1
FOR X=0 TO XSIZE-1
FOR I=0 TO IN(X,Y)
LET OUT(X,Y)=OUT(X,Y)+HIST(I)
NEXT I
LET OUT(X,Y)=INT(OUT(X,Y)*255/XSIZE/YSIZE)
NEXT X
NEXT Y
END SUB
EXTERNAL FUNCTION SETRGB(R,G,B)
LET R=MIN(255,MAX(R,0))
LET G=MAX(0,MIN(G,255))
LET B=MAX(0,MIN(B,255))
LET SETRGB=B*65536+G*256+R
END FUNCTION
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 PICTURELOAD(N$,XSIZE,YSIZE)
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
EXTERNAL SUB HSV2RGB(R,G,B,H,S,V)
IF S=0 THEN
LET R=V
LET G=V
LET B=V
EXIT SUB
END IF
LET T=V-S*V/255
LET HH=H
IF H>=300 OR H<60 THEN
IF H>=300 THEN LET HH=360-HH
IF H<60 THEN LET HH=-HH
LET HH=HH/60
LET RR=0
IF HH<0 THEN
LET BB=1
LET GG=HH+BB
ELSE
LET GG=1
LET BB=GG-HH
END IF
ELSEIF H>=60 AND H<180 THEN
LET HH=HH-120
LET HH=HH/60
LET GG=0
IF HH<0 THEN
LET BB=1
LET RR=HH+BB
ELSE
LET RR=1
LET BB=RR-HH
END IF
ELSEIF H>=180 AND H<300 THEN
LET HH=HH-240
LET HH=HH/60
LET BB=0
IF HH<0 THEN
LET RR=1
LET GG=HH+RR
ELSE
LET GG=1
LET RR=GG-HH
END IF
END IF
LET R=-RR*(V-T)+V
LET R=INT(R)
LET G=-GG*(V-T)+V
LET G=INT(G)
LET B=-BB*(V-T)+V
LET B=INT(B)
END SUB
EXTERNAL SUB RGB2HSV(R,G,B,H,S,V)
LET V=MAX(MAX(R,G),B)
LET T=MIN(MIN(R,G),B)
IF V=0 THEN
LET S=0
ELSE
LET S=((V-T)*255)/V
END IF
IF S=0 THEN
LET H=0
ELSE
LET RR=(V-R)/(V-T)
LET GG=(V-G)/(V-T)
LET BB=(V-B)/(V-T)
IF V=R THEN
LET H=BB-GG
ELSEIF V=G THEN
LET H=2+RR-BB
ELSEIF V=B THEN
LET H=4+GG-RR
END IF
LET H=H*60
END IF
IF H<0 THEN LET H=H+360
END SUB
|
|