tnblog
首页
视频
资源
登录

Halcon的Blob分析

1864人阅读 2024/5/7 11:25 总访问:3467454 评论:0 收藏:0 手机
分类: Halcon

Halcon的Blob分析

什么是Blob分析


图像处理(image processing),用计算机对图像进行分析,以达到所需结果的技术。又称影像处理。图像处理一般指数字图像处理。数字图像是指用工业相机、摄像机、扫描仪等设备经过拍摄得到的一个大的二维数组,该数组的元素称为像素,其值称为灰度值。图像处理技术一般包括图像压缩,增强和复原,匹配、描述和识别3个部分。

Blob,即binary large object,二进制大型对象,是事务处理过程中遇到的一些大型的、复杂的数据项,必须作为一个完整的数据项看待。如一幅图形、一帧图像、一段语言等。在计算机视觉中的Blob是指图像中的具有相似颜色、纹理等特征所组成的一块连通区域。

Blob分析(Blob Analysis)是对图像中相同像素的连通域进行分析(该连通域称为Blob)。其过程其实就是将图像进行二值化,分割得到前景和背景,然后进行连通区域检测,从而得到Blob块的过程。简单来说,blob分析就是在一块“光滑”区域内,将出现“灰度突变”的小区域寻找出来。

Blob分析工具可以从背景中分离出目标,并可以计算出目标的数量、位置、形状、方向和大小,还可以提供相关斑点间的拓扑结构。在处理过程中不是对单个像素逐一分析,而是对图像的行进行操作。图像的每一行都用游程长度编码(RLE)来表示相邻的目标范围。这种算法与基于像素的算法相比,大大提高了处理的速度。

适用范围:针对二维目标图像和高对比度图像,适用于有无检测和缺陷检测。常用于二维目标图像、高对比度图像、存在/缺陷检测、数值范围和旋转不变性需求。显然,纺织品的瑕疵检测,玻璃的瑕疵检测,机械零件表面缺陷检测,可乐瓶缺陷检测,药品胶囊缺陷检测等很多场合都会用到blob分析。

Blob分析流程及常用算子

基本步骤


Blob分析的基本步骤,这是一种理想状态,也是最基本的套路,获取图像->分割图像(区分前景像素和背景像素)->特征提取(比如面积、重心、旋转角度等)。
halcon代码实现如下:

  1. read_image(Image,'particle')//读取图像
  2. threshold(Image, BrightPixels,120,255)//阈值分割算子 二值化
  3. connection(BrightPixels,Particles)//断开联通区域
  4. area_center(Particles,Area,Row,Column)//获取这个区域的中心和面积


实际上,提取Blob之前和分析Blob之后也存在重要的步骤。比如,提取Blob之前一般要对图像进行预处理,比如图像的变换和校正、平滑与去噪、以及增强处理;分析Blob之后需要将Blob进行选取,或者将Blob重心的像素值向物理坐标系坐标值的转化。
因此,Blob实现方法需要具体情况具体分析。就拿阈值分割提取Blob而言,是用固定阈值还是动态阈值,这都是根据图片情况进行具体分析的。

完整的Blob分析步骤


在实际应用中,Blob 的分割会很复杂,需要处理更多步骤。 其原因有多种,比如杂乱或不均匀的照明、图像中有很多杂斑(很难提取目标位)。 此外,对Blob进行后处理以获取客户需要的直观显示数据,例如将特征转换为真实世界单位或结果可视化。


整个过程为:获取图像->应用ROI->定位ROI->矫正图像->图像预处理->动态获取分割参数->分割图像->处理区域->特征提取->将像素坐标转换到世界坐标->结果显示或者输出。


