1. 程式人生 > >halcon 16bit與8bit影象資料相互轉換

halcon 16bit與8bit影象資料相互轉換

dev_open_file_dialog ('read_image', 'default', 'default', Selection)
read_image (Image, Selection)

** 1. 尋找彩色影象中亮度最大的通道
decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
min_max_gray (ImageRed, ImageRed, 0.09, Min1, RMax1, Range1)
min_max_gray (ImageGreen, ImageGreen, 0.09, Min1, GMax1, Range1)
min_max_gray (ImageBlue, ImageBlue, 0.09, Min1, BMax1, Range1)
concat_obj (ImageRed, ImageGreen, ObjectsConcat)
concat_obj (ObjectsConcat, ImageBlue, ObjectsConcat)
* maxData:=[RMax1,GMax1,BMax1]
maxData:=[0,1,1]
tuple_max (maxData, Max1)
tuple_find (maxData, Max1, Indices)
if(|Indices|#1)
    Indices:=Indices[|Indices|-1]
endif
select_obj (ObjectsConcat, ObjectSelected, Indices+1)
stop()

get_image_size (Image, Width1, Height1) 
**2. 8bit單色圖轉換為16bit
get_region_points (Image, Row, Column)
get_grayval (Image, Row, Column, Grayval)
gen_image_const (Image1, 'uint2', Width1, Height1)
set_grayval (Image1, Row, Column, Grayval*256+Grayval/255)
convert_image_type (Image1, Image11, 'uint2')
min_max_gray (Image11, Image11, 0, Min, Max, Range)

* write_image (Image11, 'tiff', 0, '1')


**3. 16bit單色圖轉換為8bit
scale_image (Image, ImageScaled, 1.0/255, 0)
convert_image_type (Image, ImageConverted, 'byte')
* min_max_gray (ImageConverted, ImageConverted, 0, Min2, Max2, Range2)
write_image (ImageConverted, 'tiff', 0, '1')


**4. 8bit彩色圖轉換為16bit彩色圖
get_image_pointer3 (Image, PointerRed, PointerGreen, PointerBlue, Type1, Width3, Height3)
***彩色R畫面
gen_image1 (ImageR, Type1, Width1, Height1, PointerRed)
get_region_points (ImageR, Rows, Columns)
get_grayval (ImageR, Rows, Columns, GrayvalR)
gen_image_const (ImageR, 'uint2', Width1, Height1)
set_grayval (ImageR, Rows, Columns, GrayvalR*256+GrayvalR/255)
convert_image_type (ImageR, ImageRc, 'uint2')
get_image_pointer1 (ImageRc, PointerRc, Type, Width, Height)
***彩色G畫面
gen_image1 (ImageG, Type1, Width1, Height1, PointerGreen)
get_region_points (ImageG, Rows, Columns)
get_grayval (ImageG, Rows, Columns, GrayvalG)
gen_image_const (ImageG, 'uint2', Width1, Height1)
set_grayval (ImageG, Rows, Columns, GrayvalG*256+GrayvalG/255)
convert_image_type (ImageG, ImageGc, 'uint2')
get_image_pointer1 (ImageGc, PointerGc, Type, Width, Height)
***彩色B畫面
gen_image1 (ImageB, Type1, Width1, Height1, PointerBlue)
get_region_points (ImageB, Rows, Columns)
get_grayval (ImageB, Rows, Columns, GrayvalB)
gen_image_const (ImageB, 'uint2', Width1, Height1)
set_grayval (ImageB, Rows, Columns, GrayvalB*256+GrayvalB/255)
convert_image_type (ImageB, ImageBc, 'uint2')
get_image_pointer1 (ImageBc, PointerBc, Type, Width, Height)
gen_image3 (ImageRGB, Type, Width, Height, PointerRc, PointerGc, PointerBc)
* write_image (ImageRGB, 'tiff', 0, '16color')

       上述分為4個功能部分可通過載入對應影象資料進行驗證,例如可檢視8bit影象轉換為16bit時對應灰階值,可知功能有效,同理8bit可以轉換為10bit,12bit的影象資料。

影象資料轉換關係概述:

  1. 相機8bit影象資料      灰度值Gray範圍0-255    byte(halcon中影象格式)
  2. 相機12bit 影象資料需要轉換成16bit資料流進行輸出   一般高4位為0    灰度值Gray範圍0-2.^12-1  轉換為8bit影象資料進行處理,Gray/2.^4                                       int4(halcon中影象格式)
  3. 相機16bit影象資料    灰度值Gray範圍0-2.^16-1  轉換為8bit影象資料進行處理,Gray/(2.^8-1)     uint2(halcon中影象格式)