【机器视觉】HALCON工业机器视觉基础教程

第二章 HALCON基础知识

案例

read_image(Image,'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/brake_disk/brake_disk_part_01.png')
threshold(Image,Region,206,255)
connection(Region,ConnectedRegions)
opening_circle(ConnectedRegions,RegionOpening,3.5)
select_shape(RegionOpening,SelectedRegions,'area','and',1344.09,500000)
area_center(SelectedRegions,Area,Row,Column)
//找出面积最大的圆
m:=Area[0]
n:=0
for i:=1 to |Area|-1 by 1
    if(m < Area[i])
        m:=Area[i]
        n := i
    endif
endfor
// 标注该圆的面积
set_tposition(5604,Row[n],Column[n])
write_string(5604,Area[n])

2.1 1+2+…+100

S := 0
for i:=1 to 100 by 1
    S := S + i
endfor

2.2 输入圆的半径和周长,输出圆的周长和面积

girth := h
area := 3.141592653 * r * r
return ()

2.3

if(x<1)
    y:=x
elseif(x>=1 and x<10)
    y:=2*x+1
endif
return ()

2.4 计算图片尺寸

read_image(Image,'printer_chip/printer_chip_01')
*打开图像窗口
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)

*获取图像大小
get_image_size (Image, Width, Height)

*打开具有给定最小和最大范围的新图形窗口,以便保留给定图像的纵横比
*WidthLimit设置为-1,则使用以下默认值:[500,800]
*HeightLimit设置为-1,则使用以下默认值:[400,600]
*如果根据窗口宽高限制无法创建窗口,则忽略最小窗口大小的限制
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)

*指定活动窗口ID
dev_set_window (WindowHandle)

*展示图像变量
dev_display(Image)

get_image_size(Image, Width, Height)
set_tposition(WindowHandle,Height/2,Width/2)
write_string(WindowHandle,[Width,Height])

https://blog.csdn.net/qq_48034474/article/details/120076541

