设置首页收藏本站
开启左侧

把打光的边界使用卡尺工具进行拟合直线,求两条边界之间的距离,修改对应代码

[复制链接]
Laocui1992 发表于 2024-10-15 14:32:22 | 显示全部楼层 |阅读模式
悬赏500视觉币
  1. dev_open_window (0, 0, 512, 512, 'black', WindowHandle1)
  2. * Image Acquisition 01: Code generated by Image Acquisition 01
  3. list_files ('D:/工作/切割找边halcon代码/新建文件夹', ['files','follow_links'], ImageFiles)
  4. tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
  5. for Index := 0 to |ImageFiles| - 1 by 1     
  6.     read_image (Image, ImageFiles[Index])
  7.     **图像预处理,中值滤波
  8.     mean_image (Image, ImageMean, 10, 180)
  9.     **增强图像对比度
  10.     emphasize (ImageMean, ImageEmphasize, 77, 77, 15)
  11.     **提取亚像素轮廓算子
  12.     edges_sub_pix (ImageEmphasize, Edges, 'canny', 2, 30, 60)
  13.     **合并端点在一起的轮廓
  14.     union_adjacent_contours_xld (Edges, UnionContours, 2, 1, 'attr_keep')
  15.     **计算轮廓或多边形XLD的长度
  16.     length_xld (UnionContours, Length)
  17.     **按升序对元组的所有元素进行排序
  18.     tuple_sort_index (Length, Indices)
  19.     **反转一个数组
  20.     tuple_inverse (Indices, Inverted)
  21.     select_obj (UnionContours, ObjectSelected, Inverted[0]+1)
  22.     **拟合直线
  23.     fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
  24.     select_obj (UnionContours, ObjectSelected2, Inverted[1]+1)   
  25.     **拟合直线
  26.     fit_line_contour_xld (ObjectSelected2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr, Nc, Dist)
  27.     **生成直线
  28.     gen_contour_polygon_xld(Line, [RowBegin, RowEnd], [ColBegin, ColEnd])
  29.     **生成直线
  30.     gen_contour_polygon_xld(Line2, [RowBegin1, RowEnd1], [ColBegin1, ColEnd1])
  31.     **计算两条直线之间的距离
  32.     distance_ss(RowBegin, ColBegin, RowEnd, ColEnd,  RowBegin1, ColBegin1, RowEnd1, ColEnd1,  DistanceMin, DistanceMax)
  33.     dev_display (Image)   
  34.     disp_message(WindowHandle1, DistanceMin, 'window', 100, 100, 'red', 'true')
  35.     disp_message(WindowHandle1, [RowBegin,ColBegin,RowEnd,ColEnd], 'window', 10, 10, 'red', 'true')
  36.     dev_display(Line)
  37.     dev_display(Line2)
  38. *   dev_display (Image)
  39. *   dev_display (ObjectSelected)  
  40. *  dev_display (ObjectSelected2)
  41.     stop()
  42. endfor
复制代码

1728972523005.png 1728973086398.png 1728973108690.png 1728973124670.png 1728973141743.png 微信图片_20241014112132.jpg

最佳答案

查看完整内容

求两条直线的距离,准确来说应该是求两条平行直线的距离,不平行的直线距离是没有意义的,求平行线的距离可以取一条直线中的一点,求该点到另一条直线的距离,此图片中两条线段是近似平行的,也可以这样求,但因为线段是有长度的,所以可以依次遍历一条线段中所有点,求点到另一条线段的距离,最后求平均值 ...
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
halcon2016 发表于 2024-10-15 14:32:23 | 显示全部楼层
求两条直线的距离,准确来说应该是求两条平行直线的距离,不平行的直线距离是没有意义的,求平行线的距离可以取一条直线中的一点,求该点到另一条直线的距离,此图片中两条线段是近似平行的,也可以这样求,但因为线段是有长度的,所以可以依次遍历一条线段中所有点,求点到另一条线段的距离,最后求平均值
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
回复

使用道具 举报

halcon2016 发表于 2024-10-15 15:24:49 | 显示全部楼层
2024-10-15_152253.png
2024-10-15_152310.png
2024-10-15_152324.png
2024-10-15_152334.png
  1. * Image Acquisition 01: Code generated by Image Acquisition 01
  2. list_files ('C:/Users/Administrator/Desktop/新建文件夹', ['files','follow_links'], ImageFiles)
  3. tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
  4. dev_get_window (WindowHandle)
  5. set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
  6. for Index := 0 to |ImageFiles| - 1 by 1     
  7.     read_image (Image, ImageFiles[Index])
  8.     **图像预处理,中值滤波
  9.    
  10.     get_image_size (Image, Width, Height)
  11.    
  12.     mean_image (Image, ImageMean, 10, 180)
  13.     **增强图像对比度
  14.     emphasize (ImageMean, ImageEmphasize, 77, 77, 15)
  15.     **提取亚像素轮廓算子
  16.     edges_sub_pix (ImageEmphasize, Edges, 'canny', 2, 30, 60)
  17.     **合并端点在一起的轮廓
  18.     union_adjacent_contours_xld (Edges, UnionContours, 2, 1, 'attr_keep')
  19.     **计算轮廓或多边形XLD的长度
  20.     length_xld (UnionContours, Length)
  21.     **按升序对元组的所有元素进行排序
  22.     tuple_sort_index (Length, Indices)
  23.     **反转一个数组
  24.     tuple_inverse (Indices, Inverted)
  25.     select_obj (UnionContours, ObjectSelected, Inverted[0]+1)
  26.     **拟合直线
  27.     fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
  28.     select_obj (UnionContours, ObjectSelected2, Inverted[1]+1)   
  29.     **拟合直线
  30.     fit_line_contour_xld (ObjectSelected2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr, Nc, Dist)
  31.     **生成直线
  32.     gen_contour_polygon_xld(Line, [RowBegin,RowEnd ], [ColBegin, ColEnd])
  33.     **生成直线
  34.     gen_contour_polygon_xld(Line2, [RowBegin1, RowEnd1], [ColBegin1, ColEnd1])
  35.     **计算两条直线之间的距离
  36.     distance_ss(RowBegin, ColBegin, RowEnd, ColEnd,  RowBegin1, ColBegin1, RowEnd1, ColEnd1,  DistanceMin, DistanceMax)
  37.   
  38.     get_contour_xld (Line, Row, Col)
  39.     DD:=[]
  40.     for Index1 := 0 to |Row|-1 by 1
  41.           distance_pl (Row[Index1], Col[Index1], RowBegin1, ColBegin1, RowEnd1, ColEnd1, Distance)
  42.           DD:=[DD,Distance]
  43.     endfor
  44.     D:=mean(DD)
  45.    
  46.     dev_display (Image)   
  47.     dev_display(Line)
  48.     dev_display(Line2)
  49.     dev_disp_text ('距离D='+D$'.2f', 'image', Height/2, ColBegin1+120, 'red', [], [])
  50.     stop()
  51. endfor
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
回复

使用道具 举报

wenbo_bob 发表于 2024-11-14 13:55:26 | 显示全部楼层
学习一下
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表