天天看点

机器视觉halcon例程学习笔记之边缘定位

**

halcon例程_方法_边缘定位

例子:rim.hdev

* Inspection of rim & tire
* The circular holes are extracted as well as the region containing
* the imprinted characters.
* 
* Set the display parameters and read the image
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 768, 576, 'black', WindowID)
*设置显示字体,字号16,字体mono,加粗,不倾斜
set_display_font (WindowID, 16, 'mono', 'true', 'false')
read_image (Rim, 'rim')
dev_display (Rim)
*显示继续消息,等待用户按F5继续执行
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 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)
*显示继续消息,等待用户按F5继续执行
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
*选择圆度超过0.85,面积超过50像素
select_shape (DarkRegions, Circles, ['circularity','area'], 'and', [0.85,50], [1.0,99999])
dev_display (Rim)
* 设置12个输出颜色
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)
*计算2个区域的差距
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仅选择长度大于30像素的轮廓
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)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 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选择40到400像素区域
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 ()
* 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选择面积至少为400像素的区域
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 ()
dev_set_colored (12)
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)```

           

继续阅读