首先加载图片。

  1. ** 关闭窗口更新,即在显示图像后不立即更新窗口,提高性能。
  2. dev_update_window ('off')
  3. ** 关闭当前打开的窗口,清除窗口相关资源。
  4. dev_close_window ()
  5. ** 打开一个新窗口,大小为728x512,背景颜色为黑色,WindowID是窗口的标识符。
  6. dev_open_window (0, 0, 728, 512, 'black', WindowID)
  7. ** 从文件 'die/die_03' 中读取图像数据到变量 Bond 中。读取的就是我上传的图片
  8. read_image (Bond, 'die/die_03')
  9. ** 在当前打开的窗口中显示图像 Bond
  10. dev_display (Bond)
  11. ** 设置窗口中显示文本的字体属性,大小为14,字体为单色(单色意味着文本颜色不受当前显示图像的影响)。
  12. set_display_font (WindowID, 14, 'mono', 'true', 'false')
  13. ** 在窗口中显示持续性消息,文本颜色为黑色。
  14. disp_continue_message (WindowID, 'black', 'true')
  15. ** 停止当前程序的执行。
  16. stop ()

  1. *灰度处理 二值化
  2. threshold (Bond, Bright, 100, 255)
  3. *形态学处理,一般用于定位,形状转换,rectangle2带方向的矩形
  4. shape_trans (Bright, Die, 'rectangle2')
  5. ** 设置绘图颜色为绿色
  6. dev_set_color ('green')
  7. ** 设置绘图线条宽度为 3 个像素
  8. dev_set_line_width (3)
  9. ** 设置绘图时为图形边缘着色,这意味着绘制的矩形将在边缘处着色
  10. dev_set_draw ('margin')
  11. ** 在当前窗口中显示处理后的图像Die
  12. dev_display (Die)
  13. ** 在窗口中显示持续性消息,文本颜色为黑色
  14. disp_continue_message (WindowID, 'black', 'true')
  15. stop ()

  1. *区域锁定 将图像 Bond 限制在矩形区域 Die 内,并将结果存储在 DieGrey 中。
  2. reduce_domain (Bond, Die, DieGrey)
  3. *再次二值化 对图像 DieGrey 进行二值化处理,将灰度值在 0 50 之间的像素设为白色,其他设为黑色,结果存储在 Wires 中。
  4. threshold (DieGrey, Wires, 0, 50)
  5. *用给定的形状特征填充区域:面积为1~100的区域
  6. fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
  7. ** 在当前窗口中显示图像 Bond
  8. dev_display (Bond)
  9. ** 设置绘图模式为填充模式。
  10. dev_set_draw ('fill')
  11. ** 设置绘图颜色为红色。
  12. dev_set_color ('red')
  13. ** 在当前窗口中显示填充后的图像 WiresFilled
  14. dev_display (WiresFilled)
  15. ** 在窗口中显示持续性消息,文本颜色为黑色。
  16. disp_continue_message (WindowID, 'black', 'true')
  17. stop ()

  1. *形态学处理:此处为开运算/(也可以用腐蚀),减少像素,circle–对圆形作用最大
  2. *形态学 腐蚀
  3. * erosion_circle (WiresFilled, RegionErosion, 15.5)
  4. *形态学 膨胀
  5. * dilation_circle (RegionErosion, RegionDilation, 15.5)
  6. *形态学 开运算(效果同上 先腐蚀后膨胀 先变小再变大)
  7. opening_circle (WiresFilled, Balls, 15.5)
  8. dev_set_color ('green')
  9. dev_display (Balls)
  10. disp_continue_message (WindowID, 'black', 'true')
  11. stop ()


我们可以看到矩形的面积为2418,而圆形的面积只在1000多点左右,所以我们可以通过的它的大小范围来进行提取特征值。
这里设置了800-1200的范围。

  1. *将二值化分开的区域划分为不同的连通区域,断成不同的区域,方便后面作特征值提取处理
  2. connection (Balls, SingleBalls)
  3. *特征值提取
  4. * select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
  5. ** 根据形状的面积选择区域,将面积在 800 1200 之间的形状选出,结果存储在 IntermediateBalls 中。
  6. select_shape (SingleBalls, IntermediateBalls, 'area', 'and', 800, 1200)
  7. ** IntermediateBalls 中的形状按照其第一个点的位置进行排序,排序方式为按照列顺序,结果存储在 FinalBalls 中。
  8. sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')
  9. ** 在当前窗口中显示图像 Bond
  10. dev_display (Bond)
  11. ** 设置彩色显示,可能是指定一种颜色的显示。
  12. dev_set_colored (12)
  13. ** 在当前窗口中显示 FinalBalls
  14. dev_display (FinalBalls)
  15. ** 在窗口中显示持续性消息,文本颜色为黑色。
  16. disp_continue_message (WindowID, 'black', 'true')
  17. stop ()

  1. ** 计算 FinalBalls 中每个形状的最小外接圆,结果的圆心坐标存储在 (Row, Column) 中,半径存储在 Radius 中。
  2. smallest_circle (FinalBalls, Row, Column, Radius)
  3. ** 计算最小外接圆的数量,即形状的数量。
  4. NumBalls := |Radius|
  5. ** 计算最小外接圆的直径。
  6. Diameter := 2 * Radius
  7. ** 计算最小外接圆直径的平均值。
  8. meanDiameter := mean(Diameter)
  9. ** 计算最小外接圆直径的最小值。
  10. minDiameter := min(Diameter)
  11. ** 在当前窗口中显示图像 Bond
  12. dev_display (Bond)
  13. ** 在窗口中显示最小外接圆。
  14. disp_circle (WindowID, Row, Column, Radius)
  15. ** 设置绘图颜色为白色。
  16. dev_set_color ('white')
  17. ** 在窗口中显示形状直径信息。
  18. disp_message (WindowID, 'D: ' + Diameter$'.4', 'image', Row - 2 * Radius, Column, 'white', 'false')
  19. ** 恢复窗口的更新功能,即重新启用窗口的更新。
  20. dev_update_window ('on')

