二维测量--轮辋和轮胎的检查

对应示例程序:
rim.hdev

目标:提取圆孔以及包含印记字符的区域

思路为:
      1.读取图像,窗口初始化
      2.先定位圆孔的位置和测量直径:
         a.分割提取出灰度较低的区域 再根据圆度和面积筛选出较小的圆孔
         b.之后 膨胀一次 腐蚀一次 求二者之间的差值图像进行目标区域的定位
         c.根据上述定位区域 截取ROI图像 再进行边缘检测 和椭圆拟合 得到 圆孔的直径等相关测量参数
      3.定位字符区域:
         a.将图像进行局部阈值分割,再根据面积,椭圆半径等参数的筛选,定位出字符区域
         b.上面提取的图像 有干扰 不太好区分 就把他合并成一个区域 先闭操作进行放大 再根据面积筛选出包含ROI的不规则区域
         b.将不规则区域进行凸包操作
         d.将与之前的阈值图 进行交集计算 抠出ROI
      总结:这个例程其实主要用到的就是Blob分析的手法。

图像:
                                                                       原图在这里插入图片描述
                                                                    圆孔的定位和测量结果图在这里插入图片描述
                                                                     包含干扰区域的字符图像

在这里插入图片描述
                                                                       闭操作结果图

在这里插入图片描述
                                                                       凸包图

在这里插入图片描述

                                                                       结果图

在这里插入图片描述

代码:

* Set the display parameters and read the image
//初始化  设置窗口  读入图像
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 768, 576, 'black', WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
read_image (Rim, 'rim')
dev_display (Rim)
disp_continue_message (WindowID, 'black', 'true')
stop ()

//Blob分析:  
//阈值分割提取出灰度较低的区域  再根据圆度和面积筛选出较小的圆孔
//之后 膨胀一次 腐蚀一次  求二者之间的差值图像进行目标区域的定位
//根据上述定位区域  截取ROI区域  再进行边缘检测  和椭圆拟合  得到 圆孔的直径等相关测量参数

* To find the holes, we first segment the dark regions
*为了找到洞,我们首先对暗区域进行分割
threshold (Rim, Dark, 0, 128)
dev_display (Rim)
dev_set_color ('green')
dev_set_draw ('fill')
dev_display (Dark)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* Determine all connected components
connection (Dark, DarkRegions)
* Select regions with circular shape and an area larger than 50 pixel
select_shape (DarkRegions, Circles, ['circularity','area'], 'and', [0.85,50], [1.0,99999])
dev_display (Rim)
dev_set_colored (12)
dev_display (Circles)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* To extract a region that contains just the edges of the circles
* use dilation and erosion.
dilation_circle (Circles, ROIOuter, 8.5)
erosion_circle (Circles, ROIInner, 8.5)
difference (ROIOuter, ROIInner, ROI)
union1 (ROI, ROIEdges)
dev_display (Rim)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('green')
dev_display (ROIEdges)
disp_continue_message (WindowID, 'black', 'true')
stop ()

//边缘检测
* Reduce the region of interest (domain) to the extracted
* regions containing the edges.
reduce_domain (Rim, ROIEdges, RimReduced)
* Extract subpixel precise edges
edges_sub_pix (RimReduced, Edges, 'canny', 4, 20, 40)
* Select only the contours with length larger than 30 pixels
select_contours_xld (Edges, RelEdges, 'length', 30, 999999, 0, 0)
dev_display (Rim)
dev_set_colored (12)
dev_display (RelEdges)
disp_continue_message (WindowID, 'black', 'true')
stop ()

//拟合圆
* Fit an ellipse to the extracted edges
fit_ellipse_contour_xld (RelEdges, 'ftukey', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Ra, Rb, StartPhi, EndPhi, PointOrder)
display_ellipses (Rim, Row, Column, Phi, Ra, Rb, WindowID)
* dev_display (Rim)
* disp_ellipse (WindowID, Row, Column, Phi, Ra, Rb)
* disp_arrow (WindowID, Row - Ra * sin(Phi), Column + Ra * cos(Phi), Row + Ra * sin(Phi), Column - Ra * cos(Phi), 1)
* disp_arrow (WindowID, Row + Ra * sin(Phi), Column - Ra * cos(Phi), Row - Ra * sin(Phi), Column + Ra * cos(Phi), 1)
* Phi2 := Phi + 1.57
* disp_arrow (WindowID, Row - Rb * sin(Phi2), Column + Rb * cos(Phi2), Row + Rb * sin(Phi2), Column - Rb * cos(Phi2), 1)
* disp_arrow (WindowID, Row + Rb * sin(Phi2), Column - Rb * cos(Phi2), Row - Rb * sin(Phi2), Column + Rb * cos(Phi2), 1)
* dev_set_color ('yellow')
* for I := 1 to |Row| by 1
*     disp_message (WindowID, 'd: ' + Diameter[I - 1], 'image', Row[I - 1] - (Ra[I - 1] * 1.5), Column[I - 1] - 70, 'yellow', 'false')
* endfor
disp_continue_message (WindowID, 'black', 'true')
stop ()
///接下来 进行字符的定位

