halcon 算子

语法基础

* a := 1                 赋值语句
* 
* if( a == 1 )           循环语句
*    b := 1 
* else
*    b := 2
* endif
* 
* for i := 1 to 10 by 1   for循环
*     a := a + 1
* endfor
* 
* switch(a)               switch语句
* case 1:
*     c := 1
*     break
* case 2:
*     c := 2
*     break
* endswitch
* 
* while(a)                while语句
*     a := a + 1
* endwhile

读取图片并改变某点像素的灰度值

* read_image(Image,'D:/数字图像处理/3_1.jpg')
* set_grayval(Image,0,0,255)

> 循环嵌套改变灰度值

read_image (Image, 'D:/数字图像处理/3_1.jpg')
for i := 0 to 127 by 1
    set_grayval (Image, i, i, 255)
endfor
for j := 0 to 127 by 1
     set_grayval (Image, 127-j, j, 255)
endfor

* for i := 1 to 100 by 1
*     for j := 1 to 100 by 1
*     set_grayval(Image,i,j,255)
*     endfor
* endfor

标准的图片显示操作

dev_close_window()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

将图像进行通道分解,分别转换为三个通道的RGB图像

decompose3 (Image, ImageR, ImageG, ImageB)
compose3 (ImageR, ImageG, ImageB, MultiChannelImage)

> 将图片转化为灰度图

rgb1_to_gray (Image, GrayImage)

> 图片转换--使用颜色转换将RGB三个通道图像转化为HSV通道的图像
> hsv里面的v指的是RGB里面的最大的值,v = max(max(r,g),b); 而HSI的I是平均值,I=(r+g+b) / 3; 

trans_from_rgb (ImageR, ImageG, ImageB, H, S, V, 'hsv')

trans_to_rgb (ImageB, ImageG, ImageR, ImageRed, ImageGreen, ImageBlue, 'hsi')

提取灰度值在128-255之间的内容

threshold (Image, Region, 128, 255)

改变亮度

scale_image (Image, ImageScaled, 1, 0)

数组及其赋值操作

a := [1,2,3]
b := a[1]
a[1] := 4

图像三元素分解

dev_close_window()
read_image (Image, 'D:/数字图像处理/image/1.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

decompose3 (Image, ImageR, ImageG, ImageB)
trans_from_rgb (ImageR, ImageG, ImageB, H, S, I, 'hsi')  
*将RGB转换为HSI
scale_image (Image, ImageScaled, 1.1, 0)
trans_to_rgb (H, S, ImageScaled, ImageRed, ImageGreen, ImageBlue, 'hsi')
compose3 (ImageR, ImageG, ImageB, MultiChannelImage)

抠图

threshold (Image, Region, 168, 255)
*选中灰度值在某一范围的图像
connection (Region, ConnectedRegions)
*将选中的图像分区域分割
area_center (ConnectedRegions, Area, Row, Column)
*计算按区域分割的图像的数据
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 9999)
*选择参数(面积)在该范围之内的图像
union1 (SelectedRegions, RegionUnion)
*分割区域合并在一起
difference (RegionUnion, RegionUnion, RegionDifference)
*区域求差
complement (SelectedRegions, RegionComplement)
*求补集

对比度变换

scale_image (GrayImage, ImageScaled, 3, 0)
*改变对比度
scale_image (ImageScaled, ImageScaled1, 1, 100)
*改变亮度
scale_image (ImageScaled, ImageScaled1, 1, -100)
*改变亮度
dump_window (WindowHandle, 'bmp', 'C:/Users/学习/Desktop/1')
*保存图片
invert_image (Image, ImageInvert)
*取反
scale_image_max (Image, ImageScaleMax)
*分段线性
emphasize (Image, ImageEmphasize, Width, Height, 1)
log_image (Image, LogImage, 'e')
*取对数
pow_image (Image, PowImage, 2)
*取指数

直方图修正

dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

