实例图片
C:\Users\Public\Documents\MVTec\HALCON-19.11-Progress\examples\images
*读取图片
read_image (Image1, 'D:/c++/image/1.png')
*读取大小
get_image_size(Image1,Width, Height)
*创建窗口
dev_close_window()
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Image1)
*获得图像的时间
get_image_time(Image1, MSecond, Second, Minute, Hour, Day, YDay, Month, Year)
*获取类型
get_image_type(Image1, Type)
*指针,内存地址
get_image_pointer1(Image1, Pointer, Type1, Width1, Height1)
read_image(Image,'D:/c++/image/2.jpg')
*图片分成rgb
decompose3(Image, Image11, Image2, Image3)
*rgb在进行合并
compose3(Image11, Image2, Image3, MultiChannelImage)
*rgb变成灰通道
rgb1_to_gray(MultiChannelImage, GrayImage)
*rgb转换成hsv颜色空间,使用色调,饱和度,亮度,表示
trans_from_rgb(Image11, Image2, Image3, ImageResult1, ImageResult2, ImageResult3, 'hsv')
对图像进行抠图,轮廓,和亚像素
*读取图片
read_image(Image, 'fabrik')
*读取图像大小
get_image_size(Image, Width, Height)
*使用窗口
dev_clear_window()
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Image)
*对图像进行,阈值判断,全局阈值
threshold(Image, Region, 128, 255)
*对图像进行打散标注
connection(Region, ConnectedRegions)
*使用面积进行过滤,rectangularity:使用矩形过滤
select_shape(ConnectedRegions, SelectedRegions, 'rectangularity', 'and', 0.9, 1)
*在使用一次进行过滤
select_shape(SelectedRegions, SelectedRegions1, 'area', 'and',7500, 99999)
*进行填充
fill_up(SelectedRegions1, RegionFillUp)
*进行抠图,减少范围,roi
reduce_domain(Image, RegionFillUp, ImageReduced)
*找到对应的阈值
threshold(ImageReduced, Region1, 0, 100)
*得到亚像素轮廓,
threshold_sub_pix(ImageReduced, Border, 158)
*在轮廓中,使用的是轮廓的长度,进行过滤筛选
select_contours_xld(Border, SelectedContours, 'contour_length', 90, 120, -0.5, 0.5)
*适合的区域有多少个
count_obj(SelectedContours, Number)
*对轮廓进行填充
gen_region_contour_xld(SelectedContours, Region2, 'filled')
*可以在进行抠图
reduce_domain(ImageReduced, Region2, ImageReduced1)
形态学,腐蚀,膨胀
*区域集合运算及变换
*读取图片
read_image(Image, 'D:/c++/image/5')
*图片大小
get_image_size(Image, Width, Height)
dev_close_window()
*开窗口
dev_open_window(0, 0, Width/2, Height/2, 'black', WindowHandle)
*图像显示
dev_display(Image)
*转换成灰色图片
rgb1_to_gray(Image, GrayImage)
*设置字体
set_display_font ( WindowHandle, 14, 'mono', 'true', 'false')
*显示字体右下角
disp_continue_message ( WindowHandle, 'black', 'true')
Grag := 80
*直方图全阈值分割
threshold(GrayImage, Region, Grag, 255)
*自动阈值,dark:黑色的部分找出来,light:白色部分
binary_threshold(GrayImage, Region1, 'max_separability', 'light', UsedThreshold)
*均值滤波,教程写的是亮度像素值乘2+1
mean_image(GrayImage, ImageMean, 20, 20)
*原图与滤波后的图进行比较,明暗差别大于5的进行选中,动态阈值
dyn_threshold(GrayImage, ImageMean, RegionDynThresh, 8, 'light')
*查看多少个
count_obj(RegionDynThresh, Number)
*进行打散
connection(RegionDynThresh, ConnectedRegions)
*查看多少个678个
count_obj(ConnectedRegions, Number1)
*进行面积的阈值处理
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 99999)
select_shape(SelectedRegions, SelectedRegions1, 'area', 'and', 900,1200)
*用大的分割图减去小的分割图
difference(SelectedRegions, SelectedRegions1, RegionDifference)
*进行填充
fill_up(RegionDifference, RegionFillUp)
*用矩形相似度
select_shape(RegionFillUp, SelectedRegions2, 'rectangularity', 'and', 0.9, 1)
*使用外接圆
select_shape(SelectedRegions2, SelectedRegions3, 'outer_radius', 'and', 90, 120)
*中心点使用画出外接圆,内接圆,矩形,等
shape_trans(SelectedRegions3, RegionTrans, 'inner_circle')
*可以进行画
draw_region(Region2, WindowHandle)
*凸性,不规则图像,用最高点进行了连接
shape_trans(Region2, RegionTrans1, 'convex')
*用凸性给出的区间画出的,
difference(RegionTrans1, Region2, RegionDifference1)
*图像显示
dev_display(SelectedRegions)
*骨架,再中间
skeleton(SelectedRegions, Skeleton)
dev_display(Region2)
*先膨胀后收缩可以填坑,闭运算
*膨胀
dilation_circle(Region2, RegionDilation, 3.5)
*收缩
erosion_circle(RegionDilation, RegionErosion, 3.5)
*用膨胀减去收缩的可以得到边缘
difference(RegionDilation, RegionErosion, RegionDifference2)
*获取边缘
boundary(RegionDifference2, RegionBorder, 'inner')
画出圆,矩形,计算出距离
read_image(Image, 'D:/c++/image/5')
*读取图像大小
get_image_size(Image, Width, Height)
*使用窗口
dev_clear_window()
dev_open_window(0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display(Image)
*创建一个圆型的roi
draw_circle(WindowHandle, Row, Column, Radius)
*生产区域的圆
gen_circle(Circle, Row, Column, Radius)
*创建一个椭圆
draw_ellipse(WindowHandle, Row1, Column1, Phi, Radius1, Radius2)
*生成椭圆
gen_ellipse(Ellipse, Row1, Column1, Phi, Radius1, Radius2)
*生成一个外接圆
smallest_circle(Ellipse, Row2, Column2, Radius3)
*显示出来
gen_circle(Circle1, Row2, Column2, Radius3)
*创建一个矩形
draw_rectangle1(WindowHandle, Row11, Column11, Row21, Column21)
*显示矩形
gen_rectangle1(Rectangle, Row11, Column11, Row21, Column21)
*创建一个带方向的矩形
draw_rectangle2(WindowHandle, Row3, Column3, Phi1, Length1, Length2)
*显示出来
gen_rectangle2(Rectangle1, Row3, Column3, Phi1, Length1, Length2)
*创建用点来创建
draw_region(Region, WindowHandle)
*创建一个直线
draw_line(WindowHandle, Row12, Column12, Row22, Column22)
*显示直线
gen_region_line(RegionLines, Row12, Column12, Row22, Column22)
draw_point(WindowHandle, Row4, Column4)
gen_region_points(Region1, Row4, Column4)
dev_display(Image)
*创建一个矩形
draw_rectangle1(WindowHandle, Row11, Column11, Row21, Column21)
*显示矩形
gen_rectangle1(Rectangle, Row11, Column11, Row21, Column21)
*保存区域
write_region(Rectangle, 'D:/c++/image/region.hobj')
dev_display(Image)
*读取感兴趣区域
read_region(RegionROI,'D:/c++/image/region.hobj' )
*照片与感兴趣区域直接就给截取出来,但是不是从0开始的
reduce_domain(Image, RegionROI, ImageReduced)
*生成一个空区域
gen_empty_region(RegionROI)
gen_empty_obj(EmptyObject)
*让一个矩形添加进去
concat_obj(RegionROI, Rectangle, RegionROI)
*让一个圆写进去
concat_obj(RegionROI, Circle, RegionROI)
*创建一个点
dev_display(Image)
draw_point(WindowHandle, Row5, Column5)
gen_cross_contour_xld(Cross, Row5, Column5, 60, 0)
*创建一个点
draw_point(WindowHandle, Row6, Column6)
gen_cross_contour_xld(Cross1, Row6, Column6, 60, 0)
*计算两个点的距离
distance_pp(Row5, Column5, Row6, Column6, Distance)
*现在中如果是10厘米
readDis := 10
*用现实中的距离除于像素中的,就可以得到比值
mmpp := readDis/Distance1
*创建一个线
draw_line(WindowHandle, Row13, Column13, Row23, Column23)
*显示出来
gen_region_line(RegionLines1, Row13, Column13, Row23, Column23)
*点到直线的距离
distance_pl(Row5, Column5, Row13, Column13, Row23, Column23, Distance1)
* 得到的比值在乘线到点的距离
read := mmpp *Distance1
*点到区域的距离,后面是最长的距离和最短的距离
distance_pr(RegionLines1, Row6, Column6, DistanceMin, DistanceMax)
*创建用鼠标画出不规则图像
draw_region(Region2, WindowHandle)
*用这个不规则图形,能测量出与直线最近距离和最远距离
distance_lr(Region2, Row13, Column13, Row23, Column23, DistanceMin1, DistanceMax1)
*区域带区域之间的最短距离的点是多少
distance_rr_min(Region2, RegionLines1, MinDistance, Row14, Column14, Row24, Column24)
*显示出来
gen_region_line(RegionLines2, Row14, Column14, Row24, Column24)
*最短的点用坐标点显示出来
gen_cross_contour_xld(Cross2, Row14, Column14, 60, 0.48)
gen_cross_contour_xld(Cross3, Row24, Column24, 60,0.48 )
对区域进行移动
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'white', WindowID)
dev_set_color ('black')
dev_set_draw ('margin')
dev_clear_window ()
* Draw with the mouse an arbitrary region into the window
draw_region (Region, WindowID)
area_center (Region, Area, Row1, Column1)
Button := 1
while (Button == 1)
* Click to the position where the region should be placed
* Use right mouse button to exit the loop
get_mbutton (WindowID, Row, Column, Button)
move_region (Region, RegionMoved, Row - Row1, Column - Column1)
endwhile
可以早到矩形的各个边
read_image(Image, '1')
get_image_size(Image, Width, Height)
dev_close_window()
dev_open_window(0, 0, Width/4, Height/4, 'black', WindowHandle)
dev_display(Image)
*均值滤波
mean_image(Image, ImageMean, 50, 50)
dyn_threshold(Image, ImageMean, RegionDynThresh, 10, 'light')
connection(RegionDynThresh, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 45000, 99999)
*使用矩形进行填充,进行筛选
fill_up(SelectedRegions, RegionFillUp)
select_shape(RegionFillUp, SelectedRegions1, 'rectangularity', 'and', 0.8, 1)
*闭运算
closing_circle(SelectedRegions1, RegionClosing, 43.5)
*开运算
opening_circle(RegionClosing, RegionOpening, 43.5)
*选择边
boundary(RegionOpening, RegionBorder, 'inner')
*整体矩形往上面挪动
move_region(RegionOpening, RegionMoved, -15 , 1)
*将矩形进行缩小
erosion_circle(RegionMoved, RegionErosion, 13.5)
*相交的画出,得到一个直线
intersection(RegionBorder, RegionErosion, RegionIntersection)
物体放大
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'white', WindowID)
dev_set_color ('black')
* Draw with the mouse an arbitrary (small) region into the window
draw_region (Region, WindowID)
*缩放宽高都放大2倍
zoom_region (Region, RegionZoom, 2, 2)
dev_clear_window ()
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (Region)
dev_set_color ('green')
dev_display (RegionZoom)
生成中心线,在进行镜像
*找出中心线
gen_region_line(RegionLines,Height/2,0,Height/2,Width)
*如果使用中心线进行镜像,就是(Height/2)*2
mirror_region(要进镜像的参数,输出的参数,"row",(Height/2)*2)
转置
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'white', WindowID)
dev_set_color ('black')
* Draw with the mouse an arbitrary region into the window
draw_region (Region, WindowID)
transpose_region (Region, Transposed, 256, 256)
dev_clear_window ()
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (Region)
dev_set_color ('green')
dev_display (Transposed)
一维码
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] Camera MV-UB500#CC5CCDD3-4', 0, -1, AcqHandle)
*创建句柄
create_bar_code_model([], [], BarCodeHandle)
dev_close_window()
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_set_color('green')
dev_set_draw('margin')
dev_set_line_width(3)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
*后面数字是识别多少个
set_bar_code_param(BarCodeHandle, 'element_size_min', 2 )
while (true)
grab_image (Image, AcqHandle)
rgb1_to_gray(Image, GrayImage)
* Image Acquisition 01: Do something
*直方图阈值
threshold(GrayImage, Region2, 100, 255)
*全局阈值
binary_threshold(Image, Region1, 'max_separability', 'light', UsedThreshold1)
connection(Region1, ConnectedRegions2)
*选着给定形状的区域,计算区域中的最小外接平面矩形
select_shape_std(ConnectedRegions2, SelectedRegions2, 'max_area', 70)
*变换区域形状
shape_trans(SelectedRegions2, RegionTrans1, 'convex')
*通过reduce_domain确实能获得特定区域Region位置的图像,直接进行抠图
reduce_domain(Image, RegionTrans1, ImageReduced1)
*通过均值平滑图像
mean_image(ImageReduced1, ImageMean1, 9, 9)
*阈值分割
dyn_threshold(ImageReduced1, ImageMean1, RegionDynThresh1, 1, 'dark')
connection(RegionDynThresh1, ConnectedRegions3)
*筛选
select_shape(ConnectedRegions3, SelectedRegions3, 'area', 'and', 3000, 4800)
select_shape(SelectedRegions3, SelectedRegions6, 'width', 'and', 200, 99999)
fill_up(SelectedRegions6, RegionFillUp3)
*筛选出矩形,算子满足特征区域,'rectangle1':计算区域的最小外接平行矩形,
select_shape_std(RegionFillUp3, SelectedRegions9, 'rectangle1', 60)
dilation_circle(SelectedRegions9, RegionDilation1, 40.5)
*进行抠图
reduce_domain(Image, SelectedRegions9, ImageReduced3)
*dyn_threshold(ImageReduced3, ImageReduced3, RegionDynThresh2, 1, 'dark')
find_bar_code(ImageReduced3, SymbolRegions1, BarCodeHandle, 'auto', DecodedDataStrings1)
get_bar_code_result(BarCodeHandle, 'all', 'decoded_types', BarCodeResults1)
disp_message(WindowHandle, DecodedDataStrings1, 'window', 12, 12, 'black', 'true')
endwhile
clear_bar_code_model(BarCodeHandle)
close_framegrabber (AcqHandle)
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] Camera MV-UB500#CC5CCDD3-4', 0, -1, AcqHandle)
*创建句柄
create_bar_code_model([], [], BarCodeHandle)
dev_close_window()
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_set_color('green')
dev_set_draw('margin')
dev_set_line_width(3)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
*后面数字是识别多少个
set_bar_code_param(BarCodeHandle, 'element_size_min', 2 )
while (true)
grab_image (Image, AcqHandle)
*进行识别
find_bar_code(Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
get_bar_code_result(BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
disp_message(WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')
endwhile
clear_bar_code_model(BarCodeHandle)
close_framegrabber (AcqHandle)
一维码设置的参数
set_bar_code_param (BarCodeHandle, ‘contrast_min’, ‘True’)*设置后面的true和false进行不同解码,true进行全局解码,速度慢但是准确,false识别前几个进行解码,会有错误