基于halcon的HSV颜色匹配实例

it2024-07-24  39

HSV颜色识别

原图代码结果

原图

代码

dev_update_off () dev_close_window () *颜色数组 FuseColors := ['Orange','Red','Blue','Yellow','Green'] *画笔颜色数组 DisplayColors := ['coral','red','blue','goldenrod','forest green'] * HSV模型中H分量: Orange 10-30, Red 0-10,Blue 125-162,Yellow 30-64,Green 96-128 ... HueRanges := [10,30,0,10,125,162,30,64,96,128] read_image (Image, 'color_fuses_00.png') dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) *1、给颜色空间转换创建查找表 create_color_trans_lut ('hsv', 'from_rgb', 8, ColorTransLUTHandle) Imagepath := 'color_fuses_0' for i := 0 to 4 by 1 read_image (Image1, Imagepath+i) dev_display (Image1) dev_set_draw ('margin') dev_set_line_width (3) *2、RGB图像分解R 、G 、 B 通道 decompose3 (Image1, ImageR, ImageG, ImageB) *3、使用预先生成的查找表进行颜色空间转换。 apply_color_trans_lut (ImageR, ImageG, ImageB, ImageResultH, ImageResultS, ImageResultV, ColorTransLUTHandle) *4、使用S分量提取保险丝区域 threshold (ImageResultS, Regions, 65, 255) *5、在H分量中截取保险丝区域 reduce_domain (ImageResultH, Regions, ImageReducedH) for j := 0 to |FuseColors|-1 by 1 threshold (ImageReducedH, CurrentFuseRegion, HueRanges[2*j], HueRanges[2*j+1]) connection (CurrentFuseRegion, ConnectedRegions) fill_up (ConnectedRegions, RegionFillUp) select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 5000, 20000) shape_trans (SelectedRegions, RegionTrans, 'convex') smallest_rectangle2 (RegionTrans, Row1, Column1, Phi, Length1, Length2) dev_set_color (DisplayColors[j]) dev_display (RegionTrans) for k :=0 to |Row1|-1 by 1 set_tposition (WindowHandle, Row1[k], Column1[k]-0.4*Length1[k]) write_string (WindowHandle, FuseColors[j]+(k+1)) endfor endfor disp_message (WindowHandle, '颜色识别结果_'+(i+1), 'window', 10, 10, 'black', 'true') dump_window (WindowHandle, 'png', '颜色识别结果_'+(i+1)) stop () endfor

结果

最新回复(0)