常见特征提取


1)、区域特征:
a:面积area;
b:力矩Moments;
c:平行于主轴的最小矩形smallest_rectangle1;
d:任意方向的最小矩形smallest_rectangle2;
e:最小圆形smallest_circle;
f:凸包面积convexity;
g:contlength区域边界长度;
h:圆形roundness;
j:圆度circularity;
k:紧密度compactness;
l:矩形度rectangularity;

2)、灰度特征
a:简单灰度值特征:区域的平均灰度值;
b:区域的最小和最大灰度值;

常用算子总结


1)、图像预处理常用算子:
a:mean_image:均值滤波
b:gauss_image:高斯滤波
c:median_image:中值滤波

2)、动态获取分割参数常用算子:
a:gray_histo_abs:灰度直方图
b:histo_to_thresh:直方图二值化

开运算和闭运算(形态学)


图像分割时,会用到开运算和闭运算,采用不同的分割策略,效果会不一样,这里仅先介绍下概念。
1)、开运算 先腐蚀后膨胀;
2)、闭运算 先膨胀后腐蚀;
3)、腐蚀;
4)、膨胀。

开运算和闭运算要用到的算子如下:
1)、开运算 opening(ConnectedRegions, ConnectedRegions, RegionOpening1)
2)、闭运算 closing(RegionOpening1, RegionOpening1, RegionClosing1)
3)、腐蚀 erosion1(RegionClosing1, RegionClosing1, RegionErosion1, 1)
4)、膨胀 dilation1(RegionErosion1, RegionErosion1, RegionDilation1, 1)
5)、圆形结构开运算 opening_circle(ConnectedRegions, RegionOpening, 3.5)
6)、圆形结构闭运算 closing_circle(RegionOpening, RegionClosing, 3.5)
7)、圆形结构腐蚀 erosion_circle(RegionClosing, RegionErosion, 3.5)
8)、圆形结构膨胀 dilation_circle(RegionErosion, RegionDilation, 3.5)

Blob分析案例说明

豆子分割统计(形态学案例)


加载我们的图像。

  1. * 采集图像操作
  2. ** 关闭图像更新,提高处理效率。
  3. dev_update_off ()
  4. ** 读取图像 'pellets'
  5. read_image (Image, 'pellets')
  6. ** 关闭当前窗口。
  7. dev_close_window ()
  8. ** 获取图像尺寸。
  9. get_image_size (Image, Width, Height)
  10. ** 打开窗口,设置窗口大小和背景颜色。
  11. dev_open_window (0, 0, Width, Height, 'black', WindowID)
  12. ** 设置显示区域。
  13. dev_set_part (0, 0, Height - 1, Width - 1)
  14. ** 设置显示字体。
  15. set_display_font (WindowID, 16, 'mono', 'true', 'false')
  16. ** 设置彩色显示。
  17. dev_set_colored (6)
  18. ** 设置绘图模式为边缘绘制。
  19. dev_set_draw ('margin')
  20. ** 设置绘图线条宽度。
  21. dev_set_line_width (3)
  22. ** 在窗口中显示图像。
  23. dev_display (Image)
  24. ** 在窗口中显示消息。
  25. disp_message (WindowID, 'Detect each single pellet', 'window', 12, 12, 'black', 'true')
  26. ** 在窗口中显示持续性消息。
  27. disp_continue_message (WindowID, 'black', 'true')
  28. ** 停止程序执行。
  29. stop ()

  1. * 从背景分割颗粒的区域
  2. ** 对图像进行二值化。
  3. binary_threshold (Image, LightRegion, 'max_separability', 'light', UsedThreshold)
  4. ** 对二值化图像进行开运算。
  5. opening_circle (LightRegion, Region, 3.5)
  6. ** 在窗口中显示分割结果。
  7. dev_display (Region)
  8. ** 在窗口中显示消息。
  9. disp_message (WindowID, 'First, segment the pellets', 'window', 12, 12, 'black', 'true')
  10. ** 在窗口中显示持续性消息。
  11. disp_continue_message (WindowID, 'black', 'true')
  12. ** 停止程序执行。
  13. stop ()

  1. * 第一次分割出颗粒区域,只是分割处理而已不能进行统计
  2. ** 进行连通性分析。
  3. connection (Region, ConnectedRegionsWrong)
  4. ** 在窗口中显示图像和连通区域。
  5. dev_display (Image)
  6. dev_display (ConnectedRegionsWrong)
  7. ** 在窗口中显示消息。
  8. disp_message (WindowID, 'Simple connection fails', 'window', 12, 12, 'black', 'true')
  9. ** 在窗口中显示持续性消息。
  10. disp_continue_message (WindowID, 'black', 'true')
  11. ** 停止程序执行。
  12. stop ()


