halcon采圖
- halcon.net
- halcon.dll 放到debug下
sdk1采圖
采集流程
-
轉(zhuǎn)正 (ROI目標區(qū)轉(zhuǎn)正)仿射變換
平移
選擇
縮放
分割 二值化 - >二值圖
-
形態(tài)學(xué)分析
- 膨脹
- 腐蝕
- 開運算 細小腐蝕
- 閉運算 細小膨脹
-
特征提取
- 定位
- 圖像預(yù)處理
- 圖像分割
- 顏色
- 輪廓定位
- 定位
圖像分類

二值圖形態(tài)學(xué)分析
灰度圖的形態(tài)學(xué)分析


放射變換






效果越好時間越長


halcon 實現(xiàn)仿射變換
畫出區(qū)域做基礎(chǔ)的仿射變換

* 1. 關(guān)閉之前的窗口(避免多個窗口干擾)
dev_close_window ()
* 2. 新建512x512的黑色窗口(Width=512,Height=512,背景黑色)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
* 3. 手動畫區(qū)域(在窗口內(nèi)拖動鼠標畫任意形狀,右鍵確認)
disp_message (WindowHandle, '拖動鼠標畫區(qū)域(右鍵確認)', 'window', 12, 12, 'white', 'true')
draw_region (Region, WindowHandle)
* 4. 顯示原始區(qū)域(讓你看到畫的區(qū)域在哪里)
dev_display (Region)
disp_message (WindowHandle, '原始區(qū)域', 'window', 36, 12, 'green', 'true')
stop () // 暫停,按F5繼續(xù)
* 5. 定義仿射變換矩陣(平移→縮放,順序:后定義的變換先執(zhí)行)
hom_mat2d_identity (HomMat2DIdentity) // 初始化單位矩陣(無任何變換)
* 5.1 平移:相對于窗口左上角(0,0),向右移64,向下移64(row=64,column=64)
hom_mat2d_translate (HomMat2DIdentity, 64, 64, HomMat2DTranslate)
* 5.2 縮放:相對于窗口左上角(0,0),x/y方向均放大2倍(scale_x=2,scale_y=2)
hom_mat2d_scale (HomMat2DTranslate, 2, 2, 0, 0, HomMat2DScale) // 最終變換矩陣:縮放+平移
* 6. 對區(qū)域執(zhí)行仿射變換(關(guān)鍵:用最終的HomMat2DScale,不是HomMat2DTranslate)
affine_trans_region (Region, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')
* 7. 顯示變換后的區(qū)域(清空窗口→顯示原始區(qū)域→顯示變換后區(qū)域,對比效果)
dev_clear_window ()
dev_display (Region) // 原始區(qū)域(黑色窗口中白色部分)
dev_set_color ('red') // 變換后的區(qū)域設(shè)為紅色,方便區(qū)分
dev_display (RegionAffineTrans) // 變換后的區(qū)域(紅色)
* 8. 提示信息
disp_message (WindowHandle, '黑色=原始區(qū)域 | 紅色=平移+縮放后區(qū)域', 'window', 12, 12, 'white', 'true')
在圖片中選出區(qū)域進仿射變換



dev_update_off () // 關(guān)閉實時更新,加快運行速度
dev_close_window () // 關(guān)閉舊窗口
* 1. 選擇并加載圖像
dev_open_file_dialog ('read_image', 'default', 'default', Selection)
if (Selection == '') // 防錯:用戶未選圖像直接取消
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
read_image (Image, Selection)
* 2. 新建窗口(適配圖像大小,避免拉伸)
get_image_size (Image, ImageWidth, ImageHeight)
dev_open_window (0, 0, ImageWidth, ImageHeight, 'black', WindowHandle)
dev_display (Image)
disp_message (WindowHandle, '原始圖像', 'window', 12, 12, 'white', 'true')
stop () // 暫停,按F5繼續(xù)
* 3. 畫矩形框選區(qū)域(水平矩形,右鍵確認)
disp_message (WindowHandle, '拖動鼠標畫矩形(框選目標區(qū)域,右鍵確認)', 'window', 40, 12, 'white', 'true')
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
* 4. 生成矩形區(qū)域+摳圖(兩種摳圖方式,按需選擇)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) // 生成矩形區(qū)域
reduce_domain (Image, Rectangle, ImageReduced) // 方式1:保留原圖尺寸,僅限制有效域(后續(xù)變換用這個)
crop_domain (ImageReduced, ImagePart) // 方式2:裁剪為矩形大小的獨立小圖像(可選)
* 顯示摳圖結(jié)果
dev_clear_window ()
dev_display (ImagePart) // 顯示裁剪后的小圖像(更直觀)
disp_message (WindowHandle, '摳圖后的區(qū)域', 'window', 12, 12, 'white', 'true')
stop () // 暫停,按F5繼續(xù)
* 5. 計算矩形區(qū)域的中心坐標(變換基準點)
area_center (Rectangle, Area, RegionCenterRow, RegionCenterCol)
* 6. 變換1:平移+旋轉(zhuǎn)組合(基于區(qū)域中心)
tuple_rad (45, RotAngle) // 目標旋轉(zhuǎn)角度:45度→弧度
* 剛性變換矩陣:原始中心(RegionCenterRow,RegionCenterCol)、無旋轉(zhuǎn)(0) → 新中心(上移50,右移30)、旋轉(zhuǎn)45度
vector_angle_to_rigid (RegionCenterRow, RegionCenterCol, 0, RegionCenterRow-50, RegionCenterCol+30, RotAngle, HomMat2D_TransRotate)
* 對圖像執(zhí)行變換(constant=填充色,true=保持原圖尺寸)
affine_trans_image (ImageReduced, ImageAffineTrans_TransRotate, HomMat2D_TransRotate, 'constant', 'true')
* 對區(qū)域執(zhí)行變換(和圖像變換一致)
affine_trans_region (Rectangle, RegionAffineTrans_TransRotate, HomMat2D_TransRotate, 'nearest_neighbor')
* 顯示平移+旋轉(zhuǎn)結(jié)果
dev_clear_window ()
stop ()
dev_display (Image) // 顯示原圖作為背景參考
stop ()
dev_display (ImageAffineTrans_TransRotate) // 變換后的圖像
stop ()
dev_set_color ('red')
dev_display (RegionAffineTrans_TransRotate) // 變換后的區(qū)域(紅色框)
disp_message (WindowHandle, '平移+旋轉(zhuǎn)后的區(qū)域(紅框)', 'window', 12, 12, 'white', 'true')
stop () // 暫停,按F5繼續(xù)
* 7. 變換2:純旋轉(zhuǎn)(中心不變,僅旋轉(zhuǎn)45度)
tuple_rad (45, RotAngle_Pure) // 目標旋轉(zhuǎn)角度:45度→弧度
* 剛性變換矩陣:原始中心、無旋轉(zhuǎn) → 同一中心、旋轉(zhuǎn)45度(無平移)
vector_angle_to_rigid (RegionCenterRow, RegionCenterCol, 0, RegionCenterRow, RegionCenterCol, RotAngle_Pure, HomMat2D_PureRotate)
* 對圖像執(zhí)行純旋轉(zhuǎn)
affine_trans_image (ImageReduced, ImageAffineTrans_PureRotate, HomMat2D_PureRotate, 'constant', 'true')
* 對區(qū)域執(zhí)行純旋轉(zhuǎn)
affine_trans_region (Rectangle, RegionAffineTrans_PureRotate, HomMat2D_PureRotate, 'nearest_neighbor')
* 顯示純旋轉(zhuǎn)結(jié)果
dev_clear_window ()
dev_display (Image) // 原圖背景
stop ()
dev_display (ImageAffineTrans_PureRotate) // 純旋轉(zhuǎn)后的圖像
stop ()
dev_set_color ('green')
dev_display (RegionAffineTrans_PureRotate) // 純旋轉(zhuǎn)后的區(qū)域(綠色框)
stop ()
disp_message (WindowHandle, '純旋轉(zhuǎn)后的區(qū)域(綠框)', 'window', 12, 12, 'white', 'true')
通過投影給畸變圖片復(fù)原



dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'C:/Users/12613/Documents/xwechat_files/wxid_zh4dvw96777z22_8506/msg/file/2025-11/微信圖片_20251124144244_455_151.png')
*投影變換
x:=[0,324,216,532]
y:=[0,493,111,590]
x1:=[0,0,535,535]
y1:=[0,590,0,590]
hom_vector_to_proj_hom_mat2d (x, y, [1,1,1,1], x1, y1, [1,1,1,1], 'normalized_dlt', HomMat2D)
projective_trans_image (Image, TransImage, HomMat2D, 'bilinear', 'false', 'false')
均值化直方圖

縮放直方圖

讓亮的更亮 暗的更暗


通過動態(tài)閾值(差值匹配圖像)

代碼
* read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/vessel.png')
*均值化直方圖
* equ_histo_image (Image, ImageEquHisto)
*縮放量的更亮暗的更暗
* scale_image (Image, ImageScaled, 1.97674, -77)
* read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/datacode/ecc200/ecc200_cpu_015.png')
*處理失焦指令
* shock_filter (Image, SharpenedImage, 0.5, 10, 'canny', 1)
* read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/circle_plate.png')
*均值濾波
* mean_image (Image, ImageMean, 9, 9)
*中值濾波
* median_image (ImageMean, ImageMedian, 'circle', 1, 'mirrored')
* read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/egypt1.png')
*自動閾值 直方圖平滑
* auto_threshold (Image, Regions, 4)
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/photometric_stereo/embossed_01.png')
mean_image (Image, ImageMean, 59, 59)
dyn_threshold (Image, ImageMean, RegionDynThresh1, 15, 'not_equal')
*膨脹
closing_circle (RegionDynThresh1, RegionClosing, 8.5)
opening_circle (RegionClosing, RegionOpening, 5.5)
connection (RegionOpening, ConnectedRegions)
*最小外接圓
smallest_circle (ConnectedRegions, Row, Column, Radius)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
dev_display (Image)
dev_display (ContCircle)