rgb1_to_gray (Image, GrayImage)
gray_histo (GrayImage, GrayImage, AbsoluteHisto, RelativeHisto)
*计算相对和绝对灰度直方图 参数(区域,图像,绝对直方图(没有除以n),相对直方图(除以n))
gen_region_histo (Region, AbsoluteHisto, 255, 255, 1)
*画出直方图

equ_histo_image (GrayImage, ImageEquHisto)
*直方图均衡化

图像平滑

dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

gauss_distribution (20, Distribution)
*生成高斯噪声
add_noise_distribution (Image, ImageNoise, Distribution)
*添加噪声

mean_image (ImageNoise, ImageMean, 9, 9)
*对图像取平均  输入参数 输出参数 平滑模板

低通滤波器频域平滑

dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

sp_distribution (5, 5, Distribution)
*产生椒盐噪声 椒盐百分比(5% 5%)
add_noise_distribution (Image, ImageNoise, Distribution)
*添加噪声
gen_lowpass (ImageLowpass, 0.1, 'n', 'dc_center', Width, Height)
*产生理想低通滤波器  0.1是截至频率  dc_center/dc_edge模式
fft_generic (ImageLowpass, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
*计算图像的快速傅里叶变换  to_freq 变换方向  complex 复数(用于计算)
convol_fft (ImageFFT, ImageLowpass, ImageConvol)
*两个图像做卷积
fft_generic (ImageConvol, ImageFFT1, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')
*图像的快速反傅里叶变换

中值滤波


dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

sp_distribution (5, 5, Distribution)
add_noise_distribution (Image, ImageNoise, Distribution)
mean_image (ImageNoise, ImageMean, 3, 3)
*均值滤波
median_image (ImageNoise, ImageMedian, 'circle', 1, 'mirrored')
*中值滤波 模板形状 边缘处理方式

图像锐化

dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

*空间域

sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
*检测边缘   类型 大小
scale_image (EdgeAmplitude, ImageScaled, 5, 0)

laplace (Image, ImageLaplace, 'absolute', 3, 'n_4')
*拉普拉斯算子检测边缘
scale_image (EdgeAmplitude, ImageScaled, 5, 0)


*频域

gen_highpass (ImageHighpass, 0.1, 'none', 'dc_center', 512, 512)
*高通滤波器 
fft_generic (Image, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
convol_fft (ImageFFT, ImageHighpass, ImageConvol)
fft_generic (ImageConvol, ImageFFT1, 'from_freq', -1, 'sqrt', 'dc_center', 'byte')
scale_image (EdgeAmplitude, ImageScaled, 5, 0)

阈值分割

dev_close_window ()
read_image (Image, 'letters')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

*  threshold (Image, Region, 128, 255)
*  threshold (Image, Region1, [64,128], [122,255])
*数组形式

gray_histo (Image, Image, AbsoluteHisto, RelativeHisto)
histo_to_thresh (AbsoluteHisto, 8, MinThresh, MaxThresh)
*分割阈值区间
threshold (Image, Region2, MinThresh, MaxThresh)
threshold (Image, Region3, MinThresh[0], MaxThresh[0])


binary_threshold (Image, Region4, 'max_separability', 'dark', UsedThreshold)
*二分阈值    最大可分性 (最大内间方差)   'smooth_histo'

*图像分割 边界分割 区域分割
*阈值分割:双峰特性
*全体阈值 :迭代阈值 最优阈值 大津法(Otsu法 类间方差最大)
*局部阈值 :直方图变换法 (光度分布不均匀)

dev_close_window ()
read_image (Image, 'fabrik')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

rgb1_to_gray (Image, GrayImage)
threshold (Image, Regions, 155, 241)
*代码插入
threshold (Image, Region, 128, 255)
*  threshold (Image, Region1, [64,128], [122,255])
*数组形式

gray_histo (Image, GrayImage, AbsoluteHisto, RelativeHisto)
histo_to_thresh (AbsoluteHisto, 8, MinThresh, MaxThresh)
*分割阈值区间
threshold (Image, Region2, MinThresh, MaxThresh)
* threshold (Image, Region3, MinThresh[0], MaxThresh[0])

binary_threshold (GrayImage, Region4, 'max_separability', 'dark', UsedThreshold)
*二分阈值    最大可分性 (最大内间方差)  
binary_threshold (GrayImage, Region5, 'smooth_histo', 'dark', UsedThreshold1)

auto_threshold (GrayImage, Regions1, 2)
*自动多域值分割图像

*dyn_threshold (GrayImage, GrayImage, RegionDynThresh, 5, 'light')
*动态阈值

D := 5
mean_image(Image,Mean,D*2+1,D*2+1)
dyn_threshold(Image,Mean,Seg,5,'light')
connection(Seg,Regions)
*边缘读取

var_threshold (GrayImage, Region6, Width, Height, 0.2, 2, 'dark')

*局部阈值
local_threshold (GrayImage, Region7, 'adapted_std_deviation', 'dark', [], [])
dual_threshold (GrayImage, RegionCrossings, 20, 5, 2)

*边缘检测
*1.梯度算子(sobel)
*2.Canny边缘检测算子
*3.robel边缘检测算子

dev_close_window ()
read_image (Image, 'D:/数字图像处理/image/8.bmp')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

*roberts边缘检测算子
roberts (Image, ImageRoberts, 'gradient_sum')
threshold (ImageRoberts, Region, 178, 255)
skeleton (Region, Skeleton)
*细化

*sobel边缘检测算子
sobel_amp (ImageRoberts, EdgeAmplitude, 'sum_abs', 3)
threshold (EdgeAmplitude, Region1, 128, 255)
skeleton (Region1, Skeleton1)

*prewitt边缘检测算子
prewitt_amp (EdgeAmplitude, ImageEdgeAmp)
threshold (EdgeAmplitude, Region2, 128, 255)
skeleton (Region2, Skeleton2)

*高斯拉普拉斯边缘检测算子
laplace_of_gauss (Image, ImageLaplace, 2)
zero_crossing (ImageLaplace, RegionCrossing)
*过零点检测

*Canny边缘检测算子
edges_image (Image, ImaAmp, ImaDir, 'canny', 1, 'nms', 20, 40)
*提取图像的边缘 
threshold (ImaAmp, Region3, 1, 255)

*图像的代数运算
*合成:C(x,y) = A(x,y) + B(x,y)

add_image (ImagePart1, ImagePart2, ImageResult, 0.5, 0)

*差异:C(x,y) = A(x,y) - B(x,y)

sub_image (ImageConverted1, ImageConverted2, ImageSub, 1, 0)

*提取:C(x,y) = A(x,y) * B(x,y)

mult_image (GrayImage, GrayImage1, ImageResult, 0.005, 0)

*校正:C(x,y) = A(x,y) / B(x,y)

div_image (Scene00, ImageGrayRamp, ImageResult, 255, 0)
hom_mat2d_identity (HomMat2DIdentity)
*生成三阶单位阵
hom_mat2d_translate (HomMat2DIdentity, 64, 64, HomMat2DTranslate)
*平移矩阵 水平和垂直方向都平移64
affine_trans_image (Image, ImageAffinTrans, HomMat2DTranslate, 'constant', 'false')
*平移变换
hom_mat2d_scale (HomMat2DTranslate, 2, 2, 0, 0, HomMat2DScale)
*缩放
*hom_mat2d_rotate (HomMat2DIdentity, 0.78, 0, 0, HomMat2DRotate)
*旋转
affine_trans_image (Image, ImageAffinTrans, HomMat2DScale, 'constant', 'false')


图像变换 平移 尺度变换 旋转

gen_rectangle1 (Rectangle, 30, 20, 100, 200)
*生成一个矩形
hom_mat2d_identity (HomMat2DIdentity)
*三阶单位阵
hom_mat2d_translate (HomMat2DIdentity, 64, 64, HomMat2DTranslate)
*构造一个平移矩阵 改变第三列的前两个
hom_mat2d_scale (HomMat2DTranslate, 2, 2, 0, 0, HomMat2DScale)
*尺度变换
hom_mat2d_rotate (HomMat2DScale, 0.78, 0, 0, HomMat2DRotate)
*旋转

*affine_trans_image
*仿射变化 原图像与构造矩阵相乘 (对图像变换)
affine_trans_region (Rectangle, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')
*仿射变化 原图像与构造矩阵相乘 (对区域变换)--平移
affine_trans_region (Rectangle, RegionAffineTrans1, HomMat2DScale, 'nearest_neighbor')
*仿射变化 原图像与构造矩阵相乘 (对区域变换)--尺度变换
affine_trans_region (Rectangle, RegionAffineTrans2, HomMat2DRotate, 'nearest_neighbor')
*仿射变化 原图像与构造矩阵相乘 (对区域变换)--旋转

*数学形态学与Blob分析

*形态学基本运算

  • 腐蚀 膨胀 开、闭运算 击中击不中变换

*腐蚀 (A(输入图像) - B(结构元素)):边界向内部收缩的过程
结构元素完全落入输入图像中时,取结构元素的位置(圆心)
去除毛刺和尖角

  • 腐蚀相关算子 :erosion_circle ; erosion_rectangle ; erosion1 ; erosion2

*膨胀 (A(输入图像) + B(结构元素)):边界向外部扩张的过程,填充小孔、凹陷、向外的尖角
结构元素与输入图像有交集的时候,取结构元素的位置(圆心)
填充凹陷部分,磨平尖角

  • 腐蚀相关算子 :dilation_circle ; dilation_rectangle ; dilation1 ; dilation2
*腐蚀实例
dev_close_window()
*读取图像
read_image (Image, '腐蚀膨胀.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
rgb1_to_gray (Image, GrayImage)
*阈值分割
threshold(GrayImage, Region, 0, 100)
*计算联通区域
connection (Region, ConnectedRegions)
dev_set_color ('black')
*使用半径为1的圆形结构元素腐蚀得到区域
erosion_circle(ConnectedRegions, RegionErosion, 1)
*使用长宽均为1的矩形结构元素腐蚀得到的区域
erosion_rectangle1(ConnectedRegions, RegionErosion1, 1, 1)
gen_circle (Circle, 50, 50, 1)
*使用生成的圆形结构元素腐蚀得到区域
erosion1 (ConnectedRegions, Circle, RegionErosion2,1)                            
*使用生成的圆形结构元素腐蚀得到区域(可设置参考点位置)
 *参考点行坐标值为0,圆心行坐标值为50,参考点的行坐标值比圆心行坐标值小,执行erosion2算子后向上移动,移动距离为|0-50|=50。
*参考点列坐标值为0,圆心列坐标值为50,参考点的列坐标值比圆心列坐标值小,执行erosion2算子后向左移动,移动距离为|0-50|=50。
erosion2 (RegionErosion2, Circle, RegionErosion3, 0, 0, 1)
*膨胀实例
read_image (Image, '腐蚀膨胀.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
rgb1_to_gray (Image, GrayImage)
threshold(GrayImage, Region, 0, 100)
connection (Region, ConnectedRegions)
dev_set_color ('black')
*使用半径为1的圆形结构元素膨胀得到区域
dilation_circle(ConnectedRegions, RegionErosion, 3)
*使用长宽均为1的矩形结构元素膨胀得到区域
dilation_rectangle1(ConnectedRegions, RegionErosion1, 3, 3)

gen_circle (Circle, 50, 50, 3)
*使用生成的结构元素膨胀得到区域
dilation1 (ConnectedRegions, Circle, RegionErosion2,1)
*使用圆形结构元素膨胀得到区域,可设置参考点位置
dilation2 (RegionErosion2, Circle, RegionErosion3, 0, 0, 1)

*开运算和闭运算
*开运算 (图像轮廓变得光滑 断开狭窄的连接 消除毛刺)

  • 先腐蚀后膨胀
    *闭运算 (弥补沟壑 消除孔洞)
  • 先膨胀后腐蚀

*指纹图像滤波 先开后闭

开运算

read_image(Image,'fabrik')
sobel_amp(Image,Sobel,'sum_abs',3)
threshold(Sobel,Edges,10,255)
gen_rectangle2(StructElement,100,100,3.07819,20,1)
*生成结构元素
opening(Edges,StructElement,Direction)
*开运算
opening_circle (ConnectedRegions, RegionOpening, 3.5)
*用半径为3.5的圆对图像做开运算
opening_rectangle1 (RegionOpening, RegionOpening1, 10, 10)
*用10*10的矩形对图像做开运算

闭运算

gen_circle (Circle, 100, 100, 40)
gen_rectangle1 (Rectangle, 30, 20, 80, 200)
gen_rectangle1 (Rectangle1, 100, 100, 200, 105)
union2 (Rectangle, Rectangle1, RegionUnion)
closing (RegionUnion, Circle, RegionClosing)
*1.边界提取    原理:原图像-腐蚀之后的图像=边界
*  boundary算子
*2.孔洞填充    
*3.骨架 细化

dev_close_window ()
read_image (Image, 'D:/数字图像处理/image/7.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)

threshold (Image, Region, 0, 156)
boundary (Region, RegionBorder, 'inner')
*内边界
boundary (Region, RegionBorder1, 'outer')
*外边界
boundary (Region, RegionBorder2, 'inner_filled')

fill_up (Region, RegionFillUp)
*填充所有孔洞
fill_up_shape (Region, RegionFillUp1, 'area', 1, 11300)
*填充面积在1~11300的孔洞


skeleton (Region, Skeleton)
*骨架

*Blob分析

  • 二维目标图像
  • 高对比度图像
  • 场景简单图像
  • ball.hdev: Inspection of Ball Bonding

四部分
1.获取图像
2.图像预处理
3.图像分割
4.blob分析


*采集图像

dev_update_window ('off')
*更新窗口
dev_close_window ()
dev_open_window (0, 0, 728, 512, 'black', WindowID)
read_image (Bond, 'die/die_03')
dev_display (Bond)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
*设置字体
disp_continue_message (WindowID, 'black', 'true')
*显示信息
stop ()


*图像预处理

threshold (Bond, Bright, 100, 255)
shape_trans (Bright, Die, 'rectangle2')
*形状变换 选中区域变成矩形
dev_set_color ('green')
dev_set_line_width (3)
dev_set_draw ('margin')
dev_display (Die)
*画边界
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_set_draw ('fill')
reduce_domain (Bond, Die, DieGrey)
*减小一个图像的区域


*图像分割

threshold (DieGrey, Wires, 0, 50)
fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
*填充孔洞
dev_display (Bond)
dev_set_draw ('fill')
dev_set_color ('red')
dev_display (WiresFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()
opening_circle (WiresFilled, Balls, 15.5)
*用结构元素为半径为15.5的圆做开运算
dev_set_color ('green')
dev_display (Balls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
connection (Balls, SingleBalls)
*连通性分析
select_shape (SingleBalls, SelectedRegions, 'area', 'and', 984.63, 1006.85)
*特征直方图插入代码
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
*按形状选择


*Blob分析 计算和显示

sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')
*排序
dev_display (Bond)
dev_set_colored (12)
dev_display (FinalBalls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
smallest_circle (FinalBalls, Row, Column, Radius)
*计算圆的半径
NumBalls := |Radius|
*半径
Diameter := 2 * Radius
*直径
meanDiameter := sum(Diameter) / NumBalls
*均值
mimDiameter := min(Diameter)
*最小值
dev_display (Bond)
disp_circle (WindowID, Row, Column, Radius)
dev_set_color ('white')
for i := 1 to NumBalls by 1
    if (fmod(i,2) == 1)
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] - 2.7 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    else
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] + 1.2 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    endif
endfor
* dump_window (WindowID, 'tiff_rgb', './ball')
dev_set_color ('green')
dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_close_window ()

你可能感兴趣的:(HALCON,计算机视觉)