天天看點

機器視覺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)```

           

繼續閱讀