图像增强有直方图均衡和直方图规定化,我们经常使用均衡化处理图像,而规定化似乎很少用到,今天参考直方图匹配这篇文章实现halcon的直方图规定化以供参考。
直方图规定化步骤:
1、计算源图像和需要规定化图像的累计直方图;
2、计算两个累计直方图各阶差的绝对值,得到一个256 x 256的矩阵, 再计算每行的最小值所在的灰度阶作为映射关系(Lut表);
3、使用Lut表进行灰度映射。
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_draw ('margin')
dev_update_off ()
dev_update_window ('off')
* 读入模板图和要规定化的图
read_image (ImageModel, 'C:/Users/86188/Desktop/Model.jpeg')
read_image (ImageToBeConverted, 'C:/Users/86188/Desktop/ToBeConverted.jpeg')
count_channels (ImageModel, ModelChannels)
count_channels (ImageToBeConverted, Channels)
if(ModelChannels == 3)
decompose3 (ImageModel, Image1, Image2, Image3)
concat_obj (Image1, Image2, ModelConcat)
concat_obj (ModelConcat, Image3, ModelConcat)
decompose3 (ImageToBeConverted, Image1, Image2, Image3)
concat_obj (Image1, Image2, ToBeConvertedConcat)
concat_obj (ToBeConvertedConcat, Image3, ToBeConvertedConcat)
else
rgb1_to_gray (ImageModel, ModelConcat)
rgb1_to_gray (ImageToBeConverted, ToBeConvertedConcat)
endif
gen_empty_obj (ImageResult)
count_obj (ModelConcat, Number)
for index := 1 to Number by 1
select_obj (ModelConcat, ImageModelSelect, index)
select_obj (ToBeConvertedConcat, ImageToBeConvertedSelect, index)
* 1、计算源图像和需要规定化图像的累计直方图
gray_histo (ImageModelSelect, ImageModelSelect, AbsoluteHistoModel, RelativeHistoModel)
gray_histo (ImageToBeConvertedSelect, ImageToBeConvertedSelect, AbsoluteHistoToBeConverted, RelativeHistoToBeConverted)
ModelAccuTuple := []
ToBeConvertedAccuTuple := []
pre_val := 0
for i := 0 to 255 by 1
pre_val := pre_val + real(AbsoluteHistoModel[i])
tuple_concat (ModelAccuTuple, pre_val, ModelAccuTuple)
endfor
tuple_div (ModelAccuTuple, pre_val, ModelAccuTuple)
pre_val := 0
for i := 0 to 255 by 1
pre_val := pre_val + real(AbsoluteHistoToBeConverted[i])
tuple_concat (ToBeConvertedAccuTuple, pre_val, ToBeConvertedAccuTuple)
endfor
tuple_div (ToBeConvertedAccuTuple, pre_val, ToBeConvertedAccuTuple)
* 2、计算两个累计直方图各阶差的绝对值,得到一个256 x 256的矩阵, 再计算每阶的最小值所在的灰度阶作为映射关系
table := []
for i := 0 to 255 by 1
oneLeve := []
for j := 0 to 255 by 1
diffAbs := abs(ModelAccuTuple[i] - ToBeConvertedAccuTuple[j])
tuple_concat (oneLeve, diffAbs, oneLeve)
endfor
tuple_sort_index (oneLeve, Indices)
tuple_concat (table, Indices[0], table)
endfor
* 3、使用Lut表进行灰度映射
lut_trans (ImageToBeConvertedSelect, ImageConverted, table)
concat_obj (ImageResult, ImageConverted, ImageResult)
endfor
count_obj (ImageResult, Number1)
if(Number1 == 3)
select_obj (ImageResult, ImageR, 1)
select_obj (ImageResult, ImageG, 2)
select_obj (ImageResult, ImageB, 3)
compose3 (ImageR, ImageG, ImageB, ImageResult)
endif
ImageModel
ImageToBeConverted
ImageResult
直方图效果不好就不放了(▔□▔)/("▔□▔)/("▔□▔)/