群里有人问题怎么测量下面的工件的尺寸?
勇哥把原图处理了一下,供大家练习。
下面代码是群中有人给出的一种思路,不完善。
其主要思路是:
。查找边缘Xld
。分割Xld
。循环判断每小段xld,通过其方向判断是不是我们需要的线条
dev_close_window() read_image (Image, 'C:/Users/hackpig/Desktop/QQ图片20191214110730.png') dev_open_window(0, 0, 512, 512, 'black', WindowHandle) decompose3(Image, Image1, Image2, Image3) get_image_size(Image2, Width, Height) gen_rectangle1 (ROI_0, 182.5, 452.972, 690.5, 613.139) reduce_domain(Image2, ROI_0, ImageReduced) threshold(ImageReduced, Region, 0, 150) region_to_bin(Region, BinImage, 255, 0, Width, Height) edges_sub_pix(BinImage, Edges, 'canny', 1, 20, 40) segment_contours_xld(Edges, ContoursSplit, 'lines_circles', 4, 3, 2) select_contours_xld(ContoursSplit, SelectedContours, 'contour_length', 10, 999999999, -0.5, 0.5) dev_display(Image) count_obj(SelectedContours, Number) for I:=1 to Number by 1 select_obj(SelectedContours, ObjectSelected, I) fit_line_contour_xld(ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist) line_orientation(RowBegin, ColBegin, RowEnd, ColEnd, Phi) Deg := deg(Phi) if (Deg < -85 and Deg > - 95 and ColBegin > 420 + 10) disp_line(WindowHandle, RowBegin, ColBegin, RowBegin, Width) disp_line(WindowHandle, RowEnd, ColBegin, RowEnd, Width) endif endfor
有些地方判断还是有误,如下图。
因此代码还需要完善。
另外,按题主的标注意图,测量点的两端刚好位于线条的圆角上面,无法精准确定具体在哪里?
所以这个案子勇哥认为恐怕得不到精确的测量结果。
2019/12/14勇哥注:
被论坛里的人打脸,勇哥说过无法获得精确测量的结果后,有人发出下图这个测量齿轮的例子。
这个能搞出来,我服了。
我收回我说过话,看来这个话题是有解的,有必要继续研究下去。
这个测量齿轮的例子应该是遍历点然后拟合的方式处理的,实际上,上面一个测量工件的例子也应该可以这样处理。
有空会试试。
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