我们可以发现进行开运算后(先腐蚀再扩大),再进行添加颜色,发现有很多是关联在一起的。
如果不希望关联在一起,我们可以先腐蚀再添加颜色,最后再扩大。

  1. * 通过腐蚀的方式来分割出每个区域
  2. ** 对图像进行腐蚀操作。
  3. erosion_circle (Region, RegionErosion, 7.5)
  4. ** 在窗口中显示图像和腐蚀结果。
  5. dev_display (Image)
  6. dev_display (RegionErosion)
  7. ** 在窗口中显示消息。
  8. disp_message (WindowID, 'Erosion of the pellet regions', 'window', 12, 12, 'black', 'true')
  9. ** 在窗口中显示持续性消息。
  10. disp_continue_message (WindowID, 'black', 'true')
  11. ** 停止程序执行。
  12. stop ()

  1. * 在次分割连通区域
  2. ** 进行连通性分析。
  3. connection (RegionErosion, ConnectedRegions)
  4. ** 在窗口中显示图像和连通区域。
  5. dev_display (Image)
  6. dev_display (ConnectedRegions)
  7. ** 在窗口中显示消息。
  8. disp_message (WindowID, 'Perform connection now', 'window', 12, 12, 'black', 'true')
  9. ** 在窗口中显示持续性消息。
  10. disp_continue_message (WindowID, 'black', 'true')
  11. ** 停止程序执行。
  12. stop ()

  1. * 通过应用膨胀使其恢复到原始颗粒大小
  2. ** 对图像进行膨胀操作。
  3. dilation_circle (ConnectedRegions, RegionDilation, 7.5)
  4. * 统计颗粒数量
  5. ** 统计连通区域数量。
  6. count_obj (RegionDilation, Number)
  7. ** 在窗口中显示图像和膨胀结果。
  8. dev_display (Image)
  9. dev_display (RegionDilation)
  10. ** 在窗口中显示消息和颗粒数量。
  11. disp_message (WindowID, Number + ' pellets detected', 'window', 12, 12, 'black', 'true')

木材统计(形态学)

  1. *读取图像
  2. read_image(image,'D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/wood.jpg')
  3. *图像转灰度
  4. rgb1_to_gray(image,grayimage)
  5. *阈值分割
  6. threshold (grayimage, regions, 60, 255)
  7. *开运算
  8. opening_rectangle1(regions,Cut,2,7)
  9. *打断非连通区域
  10. connection(Cut,connectedregions)
  11. *面积筛选
  12. select_shape (connectedregions, SelectedRegions, 'area', 'and', 391.24, 20056.3)
  13. *数出第一个部分木材个数
  14. count_obj(SelectedRegions,Number1)
  15. *面积筛选
  16. select_shape (connectedregions, SelectedRegions1, 'area', 'and', 24334.9, 100000)
  17. *腐蚀运算
  18. erosion_circle(SelectedRegions1,regionerosion1,7.5)
  19. *打断非连通区域
  20. connection(regionerosion1,connectedregions1)
  21. *面积筛选
  22. select_shape (connectedregions1, SelectedRegions2, 'area', 'and', 2707.36, 20000)
  23. *数出第二个部分木材个数
  24. count_obj(SelectedRegions2,Number2)
  25. *将第一个部分和第二个部分合并
  26. concat_obj(SelectedRegions,SelectedRegions2,objectsconcat)
  27. *数出合并部分总木材个数
  28. count_obj(objectsconcat,Number3)