(https://img-blog.csdnimg.cn/f81e823345034a339ecf3ac18c00b90f.png)

2.5 计算两点之间长度

row := [143.22,13.55,15.11]
column := [34.67,12.88,76.43]
n1 := 0
n2 := 0
distance_pp(row[0],column[0],row[1],column[1],d)
for i:=0 to |row|-1 by 1
    for j:=0 to |column|-1 by 1
        distance_pp(row[i],column[i],row[j],column[j],d1)
        if(d < d1)
            d :=d1
            n1 := i
            n2 := j
        endif
    endfor
endfor

2.6 找到圆和坐标 连线

图形预处理
找到圆和坐标

read_image(Image,'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/brake_disk/brake_disk_part_01.png')
* 创建自适应图形窗口
* 获取图像大小
get_image_size(Image,Width,Heught)
* 打开新图形窗口
dev_open_window_fit_image(Image,0,0,-1,-1,WindowHandle)
* 指定活动窗口ID
dev_set_window(WindowHandle)
* 展示图像变量
dev_display(Image)

* 计算圆面积 圆心坐标
* 二值化
threshold(Image,Region,206,255)
* 设置连通区域
connection(Region,ConnectedRegions)
* 获取所有圆区域
opening_circle(ConnectedRegions,RegionOpening,5)
* 去除噪声 
select_shape(RegionOpening,SelectedRegions,'area','and',1344.09,500000)
* 找到圆面积和坐标
area_center(SelectedRegions,Area, Row, Column)

连线

* 绘制线段
for i:=1 to |Row|-1 by 1
    * draw_line(WindowHandle,Row[0],Column[0],Row[i],Column[i])
    disp_line(WindowHandle,Row[0],Column[0],Row[i],Column[i])
endfor

在这里插入图片描述
计算连线长度及连线与水平轴的夹角

angle_ll( Row[0], Column[0], Row[1], Column[1], 150, 200, 200, 200 , Angle)

第四章 灰度图像BLOB分析

4.1 BLOB概念

read_image(Image,'particle')
* 二值化 阈值分割
threshold(Image,BrightPixels,110,255)
* 构建连通阈
connection(BrightPixels,ConnectedRegions)
* 特征值计算
area_center(ConnectedRegions,Area,Row,Column)

4.3 阈值分割

轮廓获取

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/photometric_stereo/embossed_01.png')

* 均值滤波
mean_image(Image,ImageMean, 59, 59)

* 动态阈值
dyn_threshold(Image, ImageMean, RegionDynThresh, 15, 'not_equal')

* 做开闭运算,得到完整圆点
closing_circle(RegionDynThresh, RegionClosing, 8.5)
opening_circle(RegionClosing, RegionOpening, 6.5)

* 获取连通阈
connection(RegionOpening, ConnectedRegions)

* 获取包围圆
smallest_circle(ConnectedRegions, Row, Column, Radius)

* 创建圆或圆弧的XLD轮廓
gen_circle_contour_xld(ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)

4.5 特征值计算

* 打开图片
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/calib/calib_distorted_01.png')

* 二值化 
threshold(Image, Region, 0, 30)

* 连通域
connection(Region, ConnectedRegions)

* 根据圆度和面积选择区域
select_shape(ConnectedRegions, SelectedRegions, ['roundness','area'], 'and', [0.8,44.48],[1, 200])

在这里插入图片描述

习题

  1. 求回形针位置,数量
* 打开图片
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/clip.png')

* 二值化
threshold(Image, Region, 10, 40)

* 连通阈分析
connection(Region, ConnectedRegions)

* 闭运算
closing_circle(ConnectedRegions, RegionClosing, 150)

* 选择回形针区域
select_shape(RegionClosing, SelectedRegions, 'area', 'and', 9000, 20000)

count_obj(SelectedRegions, Number)

area_center(SelectedRegions, Area, Row, Column)

在这里插入图片描述
3. 找圆点

* 打开图片
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/caltab.png')

* 二值化
threshold(Image, Region, 0, 80)

* 连通阈分析
connection(Region, ConnectedRegions)

* 选择回形针区域
select_shape(ConnectedRegions, SelectedRegions, ['roundness','area'], 'and', [0.8,44.48], [20, 20000])

* 求面积
area_center(SelectedRegions, Area, Row, Column)

在这里插入图片描述
5. 提取手写字母表

* 打开图片
read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/alpha2.png')

* 数据增强
emphasize(Image, ImageEmphasize, 7, 7, 1)

* 二值化
threshold(Image, Region, 0, 80)

* 连通阈分析
connection(Region, ConnectedRegions)

* 选择回形针区域
select_shape(ConnectedRegions, SelectedRegions, 'column', 'and', 10,458)

* 求面积
area_center(SelectedRegions, Area, Row, Column)

在这里插入图片描述

第五章 图像滤波

5.2 空间域图像滤波

均值滤波

dev_close_window()

dev_open_window(0, 0, 512, 512, 'black', WindowHandle)

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/circular_barcode.png')

add_noise_white(Image, ImageNoise, 60)

dev_open_window(0, 0, 512, 512, 'black', WindowHandle)

mean_image(ImageNoise, ImageMean, 9, 9)

dev_display(ImageMean)

在这里插入图片描述
均值滤波

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/particle.png')

threshold(Image, Large, 110, 255)

* 圆角膨胀
dilation_circle(Large, LargeDilation, 7.5)

* 返回补充图像
complement(LargeDilation, NotLarge)

* 返回图像
reduce_domain(Image, NotLarge, ParticlesRed)

* 平滑处理
mean_image(ParticlesRed, ImageMean, 31, 31)

* 动态灰度阈值
dyn_threshold(ParticlesRed, ImageMean, RegionDynThresh, 3, 'light')

* 消除小区域
opening_circle(RegionDynThresh, RegionOpening, 2.5)

*连通域
connection(RegionOpening, ConnectedRegions)

在这里插入图片描述

在这里插入图片描述

频率域高通滤波

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/tooth_rim.png')

rgb1_to_gray(Image, GrayImage)

get_image_size(GrayImage, Width, Height)

* 构造高斯低通滤波器
gen_gauss_filter(ImageGauss, 0.1, 0.1, 0, 'none', 'dc_center', Width, Height)

* 构造值为1的实数型图像
gen_image_const(Image1, 'real', Width, Height)
paint_region (Image1, Image1, ImageResult, 1, 'fill')

* 构造高斯高通滤波器
sub_image(ImageResult, ImageGauss, ImageSub, 1, 0)

fft_generic(Image,ImageFFT1, 'to_freq', -1, 'none', 'dc_center', 'complex')

* 使用高通滤波器实现滤波
convol_fft(ImageFFT1, ImageSub, ImageConvol)

* 频域转为时域
fft_image_inv(ImageConvol, ImageFFTInv)

频率域带通滤波器

划痕检测

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/surface_scratch.png')

* 反转图像
invert_image(Image, ImageInvert)

get_image_size(Image, Width, Height)
***********************************************************************
* 图像滤波
* 创建带通滤波器
gen_sin_bandpass(ImageFilter, 0.7, 'none', 'rft', Width, Height)

* 傅里叶变换
rft_generic(ImageInvert, ImageFFT, 'to_freq', 'none', 'complex', Width)

* 带通滤波
convol_fft(ImageFFT, ImageFilter, ImageConvol)

* 反变换
rft_generic(ImageConvol, Lines, 'from_freq', 'n', 'byte', Width)
***********************************************************************
* 得到划痕区域
* 二值化
threshold(Lines, Region, 5, 255)
* 分区域
connection(Region, ConnectedRegions)
* 选择面积
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 5, 5000)
* 膨胀
dilation_circle(SelectedRegions, RegionDilation, 5.5)
* 合并区域
union1(RegionDilation, RegionUnion)
* 获取图像
reduce_domain(Image,RegionUnion, ImageReduced)
***********************************************************************
* 将划痕区域连成线
* 检测线条及其宽度
lines_gauss(ImageReduced, Lines1, 0.8, 3, 9, 'dark', 'false', 'bar-shaped', 'false')
* 合并近似共线轮廓
union_collinear_contours_xld(Lines1, UnionContours, 40, 3, 3, 0.2, 'attr_keep')
* 按线条长度选择线
select_shape_xld(UnionContours, SelectedXLD, 'contlength', 'and', 15, 1000)

