IDL实现RGB和HSI的相互转换

RGB to HSI 转换公式

HSI to RGB 转换公式

利用冈大爷(冈萨雷斯)那本书上的公式没有转换成功,在网上重新找了一个转换公式。

实现代码

;Name: RGB与HSI的转换
;Time: 2013-12-19
;Author: 尹全超

PRO item_c_2

;读取图像
file = DIALOG_PICKFILE(/READ, FILTER = '*.bmp')
rgbimg = READ_BMP(file,/RGB)
img01 = image(rgbimg,LAYOUT=[3,2,1],title='RGB')

;RGB->HSI
temp=REFORM(rgbimg[0,,])
col=N_elements(temp[,0])
row=N_elements(temp[0,
])
hsi=dblarr(3,col,row)
temarr=dblarr(3)
FOR i=0,col-1 DO BEGIN
FOR j=0,row-1 DO BEGIN
r1=rgbimg[0,i,j]
g1=rgbimg[1,i,j]
b1=rgbimg[2,i,j]
;normalizing
r=double(double(r1)/(fix(r1)+fix(g1)+fix(b1)))
g=double(double(g1)/(fix(r1)+fix(g1)+fix(b1)))
b=double(double(b1)/(fix(r1)+fix(g1)+fix(b1)))

  temarr[0]=r
  temarr[1]=g
  temarr[2]=b
  temst=acos(((r*2-g-b)/(0.001+2*sqrt((r-g)^2+(r-b)*(g-b)))))
  
  IF(b le g) THEN BEGIN
    hsi[0,i,j]=double(temst*180/!pi)
  ENDIF ELSE BEGIN
    hsi[0,i,j]=double((2*!pi-temst)*180/!pi)
  ENDELSE
  
  hsi[1,i,j]=double((1-3*min(temarr))*100)
  hsi[2,i,j]=double((fix(r1)+fix(g1)+fix(b1))/(3.0*255)*255)
ENDFOR

ENDFOR
img02 = image(hsi,LAYOUT=[3,2,2],/CURRENT,title='RGB to HSI')

;HSI->RGB
rgb=bytarr(3,col,row)
FOR i=0,col-1 DO BEGIN
FOR j=0,row-1 DO BEGIN
h1=hsi[0,i,j]
s1=hsi[1,i,j]
i1=hsi[2,i,j]

  th=h1*!pi/180.0
  ts=s1/100.0
  ti=i1/255.0
 
  IF(th lt 2*!pi/3) THEN BEGIN
    x=ti*(1-ts)
    y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
    z=3*ti-(x+y)
    rgb[0,i,j]=byte(y*255)
    rgb[1,i,j]=byte(z*255)
    rgb[2,i,j]=byte(x*255)
  ENDIF ELSE BEGIN
    IF(th lt 4*!pi/3) THEN BEGIN
      th=th-2*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb[0,i,j]=byte(x*255)
      rgb[1,i,j]=byte(y*255)
      rgb[2,i,j]=byte(z*255)
    ENDIF ELSE BEGIN
      th=th-4*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb[0,i,j]=byte(z*255)
      rgb[1,i,j]=byte(x*255)
      rgb[2,i,j]=byte(y*255)
    ENDELSE
  ENDELSE

ENDFOR

ENDFOR
img03 = image(rgb,LAYOUT=[3,2,3],/CURRENT,title='HSI to RGB')

;色调-1
rgb1=bytarr(3,col,row)
FOR i=0,col-1 DO BEGIN
FOR j=0,row-1 DO BEGIN
h1=hsi[0,i,j]
s1=hsi[1,i,j]
i1=hsi[2,i,j]

  th=h1*!pi/180.0-1*!pi/180.0
  ts=s1/100.0
  ti=i1/255.0
 
  IF(th lt 2*!pi/3) THEN BEGIN
    x=ti*(1-ts)
    y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
    z=3*ti-(x+y)
    rgb1[0,i,j]=byte(y*255)
    rgb1[1,i,j]=byte(z*255)
    rgb1[2,i,j]=byte(x*255)
  ENDIF ELSE BEGIN
    IF(th lt 4*!pi/3) THEN BEGIN
      th=th-2*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb1[0,i,j]=byte(x*255)
      rgb1[1,i,j]=byte(y*255)
      rgb1[2,i,j]=byte(z*255)
    ENDIF ELSE BEGIN
      th=th-4*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb1[0,i,j]=byte(z*255)
      rgb1[1,i,j]=byte(x*255)
      rgb1[2,i,j]=byte(y*255)
    ENDELSE
  ENDELSE