看到Number3给出了木头的总数。

车牌的字符分割(数字与字母)


首先读取图片

  1. ***** 读取图片
  2. ** 关闭当前窗口。
  3. dev_close_window ()
  4. ** 读取图像 '素材/chepai18.jpg'
  5. read_image (Image, 'D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/chepai18.jpg')
  6. ** 打开窗口并适应图像大小。
  7. dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
  8. ** 在窗口中显示图像。
  9. dev_display (Image)


然后我们对图片进行二值化操作,并对灰度图进行阈值化处理

  1. ***** 处理图片-定位车牌
  2. ** 将图像转换为灰度图像。
  3. rgb1_to_gray (Image, GrayImage)
  4. ** 对灰度图像进行阈值化。
  5. threshold (GrayImage, Region, 80, 100)
  6. ** 对图像进行连通性分析。
  7. connection (Region, ConnectedRegions)
  8. ** 根据区域面积选择形状。
  9. select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 8870.97, 10000)

  1. ***** 处理图片- 转正图片
  2. ** 对选定的区域进行形状转换。
  3. shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
  4. ** 计算区域的中心和方向。
  5. area_center (RegionTrans, Area, Row, Column)
  6. orientation_region (RegionTrans, Phi)
  7. ** 将区域校正为水平状态。
  8. vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(0), HomMat2D)
  9. hom_mat2d_slant (HomMat2D, rad(15), 'x', Column, Row, HomMat2DSlant)
  10. affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
  11. affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
  12. reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)

  1. *************** 开始识别之车牌图片预处理
  2. ** 将图像转换为灰度图像。
  3. rgb1_to_gray (ImageReduced, GrayImage1)
  4. ** 对灰度图像进行阈值化。
  5. threshold (GrayImage1, Region1, 128, 255)
  6. ** 对区域进行腐蚀操作。
  7. erosion_rectangle1 (Region1, RegionErosion, 3, 3)
  8. ** 对区域进行开运算。
  9. opening_rectangle1 (RegionErosion, RegionOpening, 1, 3)
  10. ** 对图像进行连通性分析。
  11. connection (RegionOpening, ConnectedRegions1)
  12. ** 根据区域面积选择形状。
  13. select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 200, 500)
  14. ** 根据字符在车牌中的位置进行排序。
  15. sort_region (SelectedRegions1, SortedRegions, 'character', 'true', 'column')


select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
(将连接的区域进行筛选,筛选的特征有很多,如面积长度等,可以去掉不满足条件的轮廓)
描述:
操作符select_shape根据形状选择区域。对于来自区域的每个输入区域,计算所指示的特征(features)。如果计算特征的每个(Operation=‘and’)或至少一个(Operation=‘or’)在默认限制(最小值、最大值)内,则该区域将适应输出(复制)。
条件:Min_i <= Feature_i(Object) <= Max_i

功能特征的取值

