检测钻石的边缘,以及确定钻石顶点的位置

对应示例程序:
measure_diamond.hdev

目标:检测钻石的边缘,以及确定钻石顶点的位置

思路为:
      1.读取图像
      2.通过二值化,提取出钻石所在的区域
      3.利用先验知识,把能代表钻石的尖尖的部分,裁剪出来(算子 clip_region_rel )
      4.对尖尖的那部分图像进行形态学处理和边缘检测,并拟合直线
      5.最后,对直线进行排序和显示,并求出直线间的交点,作为钻石的顶点位置

图像:
在这里插入图片描述
在这里插入图片描述

代码:

//读图像 配置窗口  字体大小
dev_update_off ()
dev_close_window ()
read_image (Image, 'diamond/diamond_01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (2)
* 
NumImages := 5
for Index := 1 to NumImages by 1
    read_image (Image, 'diamond/diamond_' + Index$'.2')  //读取文件中的图像
    * 
    * Segment diamond region
    threshold (Image, Region, 128, 255)      //阈值分割
    clip_region_rel (Region, RegionClipped, 0, 290, 0, 0)  //对分割后的图像进行裁剪 其实就是从上删几行,从下开始删几行等
                          //(输入依次为:顶部剪切的行数,底部剪切的行数,左边剪切的列数,右边剪切的列数 )
    
    shape_trans (RegionClipped, RegionTrans, 'convex')   //凸包  就是用一个直线围城的区域这个ROI@区域包起来
    
    //形态学取边缘  原图膨胀一次  原图腐蚀一次  再取两次的差异 那么就是边缘图  对于较为干净的图像适合
    dilation_rectangle1 (RegionTrans, RegionDilation, 5, 1)  //膨胀
    erosion_rectangle1 (RegionTrans, RegionErosion, 5, 1)    //腐蚀
    
    difference (RegionDilation, RegionErosion, RegionDifference)  //差异
    reduce_domain (Image, RegionDifference, ImageReduced)   //抠图
    * 
    * Subpixel accurate fitting of the edges
    edges_sub_pix (ImageReduced, Edges, 'canny', 3, 5, 5)    //亚像素边缘检测
    union_collinear_contours_xld (Edges, UnionContours, 100, 10, 4, rad(10), 'attr_keep') //合并近似共线轮廓
    select_contours_xld (UnionContours, SelectedContours, 'contour_length', 50, 2000, 0, 0)  //筛选出符合指定长度的轮廓
    fit_line_contour_xld (SelectedContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)  //拟合XLD轮廓
    * 
    * Sort the edge contours and determine their intersection
    *对边缘轮廓进行排序并确定其交点
    tuple_sort_index (RowBegin, Indices)   //对元组的元素进行排序并返回已排序元组的索引
    //找到两条直线的起始点和终点
    RowBegin := subset(RowBegin,Indices)
    RowEnd := subset(RowEnd,Indices)
    ColBegin := subset(ColBegin,Indices)
    ColEnd := subset(ColEnd,Indices)
    Nr := subset(Nr,Indices)   //法向量的行坐标
    Nc := subset(Nc,Indices)   //法向量的列坐标
    Dist := subset(Dist,Indices)  //原点到该线的距离
    //画线
    //也可以这样画出直线段 ,但是显示的不太明显,例程中做了一定的调整
    //gen_contour_polygon_xld (LineEdge1, [RowBegin[0],RowEnd[0]], [ColBegin[0],ColEnd[0]])
    //gen_contour_polygon_xld (LineEdge2, [RowBegin[1],RowEnd[1]], [ColBegin[1],ColEnd[1]])
    
    gen_contour_polygon_xld (LineEdge1, [Dist[0] / Nr[0],0], [0,Dist[0] / Nc[0]])
    gen_contour_polygon_xld (LineEdge2, [Dist[1] / Nr[1],Width - 1], [0,(Dist[1] - (Width - 1) * Nr[1]) / Nc[1]])
    
   
    //取两条直线的交点,并画× 用于显示
    //取两条直线的交点,并画× 用于显示
    intersection_lines (RowBegin[0], ColBegin[0], RowEnd[0], ColEnd[0], RowBegin[1], ColBegin[1], RowEnd[1], ColEnd[1], RowTip, ColTip, IsOverlapping)
    gen_cross_contour_xld (Cross, RowTip, ColTip, 20, rad(45))
    * 
    * Display the results
    dev_display (Image)
    dev_set_color ('yellow')
    dev_display (LineEdge1)
    dev_display (LineEdge2)
    dev_set_color ('blue')
    dev_display (Cross)
    if (Index != NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

用到的几个算子:
      clip_region_rel (Region, RegionClipped, 0, 290, 0, 0) //对分割后的图像进行裁剪 其实就是从上删几行,从下开始删几行等 //(输入依次为:顶部剪切的行数,底部剪切的行数,左边剪切的列数,右边剪切的列数 )
       gen_contour_polygon_xld-- 画多边形,也用来画直线
       intersection_lines–求线的交点

参考资料:
[1].https://wenda.so.com/q/1372264895065922
[2].https://www.cnblogs.com/yuexinzheng1989/p/7481811.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值