《HALCON機器視覺與算法原理編程實踐》第9章 特徵提取-學習筆記

9.1 區域形狀特徵

在場景中選擇物體的特徵是圖像測量或者識別的重要基礎。

9.1.1 區域的面積和中心點

dev_close_window ()
*讀取圖片
read_image(Image, 'data/holes')
*設置窗口屬性,爲了獲取窗口句柄,供後面顯示文字用
get_image_size (Image, Width, Height)
*創建新窗口
dev_open_window (0, 0, Width, Height, 'black', WindowID)
*將圖像轉化爲單通道灰度圖
rgb1_to_gray (Image, GrayImage)
*創建矩形選區,選擇感興趣部分
gen_rectangle1 (Rectangle, 180, 83, 371, 522)
reduce_domain (GrayImage, Rectangle, ROI)
*閾值處理,提取圖中深色部分,也就是文字部分,這裏閾值設置爲50,基本可以取出所有黑色文字
threshold ( ROI, Region, 0, 80)
*gen_image_proto (ImageReduced, ImageCleared, 0)
*dev_display (Region)
*提取中的整個區域中將不相連的部分分割成獨立的區域
connection (Region, ConnectedRegions)
*獲取不相連的區域的數量
count_obj (ConnectedRegions, Num)
*計算所有不相連區域的面積和中心點座標。Area表示面積, Row和 Column分別表示中心點座標
area_center (ConnectedRegions, Area, Row, Column)
*打印各區域的面積
for i := 1 to Num by 1
    dev_set_color ('red')
	select_obj (ConnectedRegions, ObjectSelected, i)
	*設定了輸出文字的起始座標點
	set_tposition (WindowID, Row[i - 1]+40, Column[i - 1])
	*設置打印文字的顏色
	dev_set_color ('blue')
	*設置字體
    * set_font (WindowID, '-System-32-*-*-0-0-0-1-GB2312_CHARSET-')
    *輸出文字內容,即該區域的面積
	write_string (WindowID, Area[i-1])
endfor

9.1.2 封閉區域(孔洞)的面積

*清空顯示窗口
dev_close_window ()
*讀取包含孔洞的圖片
read_image (Image, 'data/holes2')
*打開新的顯示窗口
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*將彩色圖像轉化爲灰度圖像,這是爲了後面的圖像二值化
rgb1_to_gray (Image, GrayImage)
*進行閾值處理,提取出圖中較亮的有封閉區域的(孔洞以外)背景區域
threshold (GrayImage, Region, 50,255)
*將背景區域作爲area_holes算子的輸入,計算所有孔洞的面積
area_holes (Region, Area)
*將面積計算結果以字符串形式顯示在窗口中
disp_message (WindowHandle, 'Size of holes: ' + Area + ' pixel', 'window', 10, 10, 'black', 'true')

9.1.3 根據特徵值選擇區域

當我們想要提取Region時,圖像處理後,往往存在幾個類似的Region,此時,需要根據Region的一些特殊特徵,來選擇指定的Region。

求Region指定特徵值:region_features(Regions : : Features : Value)

根據特徵值選擇區域:select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
在這裏插入圖片描述

*清空顯示窗口
dev_close_window ()
*讀取待檢測的圖像	
read_image (Image, 'data/crossShape')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*設置系統繪製的顏色
dev_set_color ('white')
*將彩色圖像轉化爲灰度圖像,這是爲了後面的圖像二值化
rgb1_to_gray (Image, GrayImage)
*創建矩形選區,選擇感興趣部分
gen_rectangle1 (Rectangle, 100, 127, 325, 490)
reduce_domain (GrayImage, Rectangle, ROI)
gen_image_proto (ROI, ImageCleared, 0)
*進行閾值處理,提取出圖中較暗的包含了孔洞的區域
threshold (ROI, Regions, 50, 255)
*將其不連通的部分獨立分割出來,成爲一組區域
connection (Regions, ConnectedRegions)
*設置系統繪製的顏色。爲了標記選擇的區域
dev_set_color ('yellow')
*方法一
*將閾值處理的結果區域作爲select_shape算子的輸入,根據區域的寬度,選擇出目標
select_shape (ConnectedRegions, SelectedRegions1, 'area', 'and', 1000, 99999)
*方法二
*先計算面積,再選擇出面積最大的目標
area_center(ConnectedRegions, Area, Row, Column)
select_shape (ConnectedRegions, SelectedRegions2, 'area', 'and', max(Area), 99999)
*方法三
select_shape_std (SelectedRegions1, SelectedRegion3, 'max_area', 70) 
dev_clear_window ()
dev_display (SelectedRegion3)

