![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb827d80f345bdf5eee4f21931557ee7.png)
思路:
- 找到电气柜的区域
- 根据绿色通道对绿色敏感,找到绿色按钮
- 特征过滤,与空对象相比,确定绿色按钮是否存在
- 填充后截取出来,转为亚像素,筛选和计算中心点
- 中心点是否在规定区域内
*关闭窗口
dev_close_window()
read_image(image, 'F:/xue_xi/彩色按钮.jpg')
get_image_size(image, Width, Height)
*打开窗口
dev_open_window_fit_image(image, 0, 0, Width, Height, windowHandle)
*显示图片
dev_display(image)
*分割通道
decompose3(image, ImageR, ImageG, ImageB)
*---------------------------------------------------
*选取电气柜的区域
*转灰度图
rgb1_to_gray(image, GrayImages)
* 二值化
threshold(ImageB, RegionBox, 50, 128)
*填充
fill_up(RegionBox, RegionFillUp)
*截取填充区域
reduce_domain (image, RegionFillUp, ImageReduced)
*---------------------------------------------------
*在电气柜中找绿色相关
* 二值化
threshold(ImageReduced, RegionButton, 0, 50)
*选择绿色通道 截取
reduce_domain (ImageG, RegionButton, ImageReducedGreenButton)
*二值化
threshold (ImageReducedGreenButton, GreeButtonRegion, 56, 255)
* 分离非连通域
connection(GreeButtonRegion, ConnectedRegions)
*特征筛选
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 200, 99999)
*-----------------------------------------------------------------------
*判读按钮是否与空对象相等,若相等,那么按钮则不存在
*生成一个空对象
gen_empty_obj(Emptyobject)
*对比 返回1为真 0为假
test_equal_obj(SelectedRegions, Emptyobject, IsEqual)
*填充按钮区域
fill_up(SelectedRegions, FilledGreenButtom)
*截取
reduce_domain(GrayImages, FilledGreenButtom, ImageReducedGreenButtinFilled)
*转换亚像素精度
threshold_sub_pix(ImageReducedGreenButtinFilled, Border, 82)
* 筛选XLD特征
select_shape_xld(Border, SelectedXLD, 'area', 'and', 100, 99999)
* 计算绿色按钮的中心位置
area_center_points_xld (SelectedXLD, Area, Row, Column)
* 设置字体
set_display_font (windowHandle, 14, 'mono', 'true', 'false')
* 显示数字
disp_message (windowHandle, 'x='+Column+','+'y='+Row+'\n'+'面积='+ Area, 'image', Row+20, Column, 'black', 'true')
*定一个范围:130,170--150,190
i:=0
if (Row < 150 and Row >130 and Column > 170 and Column < 190)
i:=1
disp_message (windowHandle, '合格', 'image', Row+40, Column-60, 'black', 'true')
endif