Halcon边缘检测和线条检测(2)-包含了形态学闭运算

简介: Halcon边缘检测和线条检测(2)-包含了形态学闭运算
*如果是畸变图,需要先校正
read_image (Image, 'D:/1.bmp')
get_image_size (Image, Width, Height)
*彩色转灰度图
count_channels (Image, Channels)
if (Channels == 3 or Channels == 4)
    rgb1_to_gray (Image, Image)
endif
*亚像素提取边缘,Alpha数值越大,轮廓越圆滑
edges_sub_pix (Image, Edges, 'lanser1', 1, 5, 10)
*合并邻近的XLD,使得细小线段拼接起来
union_adjacent_contours_xld (Edges, UnionContours, 5, 1, 'attr_keep')
*根据轮廓特征选择XLD
*这个算子用到的轮廓特征如下:contour-length轮廓长度,direction轮廓回归线方向,用参数min1,max1;
*curvature曲率,轮廓XLD到回归线的平均距离和标准差各有范围选择,平均距离使用参数min1,max1;
*标准差使用min2,max2,条件是在两参数的大小范围之内。
select_contours_xld (UnionContours, SelectedContours, 'contour_length', 20, 999999, -0.5, 0.5)
count_obj(SelectedContours, NumberContours)
for i := 1 to NumberContours by 1
    select_obj (SelectedContours, ObjectSelected, i)
    get_contour_xld (ObjectSelected, row, col)
endfor
dev_set_color ('green')
dev_display (Image)
dev_display (Edges)
dev_display (SelectedContours)
stop()
*形态学闭运算
R:=20
gen_image_const (ConstImage, 'byte', 2*R+1, 2*R+1)
get_image_size (ConstImage, MaskWidth, MaskHeight)
gen_circle (Circle, (MaskHeight - 1) / 2.0, (MaskWidth - 1) / 2.0, R)
reduce_domain (ConstImage, Circle, ImageReduced)
gray_closing (Image, ImageReduced, ImageClosing)
* gray_closing_shape(Image, ImageClosing, R, R, 'octagon')
*亚像素提取边缘,Alpha数值越大,轮廓越圆滑
edges_sub_pix (ImageClosing, Edges, 'lanser1', 1, 5, 10)
*合并邻近的XLD,使得细小线段拼接起来
union_adjacent_contours_xld (Edges, UnionContours, 5, 1, 'attr_keep')
*根据轮廓特征选择XLD
*这个算子用到的轮廓特征如下:contour-length轮廓长度,direction轮廓回归线方向,用参数min1,max1;
*curvature曲率,轮廓XLD到回归线的平均距离和标准差各有范围选择,平均距离使用参数min1,max1;
*标准差使用min2,max2,条件是在两参数的大小范围之内。
select_contours_xld (UnionContours, SelectedContours, 'contour_length', 100, 999999, -0.5, 0.5)
count_obj(SelectedContours, NumberContours)
maxArea := 0
index := 0
for i := 1 to NumberContours by 1
    select_obj (SelectedContours, ObjectSelected, i)
    area_center_xld (ObjectSelected, Area, Row1, Column1, PointOrder)
    if (Area > maxArea)
        maxArea := Area
        index := i
    endif
endfor
select_obj (SelectedContours, ObjectSelected, index)
get_contour_xld (ObjectSelected, Row2, Column2)
stop()
*无效set_system ('init_new_image', 'false')
gen_region_contour_xld (ObjectSelected, Region, 'filled')
gen_image_const (NewImage, 'byte', Width, Height)
*Create an image with a specified constant gray value
gen_image_proto (NewImage, ImageCleared1, 255)
*Paint regions into an image
paint_region (Region, ImageCleared1, ImageResult, 0, 'fill')
write_image (ImageResult, 'jpeg', 0, 'D:/1111.jpg')
*Overpaint regions in an image
gen_image_proto (NewImage, ImageCleared2, 255)
overpaint_region(ImageCleared2, Region, 0, 'fill')
*让黑色膨胀,让白色腐蚀
R:=3
gen_image_const (ConstImage, 'byte', 2*R+1, 2*R+1)
get_image_size (ConstImage, MaskWidth, MaskHeight)
gen_circle (Circle, (MaskHeight - 1) / 2.0, (MaskWidth - 1) / 2.0, R)
reduce_domain (ConstImage, Circle, ImageReduced)
gray_erosion (ImageResult, ImageReduced, ImageErosion)
edges_sub_pix (ImageErosion, EdgesErosion, 'lanser1', 1, 5, 10)
count_obj(EdgesErosion, NumberContours)
if (NumberContours == 1)
    get_contour_xld (EdgesErosion, Row3, Column3)
endif
stop()


形态学有关的算子:


erosion1

erosion_circle

gray_erosion

gray_dilation

gray_opening

gray_closing



--官方案例--


gray_opening.hdev


count_pellets.hdev





相关文章
|
7月前
|
存储 Cloud Native Linux
OpenCV 图像像素运算操作
OpenCV 图像像素运算操作
|
18天前
|
计算机视觉
OpenCV(二十七):图像距离变换
OpenCV(二十七):图像距离变换
26 0
|
18天前
|
计算机视觉
[Halcon&图像] 形态学处理(腐蚀、膨胀、开运算、闭运算)
[Halcon&图像] 形态学处理(腐蚀、膨胀、开运算、闭运算)
109 1
|
8月前
|
机器学习/深度学习 人工智能 算法
OpenCV-差分法实现绿叶识别(图像差分+颜色通道)
OpenCV-差分法实现绿叶识别(图像差分+颜色通道)
|
8月前
|
机器学习/深度学习 人工智能 文字识别
OpenCV-字典法实现数字识别(尺寸归一化+图像差值)
OpenCV-字典法实现数字识别(尺寸归一化+图像差值)
|
12月前
|
算法 API 计算机视觉
OpenCV(图像处理)-基于Python-图像的基本变换-平移-翻转-仿射变换-透视变换
OpenCV(图像处理)-基于Python-图像的基本变换-平移-翻转-仿射变换-透视变换
134 0
|
计算机视觉 Python
OpenCV_03 图像的算数操作:图像的加法+图像的混合
你可以使用OpenCV的cv.add()函数把两幅图像相加,或者可以简单地通过numpy操作添加两个图像,如res = img1 + img2。两个图像应该具有相同的大小和类型,或者第二个图像可以是标量值。
62 0
|
计算机视觉 索引
三天学会opencv(二)——矩阵的掩膜操作
三天学会opencv(二)——矩阵的掩膜操作
三天学会opencv(二)——矩阵的掩膜操作
|
测试技术 计算机视觉
将ros的图像 转换成 opencv的图像并求距离
将ros的图像 转换成 opencv的图像并求距离
将ros的图像  转换成 opencv的图像并求距离
|
计算机视觉
OpenCV 形态学操作应用——提取水平与垂直线
OpenCV 形态学操作应用——提取水平与垂直线
250 0
OpenCV 形态学操作应用——提取水平与垂直线

热门文章

最新文章