一、图像处理需要的知识
1.图像处理基础(rgb(hsv),gray)
2.图像灰度变换(scale_img)
3.图像基础(emphasize)
4.图像几何变换(仿射变换,极坐标变换)
5.图像分割(blob分析、边缘检测、reduce_domain)
6.图像的频域(fft_img)
7.图像形态学
8.图像复原
9.运动图像
10.图像配准(模板匹配)
二、图像处理的预处理和分割过程
二、颜色识别的方法
颜色识别的方法
1.RGB->HSV (缺点:受光照影响严重)
重点分析H分量和S分量
2.分类器识别颜色
三、例子一
打开颜色识别的例子
通过对RGB转成HSV,然后因为要选出黄色,从而去限制色度和饱和度的范围,因此可以用thresh去限制饱和度和色度的范围。最后通过blob分析,获得指定颜色的物体。
完整代码
* color_simple.hdev: segment yellow cable in HSV color space dev_close_window () dev_open_window (0, 0, 640, 480, 'black', WindowHandle) for i := 1 to 2 by 1 * 读取图片 read_image (Image, 'cable' + i) * 将RGB图根据通道拆分成三张图 decompose3 (Image, Red, Green, Blue) * 将RGB转为HSV trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv') * 对饱和度阈值处理(限制饱和度范围) threshold (Saturation, HighSaturation, 100, 255) reduce_domain (Hue, HighSaturation, HueHighSaturation) * 对色度阈值处理(限制色度范围) threshold (HueHighSaturation, Yellow, 20, 50) * 计算连通域 connection (Yellow, ConnectedRegions) * 选择形状(特征) select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 0) * 开操作 closing_circle (SelectedRegions, Yellow, 3.5) * 获得目标 reduce_domain (Image, Yellow, ImageReduced) dev_display (HueHighSaturation) dev_display (ImageReduced) stop () endfor
四、例子二
这个例子,和例子一 同样的方式,只不过这个对 好几个颜色物体进行了检测,并且输出message
完整代码
* color_fuses.hdev: classify fuses by color dev_update_window ('off') * **** * step: set up fuse properties and hue ranges * **** FuseColors := ['Orange','Red','Blue','Yellow','Green'] FuseTypes := [5,10,15,20,30] * HueRanges: Orange 10-30, Red 0-10... HueRanges := [10,30,0,10,125,162,30,64,96,128] Count := 0 dev_close_window () dev_open_window (0, 0, 800, 600, 'black', WH) while (Count <= 4) * **** * step: acquire image * **** read_image (Image, 'color/color_fuses_0' + Count) dev_display (Image) set_tposition (WH, 12, 512) write_string (WH, 'color/color_fuses0' + Count + '.png') * **** * step: extract saturated hues * **** decompose3 (Image, Red, Green, Blue) trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv') threshold (Saturation, Saturated, 60, 255) reduce_domain (Hue, Saturated, HueSaturated) for Fuse := 0 to |FuseTypes| - 1 by 1 * **** * step: classify specific fuse * **** threshold (HueSaturated, CurrentFuse, HueRanges[Fuse * 2], HueRanges[Fuse * 2 + 1]) connection (CurrentFuse, CurrentFuseConn) fill_up (CurrentFuseConn, CurrentFuseFill) select_shape (CurrentFuseFill, CurrentFuseSel, 'area', 'and', 6000, 20000) area_center (CurrentFuseSel, FuseArea, Row1, Column1) dev_set_color ('magenta') for i := 0 to |FuseArea| - 1 by 1 set_tposition (WH, Row1[i], Column1[i]) write_string (WH, FuseColors[Fuse] + ' ' + FuseTypes[Fuse] + ' Ampere') endfor set_tposition (WH, 24 * (Fuse + 1), 12) dev_set_color ('slate blue') write_string (WH, FuseColors[Fuse] + ' Fuses: ' + |FuseArea|) endfor stop () Count := Count + 1 endwhile dev_update_window ('on')
五、例子三
比如要检测橙色的糖豆
检测的结果
完整代码
dev_close_window () read_image (Image, 'C:/Users/xuheding/Desktop/图片/5.png') dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image) decompose3 (Image, Red, Green, Blue) trans_from_rgb (Red, Green, Blue, Hue, Saturation, intensity, 'hsv') threshold (Hue, Regions, 4, 31) connection (Regions, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 9486.24, 12568.8) opening_circle (SelectedRegions, RegionOpening, 3.5) dev_set_draw ('margin') dev_set_line_width (3) dev_set_color ('magenta') dev_display (Image) dev_display(RegionOpening)