资源路径:F:\halcon\halconStudy
阈值分割
灰度值
读取图像
转灰度图
rgb1_to_gray(Image1,grayimage)
打开灰度直方图
使能输出
拖动阈值线条…
大于阈值的显示红色。。。
(红色就是所选择的区域)
threshold(Image1,Reasion1,100,177)
取某一个阈值下的某一个区域
先使用灰度直方图查看阈值
设置阈值
threshold(Image1,Reasion1,90,177)
分割区域(不连接的区域)
connection(Reasion1,ConnectionReasion)
按照面积和中心划分了很多区域、、
(我用花了。。)
特征直方图按照面积查看选择的区域(注意调下标)
插入代码
select_shape (ConnectionReasion, SelectedRegions, 'area', 'and', 2, 51896)
在按照位置进行筛选
select_shape (SelectedRegions, SelectedRegions2, 'row', 'and', 500, 1000)
select_shape (SelectedRegions, SelectedRegions1, 'row', 'and', 200, 600)
纵坐标是数量,横坐标是对应的参数取值范围。。
最后选择了一个噪点。。
获取中心点位置
area_center(SelectedRegions3,area,row,Colume)
read_image (Image20230321132729, 'C:/Users/12135/Desktop/联想截图_20230321132729.png')
rgb1_to_gray(Image20230321132729,grayimage)
threshold(grayimage,Reasion1,192,224)
connection(Reasion1,ConnectionReasion)
select_shape (ConnectionReasion, SelectedRegions, 'area', 'and', 2, 51896)
select_shape (SelectedRegions, SelectedRegions1, 'row', 'and', 217.89, 500)
select_shape (SelectedRegions1, SelectedRegions2, 'row', 'and', 269.27, 292.2)
select_shape (SelectedRegions2, SelectedRegions3, 'column', 'and', 815.596, 863.303)
area_center(SelectedRegions3,area,row,Colume)
形态学
膨胀
算子:
dilation_circle()
dilation_rectangle1()
threshold (grayImage, Regions, 173, 255)
使用膨胀算子
dilation_circle(Regions,RegionDilation,3.5)
read_image (Image20230322112606, 'F:/halcon/halconStudy/联想截图_20230322112606.png')
rgb1_to_gray(Image20230322112606,grayImage)
threshold (grayImage, Regions, 173, 255)
dilation_circle(Regions,RegionDilation,3.5)
腐蚀
算子:
erosion_circle()
erosion_rectangle1()
阈值过滤
threshold (GrayImage, Regions, 9, 255)
腐蚀
erosion_rectangle1 (Regions, RegionErosion, 11, 11)
在按照位置选择。。
效果如下:
connection (RegionErosion, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'column', 'and', 124.31, 203.21)
完整代码:
read_image (Image20230322114444, 'F:/halcon/halconStudy/联想截图_20230322114444.png')
rgb1_to_gray (Image20230322114444, GrayImage)
threshold (GrayImage, Regions, 9, 255)
erosion_rectangle1 (Regions, RegionErosion, 12, 12)
connection (RegionErosion, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'column', 'and', 124.31, 203.21)
开运算
先腐蚀,后膨胀
算子:
opening_circle()
opening_rectangle1()
二值化
rgb1_to_gray (Image20230322120855, GrayImage)
阈值选择
threshold (GrayImage, Regions, 12, 255)
开运算
opening_circle (Regions, RegionOpening, 10)
read_image (Image20230322120855, 'F:/halcon/halconStudy/联想截图_20230322120855.png')
rgb1_to_gray (Image20230322120855, GrayImage)
threshold (GrayImage, Regions, 12, 255)
opening_circle (Regions, RegionOpening, 10)
闭运算
先膨胀,后腐蚀
算子:
closing_circle()
closing_rectangle1()
完整代码:
read_image (Image20230322120855, 'F:/halcon/halconStudy/联想截图_20230322120855.png')
rgb1_to_gray (Image20230322120855, GrayImage)
threshold (GrayImage, Regions, 116, 255)
connection (Regions, ConnectedRegions)
closing_circle (Regions, RegionClosing, 3.5)
connection (RegionClosing, ConnectedRegions1)
闭运算前的:
闭运算后的:
综合使用
阈值选择
threshold (GrayImage, Regions, 100, 204)
划分区域
connection (Regions, ConnectedRegions)
去除小杂点(开运算)
opening_circle (Regions, RegionOpening, 3.5)
划分区域并查看
connection (Regions, ConnectedRegions)
opening_circle (Regions, RegionOpening, 3.5)
connection (RegionOpening, ConnectedRegions1)
将在一起的石头分开(腐蚀)
erosion_circle (RegionOpening, RegionErosion, 10)
在膨胀回来
dilation_circle (ConnectedRegions2, RegionDilation, 10)
把石头逐个的选择出来了
read_image (Image20230322122700, 'F:/halcon/halconStudy/联想截图_20230322122700.png')
rgb1_to_gray (Image20230322122700, GrayImage)
threshold (GrayImage, Regions, 100, 204)
connection (Regions, ConnectedRegions)
opening_circle (Regions, RegionOpening, 3.5)
connection (RegionOpening, ConnectedRegions1)
erosion_circle (RegionOpening, RegionErosion, 10)
connection (RegionErosion, ConnectedRegions2)
dilation_circle (ConnectedRegions2, RegionDilation, 10)
connection (RegionDilation, ConnectedRegions3)
开运算和检测轮廓
插入代码。。
需要去毛刺
(这里使用开运算)
opening_rectangle1(Regions,RegionOpening,11,11)
轮廓前后对比
read_image (Image20230321162033, 'F:/halcon/halconStudy/联想截图_20230321162033.png')
rgb1_to_gray(Image20230321162033,GrayImage)
threshold (GrayImage, Regions, 0, 235)
gen_contour_region_xld(Regions,Contours1,'border')
opening_rectangle1(Regions,RegionOpening,11,11)
gen_contour_region_xld(RegionOpening,Contours2,'border')
字符识别
资源图片路径:
C:\Users\Public\Documents\MVTec\HALCON-12.0\examples\images\ocr
提取点:
dots_image(LotNumber09,DotImage,5,'dark',0)
阈值选择
threshold (DotImage, Regions1, 10, 255)
拆分区域
connection(Regions1,ConnectionRegion)
指定选择字符的区域。(使用特征直方图)
插入代码:
select_shape (ConnectionRegion, SelectedRegions, ['area','row','column'], 'and', [0,232.57,177.06], [200.01,332.57,512.84])
联合区域
union1(SelectedRegions,UnionRegions)
膨胀(吧点连接为数字)
dilation_circle(UnionRegions,UnionDilation,3.5)
在拆分成不同的区域
connection(UnionDilation,ConnectionRegion2)
和膨胀之前的区域取交集
intersection(ConnectionRegion2,SelectedRegions,RegionIntersection)
排序
sort_region(RegionIntersection,SortedRegions,'upper_left','true','column')
使用神经网络分类器读取
read_ocr_class_mlp('DotPrint_0-9A-Z.omc',OCRHandle)
识别
do_ocr_multi_class_mlp(SortedRegions,LotNumber09,OCRHandle,Class,Confidence)
展示
disp_message (WindowHandle, Class, 'window', 12, 12, 'black', 'true')
dev_close_window()
dev_open_window(0,0,512,512,'black',WindowHandle)
read_image (LotNumber09, 'F:/halcon/halconStudy/lot_number_09.png')
dots_image(LotNumber09,DotImage,5,'dark',0)
threshold (DotImage, Regions1, 10, 255)
connection(Regions1,ConnectionRegion)
select_shape (ConnectionRegion, SelectedRegions, ['area','row','column'], 'and', [0,232.57,177.06], [200.01,332.57,512.84])
union1(SelectedRegions,UnionRegions)
dilation_circle(UnionRegions,UnionDilation,3.5)
connection(UnionDilation,ConnectionRegion2)
intersection(ConnectionRegion2,SelectedRegions,RegionIntersection)
sort_region(RegionIntersection,SortedRegions,'upper_left','true','column')
read_ocr_class_mlp('DotPrint_0-9A-Z.omc',OCRHandle)
do_ocr_multi_class_mlp(SortedRegions,LotNumber09,OCRHandle,Class,Confidence)
disp_message (WindowHandle, Class, 'window', 12, 12, 'black', 'true')