* 从xld等高线创建一个区域
gen_region_contour_xld(SelectedXLD, Region1, 'filled')
union1(Region1, RegionUnion1)
dilation_circle(RegionUnion1, RegionDilation1, 10.5)

在这里插入图片描述
习题
提取纹理

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/wood_knots.png')

emphasize(Image, ImageEmphasize, 7, 7, 1)

scale_image(Image, ImageScaled, 0.6, 9)

mean_image(Image, ImageMean, 3, 3)

invert_image(ImageMean, ImageInvert)

get_image_size(Image, Width, Height)

gen_sin_bandpass(ImageFilter, 0.2, 'none', 'rft', Width, Height)

rft_generic(ImageInvert, ImageFFT, 'to_freq', 'none', 'complex', Width)

convol_fft(ImageFFT, ImageFilter, ImageConvol)

rft_generic(ImageConvol,Lines,'from_freq', 'n', 'byte', Width)

threshold(Lines, Region, 5,  25)

connection(Region, ConnectedRegions)

select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 10, 5000)

dilation_circle(SelectedRegions, RegionDilation, 5.5)

union1(RegionDilation, RegionUnion)

reduce_domain(ImageMean, RegionUnion, ImageReduced)

lines_gauss(ImageReduced, Lines1, 0.8, 3, 5, 'dark', 'false', 'bar-shaped', 'false')

union_collinear_contours_xld(Lines1, UnionContours, 40, 10, 10, 0.2, 'attr_keep')

select_shape_xld(UnionContours, SelectedXLD, 'contlength', 'and', 100, 1000)

gen_region_contour_xld(SelectedXLD, Region1, 'filled')

union1(Region1, RegionUnion1)

