一、Halcon周遊xld,halcon中統計數目歸納下:
圖形元組變量計算數目 是使用 count_obj算子統計
控制元組變量計算數目 是通過|元組名稱|進行的(對控制類型數組用||統計)
count_obj(DeformedContours, NumberContours)
area_center_xld (DeformedContours, Area1, Row1, Column1, PointOrder)
for i := 1 to NumberContours by 1
select_obj (DeformedContours, ObjectSelected, i)
length_xld (ObjectSelected, Length)
get_contour_xld (ObjectSelected, row, col)
endfor
gen_region_contour_xld (DeformedContours, Region, 'filled')
region_to_bin(Region, Binary, 0, 255, Width, Height)
overpaint_region (Binary, Region1, color8, 'fill')
write_image (Binary, 'bmp', 0, 'E:/Org.bmp')
二、Region、XLD、Polygon之間的互相轉換
1、Region像素坐标值的存儲方式
根據如下代碼生成一個矩形區域,并擷取該區域内所有像素的坐标值,根據結果可以看出,Region内像素坐标值是根據行程編碼的規則存儲的。
gen_rectangle1 (ROI_0, 100, 100, 300, 300)
get_region_points (ROI_0, Rows, Columns)
2、XLD像素坐标值的存儲方式
根據如下代碼,将上面的Region轉換為XLD,并擷取該XLD上所有像素的坐标值,根據結果可以看出,XLD上像素坐标值是按照順時針的順序依次存儲的。
gen_contour_region_xld (ROI_0, Contours, 'border')
get_contour_xld (Contours, Row, Col)
3、Polygon像素坐标值的存儲方式
根據如下代碼,生成一個區域,先轉換為XLD再轉換為Polygon,并擷取該Polygon上所有像素的坐标值,根據結果可以看出,Polygon上像素坐标值是按照順時針的順序依次存儲的,并且是一些列離散的點集(隻有在拐角處會存在點)
gen_region_runs (ROI_0, [140,141,142,143,143,144,145,146,147,148,149,150,151,151,152,152,153,153,154,154,155,155,156,156,157,157,158], [189,187,185,172,183,166,164,162,161,160,158,157,156,171,155,170,154,169,154,169,153,168,153,168,168,184,168], [189,189,189,180,189,189,189,189,189,189,189,189,166,189,163,189,161,189,159,189,157,189,155,189,175,189,168])
gen_polygons_xld (Contours, Polygons, 'ramer', 2)
get_polygon_xld (Polygons, Row, Col, Length, Phi)
gen_region_points (Region, Row, Col)
4、Region、XLD、Polygon之間的相關轉換
當我們檢測不連續的劃痕缺陷時,通過邊緣檢測算子得到的xld也是不連續的。這時我們可以将不連續的xld合并為一個整體,但是我們将這個合并後的xld轉換為region後,region已經不是一條線而是一個閉合的區域。
read_image (Image, 'C:/Users/SWD-AR05/Desktop/10.png')
lines_gauss (Image, Lines, 1.5, 3, 8, 'dark', 'true', 'bar-shaped', 'true')
union_adjacent_contours_xld (Lines, UnionContours, 100, 10, 'attr_keep')
gen_region_contour_xld (UnionContours, Region, 'filled')
針對上述問題,我們可以先将合并後的xld轉換為polygon,然後将polygon轉換為region,此時region就是一條連續的線。
gen_polygons_xld (UnionContours, Polygons, 'ramer', 2)
gen_region_polygon (Region1, Row, Col)
三、region to image
HALCON提供了三種方法:region_to_bin、region_to_label、region_to_mean.
1. region_to_bin(Region,BinImage,ForegroundGray,BackgroundGray,Width,Height)它将一個區域轉化成一個二進制位元組圖像。給區域内的所有像素賦給前景灰階值,如果輸入區域大于生成的圖像,則會在圖像邊界處截斷;
threshold (Image1, Region, 128, 255)
region_to_bin(Region, Binary, 0, 255, Width, Height)
write_image (Binary, 'bmp', 0, 'E:/Org.bmp')
或者
binary_threshold (Image1, BrightRegion, 'max_separability', 'dark', UsedThreshold)
region_to_bin(BrightRegion, SaveBinary, 0, 255, Width, Height)
write_image (SaveBinary, 'jpeg 100', 0, 'd:/Org.jpg')
2. region_to_label(Region,ImageLabel,Type,Width,Height)它将區域轉化為一個标簽圖像,通過索引值:第一個區域賦予灰階值1,第二個區域賦予灰階值2,依此類推……這裡僅僅使用正的灰階值,直到256。區域大于生成圖像則會被适當地截斷。如果區域重疊,則較高值的圖像會被輸出。如果想重疊,可以調用expand_region進行處理。Type='int2'、'int4'、‘byte'
3. region_to_mean(Regions,Image,ImageMean)用它們的均值來填充圖像區域,傳回Image。這個操作符主要用來可視化分割結果。