//Blob分析:
//将图像进行局部阈值分割,再根据面积,椭圆半径等参数的筛选,定位出字符区域
* Next, the goal is to find the characters in the image.
* Extract small regions with low gray value.
gauss_filter (Rim, RimGauss, 11)  //高斯滤波
dyn_threshold (Rim, RimGauss, SmallAndDarkerRegion, 5, 'dark')  //局部阈值分割图像
dev_display (Rim)
dev_set_draw ('fill')
dev_set_color ('green')
dev_display (SmallAndDarkerRegion)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* Compute connected components
connection (SmallAndDarkerRegion, SmallAndDarker)
* Select all regions with an area between 40 and 400 pixels
select_shape (SmallAndDarker, CharCandidates, 'area', 'and', 40, 400)
* From these regions, select those that can be enclosed by
* a given elliptic region.
select_shape (CharCandidates, PossibleChar, ['ra','rb'], 'and', [10,5], [20,30])  //根据椭圆的半径 进行筛选
dev_display (Rim)
dev_set_colored (12)
dev_display (PossibleChar)
disp_continue_message (WindowID, 'black', 'true')
stop ()


//上面提取的图像 有干扰 不太好区分  就把他合并成一个区域 先闭操作进行放大  再根据面积筛选出包含ROI的不规则区域
//之后 将不规则区域进行凸包操作 
//最后 将与之前的阈值图 进行交集计算  抠出ROI


* Extract connected regions containing all the character
union1 (PossibleChar, ROI)
closing_circle (ROI, CharRegion, 17.5)
connection (CharRegion, CharBlocks)
dev_display (Rim)
dev_set_draw ('margin')
dev_display (CharBlocks)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* Select the region that has an area of at least 400 pixel
select_shape (CharBlocks, CharRegion, 'area', 'and', 400, 99999)
* Transform the region into its enclosing rectangle
shape_trans (CharRegion, ROIChar, 'rectangle2')   //将区域转换成一个矩形
dev_display (Rim)
dev_set_color ('green')
dev_display (ROIChar)
disp_continue_message (WindowID, 'black', 'true')
stop ()
display_ellipses (Rim, Row, Column, Phi, Ra, Rb, WindowID)
* Extract all character candidate regions
* that lie in the extracted rectangle region.
intersection (CharCandidates, ROIChar, Characters)  //计算区域的交集  其实就是定位字符区域
* Show the extracted characters
dev_set_colored (12)
dev_display (Characters)


//下面就是 进行可视化的优化    开个小窗口 把字符摆正显示
* Fit the smallest surrounding rectangle and get its orientation
smallest_rectangle2 (ROIChar, RowChar, ColumnChar, PhiChar, Length1Char, Length2Char)
* Rotate the image and regions so that the characters are parallel to the lower image border
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, rad(180) - PhiChar, RowChar, ColumnChar, HomMat2DRotate)
affine_trans_region (Characters, CharRotated, HomMat2DRotate, 'constant')
affine_trans_image (Rim, RimRotated, HomMat2DRotate, 'weighted', 'false')
dev_open_window (0, 800, 192, 144, 'black', WindowChar)
* For zooming in, extract a rectangle containing the characters
union1 (CharRotated, CharRotatedAll)
smallest_rectangle1 (CharRotatedAll, Row1, Column1, Row2, Column2)
ZoomHeight := Row2 - Row1 + 1
ZoomWidth := Column2 - Column1 + 1
ZoomFactor := min([768 / ZoomWidth,576 / ZoomHeight])
ZoomRow := (Row1 + Row2) / 2
ZoomColumn := (Column1 + Column2) / 2
dev_set_part (ZoomRow - 576 / ZoomFactor / 2, ZoomColumn - 768 / ZoomFactor / 2, ZoomRow + 576 / ZoomFactor / 2 - 1, ZoomColumn + 768 / ZoomFactor / 2 - 1)
dev_display (RimRotated)
dev_set_draw ('fill')
dev_set_colored (12)
dev_display (CharRotated)

用到的几个算子:
    dyn_threshold --局部阈值分割图像
    select_shape --根据参数筛选图像
    shape_trans–转变区域的形状# 学习目标:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值