在这里插入图片描述

第六章 图像的形态学处理

边界检测

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/metal-parts/work_sheet_02.png')

threshold(Image, Region, 115, 255)

erosion_rectangle1(Region, RegionErosion, 11, 11)

difference(Region, RegionErosion, RegionDifference)

在这里插入图片描述

瑕疵检测

read_image (Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/fin3.png')

threshold(Image, Region, 147, 255)

closing_circle(Region, RegionClosing,220)

difference(RegionClosing, Region, RegionDifference)

opening_circle(RegionDifference, RegionOpening, 3.5)

area_center(RegionOpening, Area, Row, Column)

在这里插入图片描述

第七章 图像的几何变换

标签旋转

read_image (Image, 'C:/Users/DELL/AppData/Roaming/MVTec/HALCON-21.05-Progress/examples/images/barcode/25interleaved/25interleaved_exposure_04.png')

threshold(Image, Region, 49, 255)

connection(Region, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 77900.9, 235228)
shape_trans(SelectedRegions, RegionTrans, 'rectangle2')

area_center(RegionTrans, Area, Row, Column)

reduce_domain(Image, RegionTrans, ImageReduced)

orientation_region(RegionTrans, Phi)

hom_mat2d_identity(HomMat2DIdentity)

hom_mat2d_rotate(HomMat2DIdentity, -Phi, Column, Row, HomMat2DRotate)

affine_trans_image(ImageReduced, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')

在这里插入图片描述

透射变换

dev_set_draw('margin')

read_image(Image, 'C:/Users/DELL/AppData/Roaming/MVTec/HALCON-21.05-Progress/examples/images/datacode/ecc200/ecc200_to_preprocess_001.png')

* 获取二维码区域
threshold(Image, Region, 0, 90)

* 将区域形状变换为convex
shape_trans(Region, RegionTrans, 'convex')

* 从区域生成 XLD 轮廓
gen_contour_region_xld(RegionTrans, Contours, 'border')

* 分割轮廓为线段
segment_contours_xld(Contours, ContoursSplit, 'lines', 5, 10, 1)

XCoordCorners := []
YCoordCorners := []

* 对段进行排序
sort_contours_xld(ContoursSplit, SortedContours, 'upper_left', 'true', 'row')

* 求段数量
count_obj(SortedContours, Number)

for Index := 1 to Number by 1
    select_obj(SortedContours, ObjectSelected, Index)
    * 找到起始点
    fit_line_contour_xld(ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
    * 连接X值
    tuple_concat(XCoordCorners, RowBegin, XCoordCorners)
    * 连接Y值
    tuple_concat(YCoordCorners, ColBegin, YCoordCorners)
endfor

* 为每个输入点输出一个十字形的轮廓
gen_cross_contour_xld(Cross, XCoordCorners, YCoordCorners, 6, 0.785398)

px1 := 70
px2 := 270
py1 := 100
py2 := 300

* 创建投影变换矩阵
vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners, [px2,px1,px2,px1 ], [py2,py2,py1,py1], 'normalized_dlt',[],[],[],[],[],[],HomMat2D1, Covariance)
* 将投影变换应用于图像
projective_trans_image(Image, TransImage1, HomMat2D1, 'bilinear', 'false', 'false')

在这里插入图片描述

习题

  1. 器件旋转至水平
* 旋转至水平
read_image(Image, 'C:/Users/Public/Documents/MVTec/HALCON-20.11-Progress/examples/images/metal-parts/bracket_tilted_02.png')

threshold(Image, Region, 70, 255)

fill_up(Region, RegionFillUp)

shape_trans(RegionFillUp, RegionTrans, 'rectangle2')

area_center(RegionTrans, Area, Row, Column)

reduce_domain(Image, RegionFillUp, ImageReduced)

orientation_region(RegionFillUp, Phi)

hom_mat2d_identity(HomMat2DIdentity)

hom_mat2d_rotate(HomMat2DIdentity, -Phi, Row, Column, HomMat2DRotate)

affine_trans_image(ImageReduced, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值