麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

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

效果图

相关阅读

麻辣GIS-Sailor

作者:

GIS爱好者,学GIS,更爱玩GIS。

声明

1.本文所分享的所有需要用户下载使用的内容(包括但不限于软件、数据、图片)来自于网络或者麻辣GIS粉丝自行分享,版权归该下载资源的合法拥有者所有,如有侵权请第一时间联系本站删除。

2.下载内容仅限个人学习使用,请切勿用作商用等其他用途,否则后果自负。

手机阅读
公众号关注
知识星球
手机阅读
麻辣GIS微信公众号关注
最新GIS干货
关注麻辣GIS知识星球
私享圈子

已有 5 条评论

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

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

      1. 麻辣GIS-Sailor
        Sailor  · 2016-06-05 20:22

        什么版本的IDL?

          1. 麻辣GIS-lhy
            lhy  · 2016-06-14 14:42

            8.3的

  2. 麻辣GIS-逆光
    2#
    逆光  · 2019-06-04 22:06

    在col、row、hsi处出现红线,控制台处出现:
    temp=REFORM(rgbimg[0,,])
    ^
    % Syntax error.
    At: E:\text\NewProject\text9\priject9.pro, Line 10

    col=N_elements(temp[,0])
    ^
    % Syntax error.
    At: E:\text\NewProject\text9\priject9.pro, Line 11

    row=N_elements(temp[0,])
    ^
    % Syntax error.
    At: E:\text\NewProject\text9\priject9.pro, Line 12
    % Compiled module: PRIJECT9.
    % Attempt to call undefined procedure: 'PRIJECT9'.
    % Execution halted at: $MAIN$


    请问如何解决????

      1. 麻辣GIS-Sailor
        Sailor  · 2019-06-10 07:03

        这是IDL基本调试问题,请根据自己情况来解决,你贴这个没有上下文我也解决不了。

留言板(小编看到第一时间回复)