先看代码实践
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 ()再看理论知识:
二值化算子与例程
Halcon中实现Otsu 算法
参考文章:
转载自:https://blog.csdn.net/libaineu2004/article/details/102826372
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:



少有人走的路


















