机器视觉halcon学习——检测缺口

编程入门 行业动态 更新时间:2024-10-12 03:17:20

机器视觉halcon学习——检测<a href=https://www.elefans.com/category/jswz/34/1726716.html style=缺口"/>

机器视觉halcon学习——检测缺口

目的:将图片中刀片上(那根白线)的缺口检测出来

效果图

dev_close_window()
dev_open_window(0, 0, 800, 800, 'black', WindowHandle)
*打开一个文件夹
list_files('D:/检测缺口', ['files','follow_links'], ImageFiles)
*选中里面的所有照片
tuple_regexp_select(ImageFiles,['.*','ignore_case'],ImageFiles)
for Index := 0 to |ImageFiles|-1 by 1
read_image(Image,ImageFiles[Index])
*灰度值变成第一通道
rgb1_to_gray(Image, GrayImage)
*连刀带缺口选出那一大块区域,去掉其他的小杂质
threshold(GrayImage, Region, 40, 255)
connection(Region, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 700, 99999)
*跟这个东西同向的画一个矩形(这里如何定位跟踪矩形暂时没有想到,目前是自己画的矩形,有大佬帮忙看看嘛)
orientation_region(SelectedRegions, Phi)
gen_rectangle1(Rectangle, 597,0,601,1599)
opening(SelectedRegions, Rectangle, RegionOpening)
*获取画的矩形的轮廓
gen_contour_region_xld(Rectangle, Contours, 'border')
fit_rectangle2_contour_xld(Contours, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi1, Length1, Length2, PointOrder)
*获取矩形位置
gen_rectangle2_contour_xld(Rectangle1, Row, Column, Phi1, Length1, Length2)lr:=Row+Length1*sin(Phi1)
lc:=Column-Length1*cos(Phi1)
gen_cross_contour_xld (Cross, lr, lc, 6, Phi1)rr:=Row-Length1*sin(Phi1)
rc:=Column+Length1*cos(Phi1)  
gen_cross_contour_xld (Cross1,rr,rc,6,Phi1)gen_contour_polygon_xld(Contour, Row, Column)
gen_cross_contour_xld (Cross1,rr,rc,6,Phi1)
*画一根中心线
gen_contour_polygon_xld (Contour, [lr,rr], [lc,rc])
gen_rectangle2 (Rectangle2, Row, Column, Phi1, Length1+20, Length2)
*关键的一步,最开始选的大部分区域跟自己画的矩形区域对比,找到多出来的那些缺口
difference (SelectedRegions, Rectangle2, RegionDifference)
dev_display(GrayImage)
dev_display(RegionDifference)
stop()area_center(RegionDifference, Area, Row, Column)if(Area>0)set_display_font(WindowHandle, 16, 'mono', 'true', 'false')disp_message(WindowHandle, '有崩口', 'window', 20, 20, 'black', 'true')elsedisp_message(WindowHandle, '正常', 'window', 20, 20, 'black', 'true') endif
stop()
endfor

(放几张比较有代表性的原图吧)
正常图片

小缺口

大缺口

这个有点像减法。
假设在下面这张图片中,你要把五角星的五个角提取出来。
1.首先你需要提取出五角星,可以根据灰度值等等方法提取。
2.你画一个矩形,把图片去减掉这个矩形,最后difference出五个角

更多推荐

机器视觉halcon学习——检测缺口

本文发布于:2024-02-27 17:33:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缺口   视觉   机器   halcon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!