读取图片:
read_image (Image, 'C: /test.png')
Halcon的一些基本数据结构:
read_image(ImageBase,'D:/data/a.png')
*获取图像的大小,以建立合适尺寸的窗口
*get_image_size (ImageBase, Width, Height)
*创建新的显示窗口
*dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
*将图像转换为单通道灰度图像
rgb1_to_gray(ImageBase,GrayImage)
dev_display (GrayImage)
*使用阈值处理提取较暗部分
threshold (GrayImage, DarkArea, 0, 80)
*填充区域
fill_up (DarkArea, RegionFillUp)
*将不相连的区域整体分割成独立的区域
connection (RegionFillUp, ConnectedRegions)
*排除杂点,将面积较大的目标选择出来
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and',0, 1000)
平移:
read_image(ImageBase,'D:/data/a.png')
*创建一个空的仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
*向y方向平移30个像素,x方向平移20个像素
hom_mat2d_translate (HomMat2DIdentity, 30,20, HomMat2DTranslate)
affine_trans_image(ImageBase, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')
旋转:
read_image(ImageBase,'D:/data/a.png')
*创建一个空的仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
*以(300,100)为参考点旋转30度
hom_mat2d_rotate(HomMat2DIdentity,30,300, 100, HomMat2DRotate)
affine_trans_image(ImageBase, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
缩放:
read_image(ImageBase,'D:/data/a.png')
*创建一个空的仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
*以(250,150)为参考点放大两倍
hom_mat2d_scale(HomMat2DIdentity,2, 2,250,150, HomMat2DScale)
affine_trans_image(ImageBase, ImageAffineTrans, HomMat2DScale, 'constant', 'false')
read_image(ImageBase,'D:/data/b.png')
rgb1_to_gray(ImageBase, GrayImage)
*获取图像的尺寸
get_image_size(GrayImage, Width, Height)
*新建显示窗口,适应图像尺寸
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(GrayImage)
*初始化角点坐标
XCoordCorners:=[]
YCoordCorners:=[]
*阈值处理,提取较暗的区域
threshold(GrayImage,DarkRegion,0, 80)
*分离不相连的区域
connection (DarkRegion, ConnectedRegions)
*选择面积最大的暗色区域,即屏幕区域
select_shape_std (ConnectedRegions, displayRegion, 'max_area', 70)
*裁剪屏幕区域
reduce_domain (GrayImage, displayRegion, displayImage)
*创建边缘轮廓
gen_contour_region_xld (displayRegion, Contours, 'border')
*将轮廓分割为边
segment_contours_xld (Contours, ContoursSplit, 'lines', 5, 4, 2)
*获取边的数量
count_obj (ContoursSplit, Number)
*存储每条边的起点位置
for index:=1 to Number by 1
select_obj(ContoursSplit, ObjectCurrent, index)
*拟合每条边
fit_line_contour_xld(ObjectCurrent, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
endfor
read_image(ImageBase,'D:/data/b.png')
get_image_size(ImageBase, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(ImageBase)
*选择ROI,指定矩形顶点坐标(y1,x1,y2,x2)
gen_rectangle1(Rectangle, 30, 10, 320, 450)
*从原图中分割出ROI
reduce_domain(ImageBase, Rectangle, ImageReduced)
read_image(ImageBase,'D:/data/c.png')
rgb1_to_gray(ImageBase, GrayImage)
*直方图均衡化
equ_histo_image(GrayImage, ImageEquHisto)
*显示直方图
*原始直方图
gray_histo(GrayImage,GrayImage, AbsoluteHisto1, RelativeHisto1)
*均衡化后的直方图
gray_histo(ImageEquHisto,ImageEquHisto, AbsoluteHisto2, RelativeHisto2)
增强边缘对比度:
read_image(ImageBase,'D:/data/d.png')
emphasize(ImageBase, ImageEmphasize, 10, 10, 1.5)
dev_display(ImageEmphasize)
增强明暗对比度:
read_image(ImageBase,'D:/data/d.png')
scale_image_max(ImageBase, ImageScaleMax)
dev_display(ImageScaleMax)
read_image(ImageBase,'D:/data/e.png')
*使用canny
shock_filter(ImageBase, SharpenedImage, 0.5, 20, 'canny', 1)
*使用拉普拉斯
*shock_filter(ImageBase, SharpenedImage, 0.5, 20, 'laplace', 1)
dev_display(SharpenedImage)
read_image(ImageBase,'D:/data/f.png')
*均值滤波
*mean_image(ImageBase, ImageMean, 9, 9)
*中值滤波,circle表示邻域的形状,也可以选择square,卷积核的尺寸,这里选择3。continued表示边界的处理方式,因为边界处往往无法移动滤波“窗口”,因此需要对像素做一些补充。这里选择continued,表示延伸边界像素。也可以选择cyclic,表示循环延伸边界像素
*median_image(ImageBase, ImageMedian, 'circle', 3, 'continued')
*高斯滤波
gauss_filter(ImageBase, ImageGauss, 5)
dev_display(ImageGauss)
read_image(ImageBase,'D:/data/g.png')
*子将图像分离成红绿蓝3个通道,并将每个通道单独存成一幅图像。
decompose3(ImageBase, Image1, Image2, Image3)
mean_image(Image1, ImageMean1, 9, 9)
*对每个颜色通道的图像进行直方图均衡,使其色彩过渡更加平滑。
emphasize(ImageMean1,em1, 5, 5, 1.5)
illuminate(em1,Image1,20,20,0.55)
equ_histo_image(Image2, ImageEquHisto2)
read_image(ImageBase,'D:/data/h.png')
rgb1_to_gray(ImageBase, GrayImage)
*DarkArea为输出的区域,类型为Region
threshold(GrayImage,DarkArea,0, 128)
read_image(ImageBase,'D:/data/i.png')
rgb1_to_gray(ImageBase, GrayImage)
auto_threshold(GrayImage, Regions, 8)
二值阈值分割:
read_image(ImageBase,'D:/data/h.png')
rgb1_to_gray(ImageBase, GrayImage)
*max_separability,表示在直方图中对最大的可分性进行分割;也可以选择smooth_histo,表示平滑直方图
*dark,表示提取较暗的部分;也可以选择light,表示提取较亮的部分
binary_threshold(GrayImage, Region, 'max_separability', 'dark', UsedThreshold)
dyn_threshold算子利用局部像素灰度差进行分割:
read_image(ImageBase,'D:/data/h.png')
rgb1_to_gray(ImageBase, GrayImage)
*由于图像对比度比较低,因此对图像进行相乘,增强对比度
mult_image (GrayImage, GrayImage, ImageResult, 0.005, 0)
*使用平滑滤波器对原始图像进行适当平滑
mean_image (ImageResult, ImageMean, 50,50)
*动态阈值分割,提取字符区域
dyn_threshold (ImageResult, ImageMean, RegionDynThresh, 4, 'not_equal')
read_image(ImageBase,'D:/data/j.png')
rgb1_to_gray(ImageBase, GrayImage)
mean_image(GrayImage, ImageMean, 30, 30)
dyn_threshold(GrayImage, ImageMean, RegionDynThresh, 30, 'light')
read_image(ImageBase,'D:/data/k.png')
rgb1_to_gray(ImageBase, GrayImage)
*设置矩形,选择感兴趣区域
gen_rectangle1(Rectangle,90,83,492,702)
reduce_domain(GrayImage, Rectangle, ImageReduced)
*主要是调整20这个参数
var_threshold(ImageReduced,Rectangle, 15, 15, 0.2,20, 'dark')
read_image(ImageBase,'D:/data/l.png')
rgb1_to_gray(ImageBase, GrayImage)
char_threshold(GrayImage, GrayImage, Characters, 2, 95, Threshold)
read_image(ImageBase,'D:/data/m.png')
mean_image(ImageBase, ImageMean, 5, 5)
regiongrowing(ImageMean, Regions, 3, 3, 6, 100)
read_image(ImageBase,'D:/data/m.png')
median_image(ImageBase, ImageMedian, 'circle', 1, 'mirrored')
*寻找颜色相似的邻域
regiongrowing(ImageMedian, Regions, 3, 3, 6, 100)
*对图像进行粗略的区域分割,提取满足条件的各个独立区域
shape_trans(Regions, RegionTrans, 'convex')
connection(RegionTrans, ConnectedRegions)
*计算出初步提取的区域的中心点坐标
area_center(ConnectedRegions, Area, Row, Column)
read_image(ImageBase,'D:/data/000.png')
rgb1_to_gray(ImageBase, GrayImage)
gauss_filter(GrayImage, ImageGauss, 11)
watersheds(ImageGauss, Basins, Watersheds)
read_image(ImageBase,'D:/data/n.png')
*将图像分解成R、G、B3个通道
decompose3(ImageBase, Image1, Image2, Image3)
*将R、G、B3个通道的图像转化为HSV颜色空间
trans_from_rgb(Image1, Image1, Image1, ImageResult1, ImageResult2, ImageResult3, 'hsv')
*进行纹理检测,其中选择的滤波器类型是ls,即行方向用level,列方向用spot进行检测*这种滤波器类型适合检测垂直方向上的缺陷
texture_laws(ImageResult2, ImageTexture, 'ls', 2, 7)
*对经过滤波器处理后的图像进行均值化,使图像更平滑,缺陷区域更明显
mean_image(ImageTexture, ImageMean, 11, 11)
*对检测出的缺陷区域进行形态学处理并显示出来
threshold(ImageMean, Region,60, 255)
*将符合条件的区域分割成独立区域
connection(Region, ConnectedRegions)
例:木材截面图像的分割与计数
read_image(ImageBase,'D:/data/o.png')
get_image_size(ImageBase, Width, Height)
rgb1_to_gray(ImageBase, GrayImage)
*进行全局阈值分割,将亮色区域提取出来
threshold(GrayImage,Bright,60, 255)
*进行开运算处理,去除边缘毛刺
opening_rectangle1(Bright,Cut,1,7)
*将非连通区域分割成独立区域
connection(Cut, ConnectedRegions)
*选择截面比较小的木材的区域
select_shape(ConnectedRegions, smallRegions, 'area', 'and',500,20000)
*对粘连区域做腐蚀操作,根据腐蚀的情况和面积选出截面区域
count_obj(smallRegions,Number1)
*选择截面比较大的木材的区域,可能有粘连和区域重叠的情况
select_shape(ConnectedRegions,largeRegions, 'area', 'and', 20000,1000000)
erosion_circle(largeRegions, erisionedLargeRegions, 8.5)
*将非连通区域分割成独立区域
connection(erisionedLargeRegions, ConnectedRegions2)
*再次选择符合面积条件的区域,排除杂点
select_shape(ConnectedRegions2, SelectedRegions, 'area', 'and', 150, 99999)
area_center返回面积和中心点。(阈值分割提取物体并计数实例)
read_image(ImageBase,'D:/data/k.png')
get_image_size(ImageBase, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
rgb1_to_gray(ImageBase, GrayImage)
*创建矩形选区,选择感兴趣区域
gen_rectangle1(Rectangle, 90,83,492,702)
reduce_domain(GrayImage, Rectangle, ROI)
*阈值处理,提取其中的圆孔
threshold(ROI, Region,0,90)
*将提取的整个区域中不相连的部分分割成独立的区域
connection(Region, ConnectedRegions)
*获取不相连的区域的数量
count_obj(ConnectedRegions, Number)
*计算所有不相连区域的面积和中心点坐标
area_center(ConnectedRegions, Area, Row, Column)
area_holes(Region, Area)
select_shape算子能高效地根据特征提取出符合条件的区域
select_shape(Regions :SelectedRegions : Features, Operation, Min, Max : )
read_image(ImageBase,'D:/data/p.png')
dev_open_window_fit_image(ImageBase, 0, 0, -1, -1, WindowHandle)
*设置绘制的颜色
dev_set_color('white')
rgb1_to_gray(ImageBase, GrayImage)
*创建矩形选区,选择感兴趣区域
gen_rectangle1(Rectangle,80, 100, 300, 400)
reduce_domain(GrayImage, Rectangle,ROI)
gen_image_proto(ROI, ImageCleared, 0)
*进行阈值处理,提取出图中较暗的包含孔洞的区域
threshold(ROI, Region,50, 255)
*将不连通的部分独立分割出来,成为一组区域
connection(Region, ConnectedRegions)
*设置绘制的颜色,为了标记选择的区域
dev_set_color('magenta')
select_shape(ConnectedRegions, SelectedRegions, 'width', 'and',100,200)
read_image(ImageBase,'D:/data/p.png')
get_image_size(ImageBase, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
rgb1_to_gray(ImageBase, GrayImage)
gen_rectangle1(Rectangle,80, 100, 300, 400)
reduce_domain(GrayImage, Rectangle,ROI)
threshold(ROI, Region,50, 255)
connection(Region, ConnectedRegions)
select_shape_std(ConnectedRegions, SelectedRegions, 'max_area', 70)
*圆
inner_circle(SelectedRegions, Row, Column, Radius)
*矩形
smallest_rectangle1(SelectedRegions, Row1, Column1, Row2, Column2)
*最小外接矩形
smallest_rectangle2(SelectedRegions, Row1, Column1, Phi, Length1, Length2)
read_image(Image, 'D:/data/q.png')
threshold(Image, Region, 1, 255)
*提取区域中的最小最大灰度值
gray_features(Region, Image, 'min',MinDisp)
gray_features(Region, Image, 'max',MaxDisp)
read_image(Image, 'D:/data/q.png')
threshold(Image, Region, 1, 255)
*提取该区域中的最大和最小灰度值
min_max_gray(Region, Image,5, Min, Max, Range)
read_image(Image, 'D:/data/q.png')
threshold(Image, Region, 1, 255)
*提取该区域中灰度值的平均值和偏差
intensity(Region, Image, Mean, Deviation)
read_image(Image, 'D:/data/q.png')
threshold(Image, Region, 1, 255)
area_center_gray(Region, Image, Area, Row, Column)
read_image(Image, 'D:/data/m.png')
get_image_size(Image, Width, Height)
dev_open_window(0, 0, Width, Height, 'white', WindowHandle)
rgb1_to_gray(Image, GrayImage)
*使用均值滤波对灰度图像进行平滑处理,以去除细节杂点
median_image(GrayImage, ImageMedian, 'circle', 2, 'mirrored')
*进行阈值处理,提取出较暗部分
threshold(ImageMedian, Region,50,130)
*使用开运算将各区域分离
opening_circle(Region, RegionOpening, 3.5)
*将不连通的区域分隔开来
connection(RegionOpening, ConnectedRegions)
*将面积较大的区域提取出来
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and',10000, 99999)