9.1.4 根據特徵值創建區域

在這裏插入圖片描述

dev_close_window ()
*讀取圖像
read_image (Image, 'data/crossShape')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
rgb1_to_gray (Image, GrayImage)
*創建矩形選區,選擇感興趣部分
gen_rectangle1 (Rectangle, 100, 127, 325, 490)
reduce_domain (GrayImage, Rectangle, ROI)
*進行閾值處理,提取出圖中較暗的包含了孔洞的區域
threshold (ROI, Regions, 50, 255)
*將其不連通的部分獨立分割出來,成爲一組區域
connection (Regions, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegion3, 'max_area', 70) 
dev_set_draw ('fill')
*求出了三個區域的最大內接圓的中心和半徑
inner_circle(SelectedRegion3,Row,Column,Radius)
*繪製圓形
gen_circle(Circles,Row,Column,Radius)
dev_set_window (WindowID)
*gen_image_proto (ROI, ImageCleared, 0)
*dev_display (SelectedRegion3)
*繪製形狀的邊緣
dev_set_draw ('margin')
dev_set_line_width (3)
*顯示內接圓形
dev_display (Image)
dev_display (Circles)

在這裏插入圖片描述

dev_close_window ()	
read_image (Image, 'data/garlic2')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
rgb1_to_gray (Image, GrayImage)
*使閾值處理提取出了較暗的區域
threshold (GrayImage, Region, 100, 255)
*將非連通區域分割成一組區域的集合
connection (Region, ConnectedRegions)
*利用面積特徵,將比較大快的區域分割出來
select_shape(ConnectedRegions,selectRegion,'area','and',50000,200000)
*求最小外接矩形
smallest_rectangle2 (selectRegion, Row1, Column1, Phi, Length1, Length2)
*根據矩形參數創建舉行的輪廓
gen_rectangle2_contour_xld (Rectangle1, Row1, Column1, Phi, Length1, Length2)
*顯示最小外接矩形
dev_set_window (WindowHandle)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
dev_display (Rectangle1)

  • inner_rectangle1:求一個區域的最大內接矩形
  • smallest_rectangle1:求最小外接矩形
    (與smallest_rectangle2的區別:smallest_rectangle1求出的矩形永遠是與圖像的水平座標軸平行的,不會發生旋轉)

9.2 基於灰度值的特徵

除了基於形狀的特徵以外,比較常用的還有基於灰度值的特徵,即利用灰度信息表現區域或者圖像的特徵。

9.2.1 區域的灰度特徵值

gray_features :計算指定區域的灰度特徵值
在這裏插入圖片描述

gray_features (Operator)
Name
gray_features — Calculates gray value features for a set of regions.

Signature
gray_features(Regions, Image : : Features : Value)

Description
gray_features has a set of regions (Regions) as input. For each of these regions the features (Features) are calculated and returned in Value. Possble values for Features:

'area'	灰度區域的面積
'row'	中心點的行座標
'column'中心點的列座標
'ra'	橢圓的長軸
'rb'	橢圓的短軸
'phi'	等效橢圓角度
'min'	灰度的最小值
'max'	灰度的最大值
'median'灰度的中值
'mean'	灰度的均值
'deviation'灰度的偏差
'plane_deviation' 近似平面的偏差
'anisotropy'
Anisotropy (see entropy_gray)
'entropy'
Entropy (see entropy_gray)
'fuzzy_entropy'
Fuzzy entropie of region (see fuzzy_entropy, with a fuzzy function from Apar=0 to Cpar=255)
'fuzzy_perimeter'
Fuzzy perimeter of region (see fuzzy_perimeter, with a fuzzy function from Apar=0 to Cpar=255)
'moments_row'
Mixed moments along a row (see moments_gray_plane)
'moments_column'
Mixed moments along a column (see moments_gray_plane)
'alpha'
Approximating plane, parameter Alpha (see moments_gray_plane)
'beta'
Approximating plane, parameter Beta (see moments_gray_plane)

9.2.2 區域的最大、最小灰度值

min_max_gray(Regions,Image::percent:Min,Max,Range)
此算子用於提取 Regions 所在區域圖像的最大與最小灰度值,
在這裏插入圖片描述

9.2.3 灰度的平均值和偏差

函數原型:intensity(Regions,Images:::Mean,Deviation)

功能:計算Images中Regions的均值和方差。

參數列表:
第1個參數Regions是輸入對象,即被計算區域
第2個參數Images是輸入變量,即灰度圖像
第3個參數Mean是輸出變量,即被測區域的灰度均值
第4個參數Deviation是輸出變量,即被測區域的灰度方差
在這裏插入圖片描述

9.2.4 灰度區域的面積和中心

函數原型:
area_center_gray(Regions: Image: : : Area, Row, Column)

函數作用:
計算灰度圖像中一個區域的面積和重心

函數原理:
該算子同area_center類似,但是它將圖像的灰度值也考慮在內,所以area_center_gray的面積計算的是灰度圖像的灰度容量。而它的重心是指灰度值的前兩個標準矩。

參數列表:
Regions:被檢測的區域
Image:灰度圖像
Row:重心的行座標
Column:重心的列座標

可能前置項:
threshold, regiongrowing, connection

可替代項:
area_center
在這裏插入圖片描述

9.2.5 根據灰度特徵值選擇區域

select_gray(Regions, Image : SelectedRegions : Features, Operation, Min, Max : )
函數計算圖像Image 的區域Regions內指定屬性Features,
在這裏插入圖片描述

*讀取輸入圖像
read_image (Image, 'data/village')
*獲取原始圖像的寬和高
get_image_size (Image, Width, Height)
*創建同尺寸的顯示圖像的窗口
dev_open_window (0, 0, Width, Height, 'white', WindowID)
*將圖像轉化爲灰度圖像
rgb1_to_gray (Image, GrayImage)
*使用均值濾波對灰度圖像進行平滑,以去除細節雜點
median_image (GrayImage, ImageMedian, 'circle', 2, 'mirrored')
*進行閾值處理,提取出較亮部分
threshold (ImageMedian, BrightRegion, 180, 255)
*使用開運算使各區域分離
opening_circle (BrightRegion, RegionClosing, 6.5)
*將不相連通的區域分割開來
connection (RegionClosing, BrightRestConnection)
*將面積較大的區域提取出來
select_shape (BrightRestConnection, SelectedRegions1, 'area', 'and', 5000, 99999)
*獲取這些區域的均值和偏差。由於湖面區域灰度值比較平滑,灰度偏差會比較小
intensity (SelectedRegions1, ImageMedian, Mean, Deviation)
*以灰度偏差爲條件,選出符合條件的區域
select_gray (SelectedRegions1, ImageMedian, SelectedRegions, 'deviation', 'and', 4, 10)
dev_clear_window ()
dev_display (GrayImage)
dev_display (SelectedRegions)

9.3 基於圖像紋理的特徵

形狀特徵描述了圖像中局部區域的幾何屬性。
圖像的表面紋理也是重要的特徵之一。

9.3.1 灰度共生矩陣

參考:灰度共生矩陣的原理及實現(特徵提取)-OpenCV
https://blog.csdn.net/qq_37059483/article/details/78292869

9.3.2 創建灰度共生矩陣

在這裏插入圖片描述

9.3.3 用共生矩陣計算灰度值特徵

