Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子

先看代码实践

dev_update_off ()
dev_close_window ()
 
*读图
read_image (Image, 'D:/1.bmp')
get_image_size (Image, Width, Height)
 
*测试提取边缘
edges_image(Image,Amp,Dir,'lanser2',0.5,'none',-1,-1)
hysteresis_threshold(Amp,Margin,20,30,30)
 
*彩色转灰度图
count_channels (Image, Channels)
if (Channels == 3)
    rgb1_to_gray (Image, GrayImage)
*真彩色转灰度图
elseif (Channels == 4)
    decompose4 (Image, ImageR, ImageG, ImageB, ImageA)
    compose3 (ImageR, ImageG, ImageB, MultiChannelImage)
    rgb1_to_gray (MultiChannelImage, GrayImage)
endif
 
*显示图
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
 
*自适应二值化
median_image (Image, Median, 'circle', 3, 'mirrored')
auto_threshold(Median, Regions, 2)
 
*动态二值化
D := 100
mean_image(Image, Mean, D*2+1, D*2+1)
dyn_threshold(Image, Mean, Seg, 5, 'light')
regiongrowing (Image, Regions, 1, 1, 6, 1)
gen_contour_region_xld (Regions, Contours, 'border')
 
*threshold_sub_pix只能取外轮廓
threshold_sub_pix(Image, Border, 128)
derivate_gauss(Image,Laplace,3,'laplace')
zero_crossing_sub_pix(Laplace,ZeroCrossings)
 
*测试soble
sobel_amp (Image, EdgeAmplitude1, 'thin_max_abs', 5)
sobel_amp (GrayImage, EdgeAmplitude2, 'thin_max_abs', 5)
 
*图像的“边缘”指的是:图像中灰度有明显跳变的地方。如果在图中画一条“有一定宽度的线”,那么线的两侧应该都可以提取到边缘。
*而线条提取的算子(例如lines_gauss)提取的是这条“有一定宽度的线”的“骨架线”,它一般只有一根。
*提取骨架线条
MaxLineWidth := 10
Contrast := 20
calculate_lines_gauss_parameters (MaxLineWidth, Contrast, Sigma, Low, High)
lines_gauss (GrayImage, Lines, Sigma, Low, High, 'dark', 'true', 'parabolic', 'true')
count_obj (Lines, Number)
* lines_gauss (GrayImage, Lines, 2.3, 0.0, 0.7, 'dark', 'true', 'parabolic', 'true')
*亚像素提取边缘
*Alpha数值越大,轮廓越圆滑
edges_sub_pix (GrayImage, Edges1, 'canny', 1, 3, 5)
*edges_sub_pix (GrayImage, Edges2, 'canny_junctions', 1, 5, 10)
*edges_sub_pix (GrayImage, Edges3, 'lanser1', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges4, 'lanser2', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges5, 'deriche1', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges6, 'deriche2', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges7, 'shen', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges8, 'mshen', 0.5, 20, 40)
*edges_sub_pix (GrayImage, Edges9, 'sobel', 0.5, 20, 40)
 
*合并邻近的XLD,使得细小线段拼接起来
*select_contours_xld (Edges1, SelectedContours, 'contour_length', 5, 99999, -0.5, 0.5)
union_adjacent_contours_xld (Edges1, UnionContours, 5, 1, 'attr_keep')
*根据轮廓特征选择XLD
*这个算子用到的轮廓特征如下:contour-length轮廓长度,direction轮廓回归线方向,用参数min1,max1;
*curvature曲率,轮廓XLD到回归线的平均距离和标准差各有范围选择,平均距离使用参数min1,max1;
*标准差使用min2,max2,条件是在两参数的大小范围之内。
select_contours_xld (UnionContours, SelectedContours, 'contour_length', 10, 99999, -0.5, 0.5)
*close_contours_xld(SelectedContours, ClosedContours)
count_obj(SelectedContours, NumberContours)
stop()
for i := 1 to NumberContours by 1
    select_obj (SelectedContours, ObjectSelected, i)
    length_xld (ObjectSelected, Length)
    *计算xld的面积以及中心位置
    area_center_xld(ObjectSelected, area, row, column, PointOrder)
    get_contour_xld (ObjectSelected, row, col)
endfor
 
dev_set_color ('green')
dev_display (Image)
dev_display (Lines)
dev_display (Edges1)
stop()
 
*测试彩色
edges_color_sub_pix (Image, EdgesColor, 'canny', 1, 5, 10)
dev_display (Image)
dev_set_color ('blue')
dev_display (EdgesColor)
 
