【機械視覺】Halcon—【六、交集并集差集和仿射變換】
目錄
【機械視覺】Halcon—【六、交集并集差集和仿射變換】
介紹
交集并集差集介紹:
1. 交集(Intersection)
2. 并集(Union)
3. 差集(Difference)
交集并集差集實例
仿射變換
介紹
例子:
旋轉變換例子:
平移、縮放、斜切變換例子
Vector_angle_to_rigid-既可以旋轉-也可以實現平移
多次變換實例
介紹
在圖像處理和計算機視覺領域,交集、并集和差集是基本的集合操作,它們在圖像分析中尤為重要,特別是在圖像分割、特征提取和對象識別等任務中。Halcon(現為MVTEC Vision++)是一個強大的機器視覺軟件,提供了豐富的圖像處理函數,可以用來執行這些集合操作。
交集并集差集介紹:
1. 交集(Intersection)
交集操作是指兩個圖像或圖像區域的重疊部分。在Halcon中,可以使用intersection
函數來獲取兩個區域的交集。
示例代碼:
read_image(Image1, 'path_to_image1')read_image(Image2, 'path_to_image2')intersection(Image1, Image2, ImageIntersection)
這里,ImageIntersection
將包含Image1
和Image2
的交集部分。
2. 并集(Union)
并集操作是指兩個圖像或圖像區域的合并部分,包括它們重疊的部分。在Halcon中,可以使用union1
或union2
函數來獲取兩個區域的并集。
示例代碼:
read_image(Image1, 'path_to_image1')read_image(Image2, 'path_to_image2')union1(Image1, Image2, ImageUnion)
或者使用union2
:
union2(Image1, Image2, ImageUnion)
這里,ImageUnion
將包含Image1
和Image2
的并集部分。
3. 差集(Difference)
差集操作是指從一個圖像或圖像區域中減去另一個圖像或圖像區域的部分。在Halcon中,可以使用difference
函數來獲取差集。
示例代碼:
read_image(Image1, 'path_to_image1')read_image(Image2, 'path_to_image2')difference(Image1, Image2, ImageDifference)
這里,ImageDifference
將包含在Image1
中但不在Image2
中的部分。
交集并集差集實例
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'clip')
threshold (Image, Region, 0, 128)*connection算子是Halcon中用于區域連通性分析的核心算子,它能夠:* 將輸入區域分割成互不連通的獨立區域* 識別并標記圖像中所有的連通組件* 為后續的形狀選擇和特征分析提供基礎
connection (Region, ConnectedRegions)* 合并區域
union1 (ConnectedRegions, RegionUnion)gen_circle (Circle, 200, 200, 100.5)
gen_circle (Circle1, 300, 200, 90.5)* 交集
intersection (Circle, Circle1, RegionIntersection)* 反選
complement (Circle, RegionComplement)* 差集
difference (Circle, Circle1, RegionDifference)dev_clear_window ()
dev_display (RegionComplement)
仿射變換
介紹
在Halcon中,仿射變換是一種常用的圖像處理技術,用于對圖像進行縮放、旋轉、平移、斜切等操作。Halcon是一個強大的機器視覺軟件庫,它提供了豐富的函數來執行各種圖像處理任務,包括仿射變換。
例子:
仿射變換分為三步: 初始化矩陣--->旋轉變換---->應用
* 第一步先創建一個初始化的矩陣(齊次矩陣)
hom_mat2d_identity (HomMat2DIdentity)
* 第二步旋轉變換
* 參數1 原始的矩陣
* 參數2 旋轉的弧度數 rad(90)
* 參數3 變換的中心點
* 參數4 生成的旋轉的變換矩陣
hom_mat2d_rotate (HomMat2DIdentity, rad(30), Row, Column, HomMat2DRotate)* 第三步 旋轉矩陣應用到圖片上
* 參數1 應用到的圖片
* 參數2 輸出圖片
* 參數3 要應用的旋轉矩陣
* 參數4 邊界的處理方式* 'constant' 填充空白區域為灰色的
* 參數5 是否自動適應變換結果* 'false' 輸出圖和輸入圖像尺寸一樣* 'true' 自動調整尺寸以顯示完整的變換結果
affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
旋轉變換例子:
* 仿射變換就是在圖形中的變換包括: 平移、縮放、旋轉、斜切(將斜體字導正)
dev_clear_window ()
read_image (Image, 'C:/Users/86152/Desktop/HALCON/一、課件/2025.5.29-/yuanx.jpg')* 獲取圖像中心點
area_center (Image, Area, Row, Column)* 第一步先創建一個初始化的矩陣(齊次矩陣)
hom_mat2d_identity (HomMat2DIdentity)
* 第二步旋轉變換
* 參數1 原始的矩陣
* 參數2 旋轉的弧度數 rad(90)
* 參數3 變換的中心點
* 參數4 生成的旋轉的變換矩陣
hom_mat2d_rotate (HomMat2DIdentity, rad(30), Row, Column, HomMat2DRotate)* 第三步 旋轉矩陣應用到圖片上
* 參數1 應用到的圖片
* 參數2 輸出圖片
* 參數3 要應用的旋轉矩陣
* 參數4 邊界的處理方式* 'constant' 填充空白區域為灰色的
* 參數5 是否自動適應變換結果* 'false' 輸出圖和輸入圖像尺寸一樣* 'true' 自動調整尺寸以顯示完整的變換結果
affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
平移、縮放、斜切變換例子
dev_clear_window ()
read_image (Image, 'C:/Users/86152/Desktop/HALCON/一、課件/2025.5.29-/yuanx.jpg')* 1 創建一個初始化矩陣
hom_mat2d_identity (HomMat2DIdentity)* 2 創建一個平移矩陣
* 參數2 豎直方向移動
* 參數3 水平移動
*hom_mat2d_translate (HomMat2DIdentity, 64, 0, HomMat2DTranslate)* 3 應用平移矩陣
*affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')* -------------------------------------------
* 縮放變換
* 參數2、3 水平和豎直方向縮放的倍數
* 參數4、5 變換的中心點
*hom_mat2d_scale (HomMat2DIdentity, 2, 2, 0, 0, HomMat2DScale)
*affine_trans_image (Image, ImageAffineTrans1, HomMat2DScale, 'constant', 'false')*-------------------------------------------
* 斜切變換
hom_mat2d_slant (HomMat2DIdentity, rad(20), 'x', 0, 0, HomMat2DSlant)affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
Vector_angle_to_rigid-既可以旋轉-也可以實現平移
dev_clear_window ()
read_image (Image, 'C:/Users/86152/Desktop/HALCON/一、課件/2025.5.29-/車牌.png')
* 獲取圖片中心坐標
area_center (Image, Area, Row, Column)
* 獲取角度
orientation_region (Image, Phi)* 求平移或旋轉矩陣
* 參數1、2 起始點的變換中心
* 參數3 原始圖片旋轉角度
* 參數4、5 變換到的位置
* 參數6 變換之后的角度
vector_angle_to_rigid (Row,Column,Phi ,Row ,Column ,rad(190) , HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
多次變換實例
read_image (Image, 'C:/Users/86152/Desktop/HALCON/一、課件/2025.5.29-/三角.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
* 合并兩個區域
*union1 (ConnectedRegions, RegionUnion)* 獲取連通區域的中心位置
area_center (Region, Area, Row, Column)
* 如果使用vector_angle_to_rigid算子 需要先獲取初始角
*orientation_region (Region, Phi)* 生成一個初始化矩陣
hom_mat2d_identity (HomMat2DIdentity)
* 先做平移變換
hom_mat2d_translate (HomMat2DIdentity, 200, 100, HomMat2DTranslate)
affine_trans_image (Image, ImageAffineTrans_one, HomMat2DTranslate, 'constant', 'false')
dev_display (ImageAffineTrans_one)* 再次平移
hom_mat2d_translate (HomMat2DIdentity, 0, 270, HomMat2DTranslate1)
affine_trans_image (Image, ImageAffineTrans_two, HomMat2DTranslate1, 'constant', 'false')
dev_display (ImageAffineTrans_two)a:=Row+200
* 在上一次基礎之上再做變換
hom_mat2d_rotate (HomMat2DIdentity, rad(180), 87,350, HomMat2DRotate)
affine_trans_image (ImageAffineTrans_two, ImageAffineTrans_three, HomMat2DRotate, 'constant', 'false')
dev_display (ImageAffineTrans_three)
?