函數原型:
cooc_feature_matrix(CoccMatrix : : : Energy, Correlation, Homogeneity, Contrast)

函數作用:
從共生矩陣中求出灰度特徵值

參數列表:
CoocMatrix(in):共生矩陣
Energy(out):灰度能量
Correlation(out):灰度相關性
Homogeneity(out):灰度的局部均勻性(一致性)
Contrast(out):灰度值對比

可能前置項:
gen_cooc_matrix

可替代項:
cooc_feature_image
在這裏插入圖片描述

9.3.4 計算共生矩陣並導出其灰度值特徵

cooc_feature_image - 計算共生矩陣並導出其灰度值特徵。

cooc_feature_image(Regions, Image : : LdGray, Direction : Energy, Correlation, Homogeneity, Contrast)

cooc_feature_image的調用對應於算子gen_cooc_matrix和cooc_feature_matrix的連續執行。 如果連續評估共生矩陣的幾個方向矩陣,則通過gen_cooc_matrix生成矩陣然後調用算子cooc_feature_matrix生成矩陣更爲有效。 參數Direction以角度或’mean’方式傳輸鄰域的方向。 在’mean’的情況下,平均值是在所有四個方向上計算的。
在這裏插入圖片描述
在這裏插入圖片描述

9.3.5 提取圖像的紋理特徵

在這裏插入圖片描述

dev_close_window ()
*讀取輸入的圖片
read_image (Image, 'data/board')
*將輸入的彩色圖像轉爲黑白圖像
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
*創建一個與輸入圖像同樣大小的窗口
dev_open_window (0, 0, Width/4, Height/4, 'black', WindowID)
*設定畫筆寬度
dev_set_line_width (5)
*創建兩個窗口用於顯示參數計算的結果
dev_open_window (0, 512, 320, 320, 'black', WindowID1)
dev_open_window (512, 512, 320, 320, 'black', WindowID2)
*分別設置兩個矩陣,選擇不同的兩部分區域
gen_rectangle1 (Rectangle1, 200,10, 380, 190)
gen_rectangle1 (Rectangle2, 580, 650, 730, 800)
*分別對兩個矩形求取灰度共生矩陣Matrix1和Matrix2
gen_cooc_matrix (Rectangle1, GrayImage, Matrix1, 6, 0)
gen_cooc_matrix (Rectangle2, GrayImage, Matrix2, 6, 0)
*分別對Matrix1和Matrix2提取灰度特徵參數
cooc_feature_matrix (Matrix1, Energy1, Correlation1, Homogeneity1, Contrast1)
cooc_feature_matrix (Matrix2, Energy2, Correlation2, Homogeneity2, Contrast2)
*採取另一種方式,直接對矩陣2的圖像求灰度特徵參數,結果與上面兩步計算出的參數是一致的
cooc_feature_image (Rectangle2, GrayImage, 6, 0, Energy3, Correlation3, Homogeneity3, Contrast3)
*顯示圖像窗口和兩個矩形的灰度共生矩陣
dev_set_window (WindowID)
dev_set_draw ('margin')
dev_display (GrayImage)
dev_display (Rectangle1)
dev_set_color('yellow')
dev_display (Rectangle2)
dev_set_window (WindowID1)
dev_display (Matrix1)
*以字符串的形式,分別在兩個矩陣的對應窗口上顯示灰度特徵值的計算結果
String := ['Energy: ','Correlation: ','Homogeneity: ','Contrast: ']
dev_set_color('red')
disp_message (WindowID1, String$'-14s' + [Energy1,Correlation1,Homogeneity1,Contrast1]$'6.3f', 'window', 12, 12, 'white', 'false')
dev_set_window (WindowID2)
dev_display (Matrix2)
dev_set_color('yellow')
String := ['Energy: ','Correlation: ','Homogeneity: ','Contrast: ']
disp_message (WindowID2, String$'-14s' + [Energy2,Correlation2,Homogeneity2,Contrast2]$'6.3f', 'window', 12, 12, 'white', 'false')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章