*测试HSV
decompose3(Image, r, g, b)
trans_from_rgb(b, g, r, h, s, v, 'hsv')
Sigma := 4
auto_threshold (h, Region1, Sigma)
auto_threshold (s, Region2, Sigma)
auto_threshold (v, Region3, Sigma)
edges_sub_pix (r, Edge1, 'canny', 1, 2, 5)
edges_sub_pix (g, Edge2, 'canny', 1, 2, 5)
edges_sub_pix (b, Edge3, 'canny', 1, 2, 5)
edges_sub_pix (h, Edge4, 'canny', 1, 2, 5)
edges_sub_pix (s, Edge5, 'canny', 1, 2, 5)
edges_sub_pix (v, Edge6, 'canny', 1, 2, 5)
stop ()

再看理论知识:

一、边缘提取

1、设置ROI兴趣区域

2、快速二值化,并连接相邻区域。

这样做的目的是进一步减少目标区域,通过二值化将目标区域大概轮廓提取出来

3、提取最接近目标区域的轮廓

常用函数有boundary,gen_contour_region_xld

4、根据自己的需求提取需要的初步轮廓

5、将初步提取的初步轮廓进行膨胀操作

6、将膨胀后的区域和原图进行减操作(在这步之前有可能需要对原图进行高斯滤波)。这样就能得到只有边缘的真实图像

7、用canny或其他算子(根据需要)提取亚像素轮廓,一般使用edges_sub_pix函数

8、处理和计算

得到真实的边缘XLD后你可能需要进一步处理得到你想要的线、弧等。

你可能用到的函数segment_contours_xld(分割)  union_collinear_contours_xld(联合相邻或相同角度直线)select_contours_xld(提取想要的轮廓) union_cocircular_contours_xld(联合相同圆)等等

得到轮廓后如果你不知道怎么处理后得到你想要的东西(线、弧、圆、角、矩形)你都可以将轮廓转化为点,然后用点集合来拟合任何你想要的东西。

 

二、BLOB分析检测(前面一篇有详细讲解,本骗只讲思路)

(1)应用ROI,可以使Blob分析加速。

(2)匹配ROI区域或图像,详将GUIDEIIB以形状为基础的匹配。

(3)校正图像<经常用来去除镜头畸变或把图像转换到参考点视角,如双目视觉时的图像校正>

(4)图像前处理

(5)引用分割参数

(6)分割图像

(7)区域处理

(8)特征提取

(9)把提取的结果转换到世界坐标中

(10)结果可视化。

    相机的标定和矫正不在本篇的学习之中。直接讲提取BLOB

1、一般先使用均值滤波去噪

2、利用去噪图像与平滑图像的OFFSET提取区域边缘,常见函数dyn_threshold 

3、提取连通域dyn_threshold 

4、根据形状或是灰度等特征来提取你想要的blob。

另一种方法就是分水岭算法

watersheds (ImageGauss, Basins, Watersheds)/

1、对图像进行高斯滤波
还有的图形更简单直接二值化就可以啦
bin_threshold (Fin, Dark) //分割图像,输出Dark区域,Fin已经被处理为区域//

difference (Fin, Dark, Background) //计算Fin与Dark两个区域的补集//

还有个函数应该说是让你高兴还是沮丧呢,以为一个函数就可以直接提取你想要的,但是参数很难调整

lines_gauss(Image, Lines, 6, 0.3, 0.5, 'light', 'true', 'gaussian', 'true')

 

三、赃物检测

1、得到两个不同高斯标准差的高斯积卷

2、对原始图像进行傅里叶变换'to_fre'

3、用之前的积卷对图像做积卷滤波

4、傅里叶反变换‘from_fre’
 

 

二值化算子与例程

auto_threshold

dyn_threshold

var_threshold

binary_threshold

fast_threshold

fast_threshold_vs_threshold

histo_to_thresh

local_threshold

threshold

threshold_sub_pix

C:\Users\Public\Documents\MVTec\HALCON-19.11-Progress\examples\hdevelop\Segmentation\Threshold

Halcon中实现Otsu 算法

https://blog.csdn.net/IntegralforLove/article/details/100102105

Halcon算子学习:图像阈值分割-threshold、binary_threshold、dyn_threshold算子

https://blog.csdn.net/Vichael_Chan/article/details/102576060

1.threshold-全局固定阈值分割
2.Binary Threshold-自动全局阈值分割
3.dyn_threshold-局部动态阈值分割
4.var_threshold算子-均值和标准偏差局部阈值分割
5.dual_threshold-双重阈值分割(有符号图像的阈值算子)
6.auto_threshold-自动全局阈值分割
7.fast_threshold-快速全局阈值分割
8.watersheds-分水岭算法分割

 

---

参考文章:

提取线条的lines_color、lines_facet、 lines_gauss算子

halcon之共线连接union_collinear_contours_xld

用halcon提取衣服徽章

官方自带的例子:

measure_grid.hdev

lines_gauss.hdev

edge_segments.hdev

rim_simple.hdev

sort_contours_xld.hdev

 

转载自:https://blog.csdn.net/libaineu2004/article/details/102826372


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864