(2012-05-08 15:03:57) 转载▼ 标签:
机器视觉
计算机视觉
halcon
Blob Analysis是对图像中相同灰度值的前景像素进行识别的一种方法。Blob就是灰度值相同的前景像素构成的连通域。Blob分析可为机器视觉应用提供图像中的blobs的数量、位置、形状和方向等信息,还可以提供相关blobs间的几何拓扑结构。 1.1 基本理念
图像分割的结果就是所谓的Blob(binary large objects),在halcon中该数据类型被叫做“区域”。
在特征提取这一步,Blob的像素数,重心,方向等被计算出来。
例: 计算所有灰度值在120和255像素构成的8连通区域的面积与中心坐标。
read_image (Image, 'particle')
threshold (Image, BrightPixels, 120, 255) connection (BrightPixels, Particles) area_center (Particles, Area, Row, Column) 1.2 扩展的理念
(1)应用ROI可以使Blob分析加速。(2)匹配ROI区域或图像,详将GUIDEIIB以形状为基础的匹配。(3)校正图像<经常用来去除镜头畸变或把图像转换到参考点视角,如双目视觉时的图像校正>(4)图像前处理(5)引用分割参数(6)分割图像(7)区域处理(8)特征提取(9)把提取的结果转换到世界坐标中(10)结果可视化。 1.3例程序 1.3.1例程序1.
* crystal.hdev: extraction of hexagonally shaped crystals via local thresholding and region post-processing* //通过局部阈值和区域后处理提取六边形晶体// dev_close_window () dev_update_window ('off') * step: acquire image read_image (Image, 'crystal')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowID) set_display_font (WindowID, 12, 'Courier', 'true', 'false') dev_set_draw ('margin') dev_set_line_width (2) dev_display (Image)
disp_continue_message (WindowID, 'black', 'true') stop ()
* step: segment image * -> using a local threshold
mean_image (Image, ImageMean, 21, 21) //利用21*21的掩膜进行均值滤波去噪// dyn_threshold (Image, ImageMean, RegionDynThresh, 8, 'dark') //利用和平滑后图像比较,提取区域边缘
* -> extract connected components
connection (RegionDynThresh, ConnectedRegions)//抽取连通的区域//
dev_display (ConnectedRegions)
disp_continue_message (WindowID, 'black', 'true') stop ()
* step: process regions
shape_trans (ConnectedRegions, ConvexRegions, 'convex')//转化区域的形状// select_shape (ConvexRegions, LargeRegions, 'area', 'and', 600, 2000)//依据形状特征选择区域//
select_gray (LargeRegions, Image, Crystals, 'entropy', 'and', 1, 5.6)//依据灰度特征选择区域,
dev_display (Image) 此处利用图像信息熵的范围进行选择// dev_display (Crystals)
1.3.2例程序2
* atoms.hdev: Locates irregularities in an atomic grid structure//找到不规则的原子网架结构//
dev_close_window () dev_update_window ('off') * step: acquire image read_image (Image, 'atoms')
get_image_pointer1 (Image, Pointer, Type, Width, Height)//返回图像通道指针,类型和大小//
crop_rectangle1 (Image, Image, Height/2, 0, Height-1, Width-1)//剪裁图像为矩形图像//
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID) set_display_font (WindowID, 12, 'Courier', 'true', 'false') dev_set_draw ('margin') dev_set_line_width (2)
dev_display (Image)
disp_message (WindowID, 'Original image', 'window', -1, -1, 'black', 'true') disp_continue_message (WindowID, 'black', 'true') stop ()
* step: segment image * -> using watershed
gauss_image (Image, ImageGauss, 5) //对图像进行高斯滤波//
watersheds (ImageGauss, Basins, Watersheds)//利用分水岭算法对图形进行分割,输出区域// dev_display (Image) dev_set_colored (12) dev_display (Watersheds)
disp_message (WindowID, 'Watersheds', 'window', -1, -1, 'black', 'true') disp_continue_message (WindowID, 'black', 'true') stop ()
* step: process regions
* -> skip regions at the border of the image //略去边缘区域// select_shape (Basins, SelectedRegions1, 'column1', 'and', 2, Width-1) select_shape (SelectedRegions1, SelectedRegions2, 'row1', 'and', 2, Height-1) select_shape (SelectedRegions2, SelectedRegions3, 'column2', 'and', 1, Width-3) select_shape (SelectedRegions3, Inner, 'row2', 'and', 1, Height-3) * -> select irregularly shaped atoms
select_shape (Inner, Irregular, 'compactness', 'and', 1.45, 3)//根据紧凑度选择区域// dev_display (Image) dev_set_line_width (1) dev_set_color ('white') dev_display (Inner) dev_set_line_width (3) dev_set_color ('red')
dev_display (Irregular)
disp_message (WindowID, 'Defects', 'window', -1, -1, 'black', 'true')
1.3.3例程序3
* particle.hdev: Measurement of small particles *
dev_update_off () dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowID) set_display_font (WindowID, 14, 'mono', 'true', 'false') read_image (Image, 'particle') dev_display (Image)
disp_message (WindowID, 'Original image', 'window', 12, 12, 'black', 'true') disp_continue_message (WindowID, 'black', 'true') stop ()
threshold (Image, Large, 110, 255) //输出110-255灰度值之间灰度的区域// * Dilate regions with a circular structuring element
dilation_circle (Large, LargeDilation, 7.5) //对区域进行膨胀运算// dev_display (Image) dev_set_draw ('margin') dev_set_line_width (3) dev_set_color ('red') dev_display (LargeDilation) dev_set_draw ('fill')
disp_message (WindowID, 'Exclude large areas from processing', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true') stop ()
* Continue to calculate small regions * Return the complement of a region
complement (LargeDilation, NotLarge) //对区域进行补集运算// reduce_domain (Image, NotLarge, ParticlesRed) //减小图像的定义域// mean_image (ParticlesRed, Mean, 31, 31) //均值滤波// * Segment the image using a local threshold
dyn_threshold (ParticlesRed, Mean, SmallRaw, 3, 'light') //输出图像边缘,利用与平滑图像的OFFSET
opening_circle (SmallRaw, Small, 2.5) //对区域进行开运算操作// connection (Small, SmallConnection) //计算连通区域// dev_display (Image) dev_set_colored (12)
dev_display (SmallConnection)
disp_message (WindowID, 'Extracted small particles', 'window', 12, 12, 'black', 'true') disp_continue_message (WindowID, 'black', 'true') stop ()
* Continue to select several region and to get informations dev_set_color ('green') dev_display (Image) dev_set_draw ('margin') dev_display (SmallConnection) Button := 1 while (Button = 1)
disp_message (WindowID, ['Select object with left mouse button','Right button to quit'], 'image', 5, 5, 'black', 'true') dev_set_color ('green')
get_mbutton (WindowID, Row, Column, Button) dev_display (Image)
dev_display (SmallConnection) dev_set_color ('red')
select_region_point (SmallConnection, SmallSingle, Row, Column)//输出包含某个点的所有区域//
dev_display (SmallSingle)
count_obj (SmallSingle, NumSingle) //计算TUPLE中的元素个数// if (NumSingle=1)
intensity (SmallSingle, Image, MeanGray, DeviationGray)//计算灰度的均值和方差//
area_center (SmallSingle, Area, Row, Column)//计算区域面积和中心坐标// disp_message (WindowID, ['Area = '+Area, 'Intensity = '+MeanGray$'.3'], 'image', Row+10, Column-90, 'black', 'true') endif endwhile
dev_set_line_width (1) dev_update_on () 1.3.4例程序4
* fin.hdev: Detection of a fin dev_update_window ('off') read_image (Fin, 'fin1')
get_image_size (Fin, Width, Height) dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowID) dev_display (Fin)
set_display_font (WindowID, 14, 'mono', 'true', 'false') disp_continue_message (WindowID, 'black', 'true') stop ()
* read_image (Fin, 'fin2') * read_image (Fin, 'fin3')
bin_threshold (Fin, Dark)//利用计算的直方图最小灰度值来分割图像为两个区域//
difference (Fin, Dark, Background) dev_set_color ('blue') dev_set_draw ('margin') dev_set_line_width (4) dev_display (Background)
disp_continue_message (WindowID, 'black', 'true') stop ()
closing_circle (Background, ClosedBackground, 250) dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true') stop ()
difference (ClosedBackground, Background, RegionDifference) opening_rectangle1 (RegionDifference, FinRegion, 5, 5) dev_display (Fin) dev_set_color ('red') dev_display (FinRegion)
area_center (FinRegion, FinArea, Row, Column) dev_set_draw ('fill') dev_set_line_width (1) dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true') stop ()
dev_close_window () 1.3.5例程序5
* fin.hdev: Detection of a fin //检测塑料工件边缘不规则部分// dev_update_window ('off')
read_image (Fin, 'fin1')
get_image_size (Fin, Width, Height) dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowID) dev_display (Fin)
set_display_font (WindowID, 14, 'mono', 'true', 'false') disp_continue_message (WindowID, 'black', 'true') stop ()
* read_image (Fin, 'fin2') * read_image (Fin, 'fin3')
bin_threshold (Fin, Dark) //分割图像,输出Dark区域,Fin已经被处理为区域//
difference (Fin, Dark, Background) //计算Fin与Dark两个区域的补集// dev_set_color ('blue') dev_set_draw ('margin') dev_set_line_width (4) dev_display (Background)
disp_continue_message (WindowID, 'black', 'true') stop ()
closing_circle (Background, ClosedBackground, 250) //Fin与Dark两个区域的补集为
dev_set_color ('green') Background,对此区域进行闭运算//
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
difference (ClosedBackground, Background, RegionDifference) //补集为多余边缘区域//
opening_rectangle1 (RegionDifference, FinRegion, 5, 5) //开运算,矩形结构元素// dev_display (Fin) dev_set_color ('red') dev_display (FinRegion)
area_center (FinRegion, FinArea, Row, Column) dev_set_draw ('fill') dev_set_line_width (1) dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true') stop ()
dev_close_window () 运算结果:
因篇幅问题不能全部显示,请点此查看更多更全内容