特征 备注
area Area of the object 对象的面积
row Row index of the center 中心点的行坐标
column Column index of the center 中心点的列坐标
width Width of the region 区域的宽度
height Height of the region 区域的高度
row1 Row index of upper left corner 左上角行坐标
column1 Column index of upper left corner 左上角列坐标
row2 Row index of lower right corner 右下角行坐标
column2 Column index of lower right corner 右下角列坐标
circularity Circularity 圆度 0~1
compactness Compactness 紧密度 0~1
contlength Total length of contour 轮廓线总长
convexity Convexity 凸性
rectangularity Rectangularity 矩形度 0~1
ra Main radius of the equivalent ellipse 等效椭圆长轴半径长度
rb Secondary radius of the equivalent ellipse 等效椭圆短轴半径长度
phi Orientation of the equivalent ellipse 等效椭圆方向
anisometry Anisometry 椭圆参数,Ra/Rb长轴与短轴的比值
bulkiness Bulkiness 椭圆参数,蓬松度πRaRb/A
struct_factor Structur Factor 椭圆参数,Anisometry*Bulkiness-1
outer_radius Radius of smallest surrounding circle 最小外接圆半径
inner_radius Radius of largest inner circle 最大内接圆半径
inner_width Width of the largest axis-parallel rectangle that fits into the region 最大内接矩形宽度
inner_height Height of the largest axis-parallel rectangle that fits into the region 最大内接矩形高度
dist_mean Mean distance from the region border to the center 区域边界到中心的平均距离
dist_deviation Deviation of the distance from the region border from the center 区域边界到中心距离的偏差
roundness Roundness 圆度,与circularity计算方法不同
num_sides Number of polygon sides 多边形边数
connect_num Number of connection components 连通数
holes_num Number of holes 区域内洞数
area_holes Area of the holes of the object 所有洞的面积
max_diameter Maximum diameter of the region 最大直径
orientation Orientation of the region 区域方向
euler_number Euler number 欧拉数,即连通数和洞数的差
rect2_phi Orientation of the smallest surrounding rectangle 最小外接矩形的方向
rect2_len1 Half the length of the smallest surrounding rectangle 最小外接矩形长度的一半 smallest_rectangle2
rect2_len2 Half the width of the smallest surrounding rectangle 最小外接矩形宽度的一半
moments_m11 Geometric moments of the region 几何矩
moments_m20 Geometric moments of the region 几何矩
moments_m02 Geometric moments of the region 几何矩
moments_ia Geometric moments of the region 几何矩
moments_ib Geometric moments of the region 几何矩
moments_m11_invar Geometric moments of the region 几何矩
moments_m20_invar Geometric moments of the region 几何矩
moments_m02_invar Geometric moments of the region 几何矩
moments_phi1 Geometric moments of the region 几何矩
moments_phi2 Geometric moments of the region 几何矩
moments_m21 Geometric moments of the region 几何矩
moments_m12 Geometric moments of the region 几何矩
moments_m03 Geometric moments of the region 几何矩
moments_m30 Geometric moments of the region 几何矩
moments_m21_invar Geometric moments of the region 几何矩
moments_m12_invar Geometric moments of the region 几何矩
moments_m03_invar Geometric moments of the region 几何矩
moments_m30_invar Geometric moments of the region 几何矩
moments_i1 Geometric moments of the region 几何矩
moments_i2 Geometric moments of the region 几何矩
moments_i3 Geometric moments of the region 几何矩
moments_i4 Geometric moments of the region 几何矩
moments_psi1 Geometric moments of the region 几何矩
moments_psi2 Geometric moments of the region 几何矩
moments_psi3 Geometric moments of the region 几何矩
moments_psi4 Geometric moments of the region 几何矩

