halcon处理灰度能量图
使用halcon处理射线图像,对高能区域和低能区域分割处理感兴趣区域,筛选区域下的灰度值区间范围。
图像灰度值为16位深度图。
* 读取灰度图像
read_image (Image, '/123.tif')** 获取图像尺寸
get_image_size (Image, Width, Height)* 分割图像为左右两部分(高能量和低能量区域)
gen_rectangle1 (LeftROI, 0, 0, Height-1, Width/2-1)
gen_rectangle1 (RightROI, 0, Width/2, Height-1, Width-1)* 提取左右区域图像
reduce_domain (Image, LeftROI, LeftImage)
reduce_domain (Image, RightROI, RightImage)* 预处理 - 增强对比度
emphasize (LeftImage, LeftEnhanced, 7, 7, 1.0)
emphasize (RightImage, RightEnhanced, 7, 7, 1.0)* 中值滤波去噪
median_image (LeftEnhanced, LeftFiltered, 'circle', 2, 'mirrored')
median_image (RightEnhanced, RightFiltered, 'circle', 2, 'mirrored')* 阈值分割提取矿石区域
threshold (LeftFiltered, LeftRegions, 50, 43055)
threshold (RightFiltered, RightRegions, 50, 43055)* 形态学处理去除小噪点
connection (LeftRegions, LeftConnected)
connection (RightRegions, RightConnected)
select_shape (LeftConnected, LeftOres, 'area', 'and', 500, 9999999)
select_shape (RightConnected, RightOres, 'area', 'and', 500, 9999999)* 计算每个矿石区域的灰度特征
* 高能量矿石区域分析
count_obj (LeftOres, NumLeftOres)
for i := 1 to NumLeftOres by 1select_obj (LeftOres, SingleOre1, i)* 计算区域的平均灰度值intensity (SingleOre1, LeftFiltered, MeanIntensity1, Deviation1)min_max_gray (SingleOre1, LeftFiltered, 0, Min1, Max1, Range1)* 根据灰度值分类if (MeanIntensity1 > 180)Class := 'High-grade ore'Color := 'blue'elseif (MeanIntensity1 > 120)Class := 'Medium-grade ore'Color := 'yellow'elseClass := 'Low-grade ore'Color := 'red'endif* 获取区域边界框smallest_rectangle1 (SingleOre1, Row1, Col1, Row2, Col2)gen_rectangle1 (Rectangle1, Row1, Col1, Row2, Col2)* 显示结果dev_set_color (Color)dev_display (SingleOre1)endfor* 低能量矿石区域分析
count_obj (RightOres, NumRightOres)
for j := 1 to NumRightOres by 1select_obj (RightOres, SingleOre2, j)* 计算区域的平均灰度值intensity (SingleOre2, RightFiltered, MeanIntensity2, Deviation2)min_max_gray (SingleOre2, RightFiltered, 0, Min2, Max2, Range2)* 根据灰度值分类(使用不同的阈值)if (MeanIntensity2 > 150)Class := 'High-grade ore'Color := 'green'elseif (MeanIntensity2 > 90)Class := 'Medium-grade ore'Color := 'yellow'elseClass := 'Low-grade ore'Color := 'red'endif* 获取区域边界框smallest_rectangle1 (SingleOre2, Row1, Col1, Row2, Col2)gen_rectangle1 (Rectangle2, Row1, Col1, Row2, Col2)* 显示结果dev_set_color (Color)dev_display (SingleOre2)endforstop()