ENDFOR

ENDFOR
img04 = image(rgb1,LAYOUT=[3,2,4],/CURRENT,title='H-1')

;色调-1
rgb1=bytarr(3,col,row)
FOR i=0,col-1 DO BEGIN
FOR j=0,row-1 DO BEGIN
h1=hsi[0,i,j]
s1=hsi[1,i,j]
i1=hsi[2,i,j]

  th=h1*!pi/180.0+10*!pi/180.0
  ind0=where(th gt 360)
  IF(ind0[0] ne -1) THEN BEGIN
    th[ind0]=360
  ENDIF
  ts=s1/100.0
  ti=i1/255.0
 
  IF(th lt 2*!pi/3) THEN BEGIN
    x=ti*(1-ts)
    y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
    z=3*ti-(x+y)
    rgb1[0,i,j]=byte(y*255)
    rgb1[1,i,j]=byte(z*255)
    rgb1[2,i,j]=byte(x*255)
  ENDIF ELSE BEGIN
    IF(th lt 4*!pi/3) THEN BEGIN
      th=th-2*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb1[0,i,j]=byte(x*255)
      rgb1[1,i,j]=byte(y*255)
      rgb1[2,i,j]=byte(z*255)
    ENDIF ELSE BEGIN
      th=th-4*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb1[0,i,j]=byte(z*255)
      rgb1[1,i,j]=byte(x*255)
      rgb1[2,i,j]=byte(y*255)
    ENDELSE
  ENDELSE

ENDFOR

ENDFOR
img041 = image(rgb1,LAYOUT=[3,2,5],/CURRENT,title='H+10')

;饱和度*0.6
rgb2=bytarr(3,col,row)
FOR i=0,col-1 DO BEGIN
FOR j=0,row-1 DO BEGIN
h1=hsi[0,i,j]
s1=hsi[1,i,j]
i1=hsi[2,i,j]

  th=h1*!pi/180.0
  ts=s1/100.0*0.6
  ti=i1/255.0
 
  IF(th lt 2*!pi/3) THEN BEGIN
    x=ti*(1-ts)
    y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
    z=3*ti-(x+y)
    rgb2[0,i,j]=byte(y*255)
    rgb2[1,i,j]=byte(z*255)
    rgb2[2,i,j]=byte(x*255)
  ENDIF ELSE BEGIN
    IF(th lt 4*!pi/3) THEN BEGIN
      th=th-2*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb2[0,i,j]=byte(x*255)
      rgb2[1,i,j]=byte(y*255)
      rgb2[2,i,j]=byte(z*255)
    ENDIF ELSE BEGIN
      th=th-4*!pi/3
      x=ti*(1-ts)
      y=ti*(1+(ts*cos(th)/cos(!pi/3-th)))
      z=3*ti-(x+y)
      rgb2[0,i,j]=byte(z*255)
      rgb2[1,i,j]=byte(x*255)
      rgb2[2,i,j]=byte(y*255)
    ENDELSE
  ENDELSE

ENDFOR

ENDFOR
img05 = image(rgb2,LAYOUT=[3,2,6],/CURRENT,title='S*0.6')

END

效果图

IDL实现3*3均值滤波和中值滤波 IDL使用N_elements获取图像的行数和列数

作者:,GIS爱好者。
分享本文,请您带上本文链接
分享到:

已有 3 条评论

  1. lhy
    1#
    lhy  · 2016-06-04 20:32

    请问我老提示idl itools create visualization,怎么回事啊,出不来这个结果啊

      1. Sailor
        Sailor  · 2016-06-05 20:22

        什么版本的IDL?

          1. lhy
            lhy  · 2016-06-14 14:42

            8.3的

发表评论