车牌字符分割带中文(中文、数字、字母)

  1. ***** 读取图片
  2. dev_close_window ()
  3. read_image (Image, 'D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/chepai18.jpg')
  4. dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
  5. dev_display (Image)
  6. ***** 处理图片-定位车牌
  7. rgb1_to_gray (Image, GrayImage)
  8. threshold (GrayImage, Region, 80, 100)
  9. connection (Region, ConnectedRegions)
  10. select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 8870.97, 10000)
  11. ***** 处理图片- 转正图片
  12. shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
  13. area_center (RegionTrans, Area, Row, Column)
  14. orientation_region (RegionTrans, Phi)
  15. vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(0), HomMat2D)
  16. hom_mat2d_slant (HomMat2D, rad(15), 'x', Column, Row, HomMat2DSlant)
  17. affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
  18. affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
  19. reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)
  20. *************** 开始识别之车牌图片预处理,带中文字符分割
  21. rgb1_to_gray (ImageReduced, GrayImage1)
  22. threshold (GrayImage1, Regions, 176, 242)
  23. closing_circle (Regions, RegionClosing, 3.5)
  24. opening_circle (Regions, RegionOpening, 1.7)
  25. connection (RegionOpening, ConnectedRegions1)
  26. select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 20, 600)
  27. sort_region (SelectedRegions1, SortedRegions, 'character', 'true', 'column')
  28. *************** 组装苏字区域
  29. select_obj (SortedRegions, ObjectSelected1, 1)
  30. select_obj (SortedRegions, ObjectSelected2, 2)
  31. select_obj (SortedRegions, ObjectSelected3, 3)
  32. union2 (ObjectSelected1, ObjectSelected2, RegionUnion)
  33. union2 (RegionUnion, ObjectSelected3, RegionUnion1)
  34. select_obj (SortedRegions, ObjectSelected4, 4)
  35. select_obj (SortedRegions, ObjectSelected5, 5)
  36. select_obj (SortedRegions, ObjectSelected6, 6)
  37. select_obj (SortedRegions, ObjectSelected7, 7)
  38. select_obj (SortedRegions, ObjectSelected8, 8)
  39. select_obj (SortedRegions, ObjectSelected9, 9)
  40. ***************** 把所有区域组成一个对象
  41. gen_empty_obj (NumberObject)
  42. concat_obj (NumberObject, RegionUnion1, NumberObject)
  43. concat_obj (NumberObject, ObjectSelected4, NumberObject)
  44. concat_obj (NumberObject, ObjectSelected5, NumberObject)
  45. concat_obj (NumberObject, ObjectSelected6, NumberObject)
  46. concat_obj (NumberObject, ObjectSelected7, NumberObject)
  47. concat_obj (NumberObject, ObjectSelected8, NumberObject)
  48. concat_obj (NumberObject, ObjectSelected9, NumberObject)
  49. **************** 创建训练文件
  50. TrainFile:='D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/code/Charactor.trf'
  51. Words:=['苏','E','C','6','2','N','8']
  52. ** 完成图像与字符训练对应关系
  53. write_ocr_trainf (NumberObject, GrayImage1, Words, TrainFile)
  54. ** 读取训练文件
  55. read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
  56. ** 创建一个分类识别器
  57. create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
  58. ** 训练分类识别器
  59. trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)
  60. ** 保存分类识别文件
  61. write_ocr_class_mlp (OCRHandle, 'D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/code/Charactor.omc')
  62. *********************** 基于训练omc文件开始识别带中文车牌**************
  63. read_ocr_class_mlp ('D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/code/Charactor.omc', OCRHandle1)
  64. do_ocr_multi_class_mlp (NumberObject, GrayImage1, OCRHandle1, Class, Confidence)
  65. dev_clear_window ()
  66. dev_display (Image)
  67. dev_set_color ('red')
  68. set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
  69. for Index := 0 to |Class|-1 by 1
  70. set_tposition (WindowHandle,30, 120+36*Index)
  71. write_string (WindowHandle, Class[Index])
  72. endfor

统计每个骰子的点数(距离变换+分水岭)

  1. ** 关闭当前窗口。
  2. dev_close_window ()
  3. ** 打开一个新窗口,设置窗口大小和背景颜色。
  4. dev_open_window (0, 0, 500, 500, 'black', WindowHandle)
  5. ** 设置显示字体。
  6. set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
  7. ** 列出指定目录下的所有文件。
  8. list_files ('D:/ai/Halcon/vedio/061--机器视觉Course061Halcon的Blob图像分析/素材与源码/dice/', ['files','follow_links'], ImageFiles)
  9. ** 从文件列表中筛选出符合指定正则表达式的图像文件。
  10. tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
  11. ** 初始化结果索引。
  12. ResultIndex:=1
  13. ** 遍历图像文件列表。
  14. for Index := 0 to |ImageFiles| - 1 by 1
  15. ** 读取图像文件。
  16. read_image (Image, ImageFiles[Index])
  17. ** 获取图像尺寸。
  18. get_image_size (Image, Width, Height)
  19. ** 对图像进行预处理
  20. ** 对图像进行阈值化。
  21. threshold (Image, Regions, 180, 255)
  22. ** 填充区域。
  23. fill_up (Regions, RegionFillUp)
  24. ** 进行连通性分析。
  25. connection (RegionFillUp, ConnectedRegions)
  26. ** 计算距离变换。
  27. distance_transform (ConnectedRegions, DistanceImage, 'octagonal', 'true', 1000, 1000)
  28. ** 转换图像类型为 BYTE
  29. convert_image_type (DistanceImage, ImageConverted, 'byte')
  30. ** 对图像进行反色处理。
  31. invert_image (ImageConverted, ImageInvert)
  32. ** 对图像进行最大值缩放。
  33. scale_image_max (ImageInvert, ImageScaleMax)
  34. ** 使用分水岭算法进行图像分割。
  35. watersheds_threshold (ImageScaleMax, Basins, 10)
  36. ** 计算分割结果和连通区域的交集。
  37. intersection (Basins, ConnectedRegions, RegionIntersection)
  38. ** 对分割结果进行处理
  39. ** 根据面积选择形状。
  40. select_shape (RegionIntersection, SelectedRegions, 'area', 'and', 30000, 100000)
  41. ** 统计选定区域数量。
  42. count_obj (SelectedRegions, Number)
  43. ** 在窗口中显示原始图像。
  44. dev_display (Image)
  45. ** 遍历每个选定区域。
  46. for Index1 := 1 to Number by 1
  47. ** 选择指定的区域。
  48. select_obj (SelectedRegions, ObjectSelected, Index1)
  49. ** 缩小图像域。
  50. reduce_domain (Image, ObjectSelected, ImageReduced)
  51. ** 对缩小后的图像进行阈值化。
  52. threshold (ImageReduced, Regions1, 0, 150)
  53. ** 填充区域。
  54. fill_up (Regions1, RegionFillUp1)
  55. ** 进行连通性分析。
  56. connection (RegionFillUp1, ConnectedRegions1)
  57. ** 根据面积选择形状。
  58. select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 200, 20000)
  59. ** 统计选定区域数量。
  60. count_obj (SelectedRegions1, Number1)
  61. ** 计算区域中心。
  62. area_center (ObjectSelected, Area, Row, Column)
  63. ** 在图像中显示消息。
  64. disp_message (WindowHandle, Number1, 'image', Row, Column, 'green', 'false')
  65. endfor
  66. ** 停止程序执行。
  67. stop()
  68. endfor


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

Halcon 学习笔记一

Halcon 学习笔记一[TOC] Halcon学习笔记一 算子 描述 dev_ 系统、窗口相关算子 read_ 与读相关算子...

Halcon 基础应用(1)

Halcon 基础应用(1)[TOC] Halcon中的数组创建数组** 元组/数组 ** 创建一个数组 A := [] A[0] := 1 A[1] := 2 创建...

Halcon 图像增强

Halcon 图像增强[TOC] 概念和分类图像增强:有目的的强调图像的整体或局部特性,将原来不清晰的图像变得清晰或强调某些感...

Halcon 图像分割

Halcon 图像分割[TOC] 边缘检测边缘检测简介边缘是指图像中像素灰度有阶跃变化或屋顶状变化的那些像素的集合。它包含了丰...

cAPS.NET 保存base64位格式图片

publicvoidUpload() { //取出图片对应的base64位字符 stringimgBase=Request[&quot;imgBase&quot;]; //c#里边的base6...

使用OLEDB读取不同版本Excel连接字符串设置

使用OleBD读取excel的时候,excel不同的版本,连接字符串的写法也会不一样。///&lt;summary&gt; ///读取excel ///&lt;/su...

vs2017 对 COM 组件调用返回了错误 HRESULT E_FAIL

vs2017添加引用报错 对 COM 组件的调用返回了错误 HRESULT E_FAIL 1.以管理员身份打开vs2017开发人员命令指示符 2...

分布式服务架构与微服务架构概念区别与联系

分布式:分散压力。微服务:分散能力。当下理解分布式:不同模块部署在不同服务器上作用:分布式解决网站高并发带来问题集...

分布式-微服务-集群区别

1.分布式将一个大的系统划分为多个业务模块,业务模块分别部署到不同的机器上,各个业务模块之间通过接口进行数据交互。区...

EasyUI弹窗批量修改combogrid下拉框

JS方法//点击弹出批量修改框 UpdateLot:function(){ varrow=$(&quot;#dg&quot;).datagrid(&quot;getChecked&quot;); if(...

js与Controller中分割字符串方法

js: varstr=OpenRule; varstrs=newArray(); strs=str.split(&quot;,&quot;); for(vari=0;i&lt;strs.length;i++){ $(&q...

如何修改重置MD5加密后SQL用户密码

二次开发时,要加一个忘记密码的功能,后台写了修改密码的方法,数据库执行也修改成功,但是登录一直提示密码错误。之所以...

如何修改CSS中存在element.style内联样式

改腾讯地图的时候调整了下样式,发现样式一直存在问题,修改style里面的值,一点用都没有,html中这个值还找不到是在哪里出...

微信交易单号和订单号区别

一般第三方在线支付系统中都会有两类订单号transactionId 为支付系统的订单号,由支付系统生成,并在回调时传回给商户,用...

C ?、?? 问号和2个问号用法(类型?、对象?)

C# ?C# ???:单问号1.定义数据类型可为空。可用于对int,double,bool等无法直接赋值为null的数据类型进行null的赋值如这...
这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术