1.像素 2592x1944 相当于 500万像素
2.图像 本质是二位数组(内存里面是一维数组)
图像越大 分别率越大
3.特征对比相对大的 方便寻找大的 灰度图片进行处理
- 图像->区域->图像
函数
read_image() 从磁盘读取文件
get_image_size() 获取图片大小
dev_close_window() 关闭显示图片的窗口
dev_open_window() 打开显示图片窗口
threshold() 根据灰度值查找区域 全局
dyn_threshold() 局部域值
binary_threshold()
mean_image() 均值滤波
connection() 打散查找的区域
select_shape() 按照打散的区域按照特征值查找
fill_up() 填充区域
reduce_domain() 抠图
threshold)sub_pix() 亚像素灰度值查找
select_contours_xld() 按照亚像素查找线的长度
count_obj() 按照亚像素获取查找结果
shape_trans() 对形状进行仿射变换。它可以对点、直线、圆、矩形等形状进行平移、旋转和缩放等操作
opening_circle() 开运算
rgb_to_gray() 彩色图转灰度图
p7 BLOB分析
图像处理流程
1 获取图像
2 图像分割
3 区域处理
4 特征提取
秘诀:定位特征 逐步求精
p8
p9
图像分割介绍
1 灰度直方图
2 阈值分割
threshold
binary_threshold
mean_image + dyn_threshold
fast_threshold
auto_threshold
var_threshold
3区域集合运算及变换
1集合运算
union1
union2
difference
intersection
2区域变换
connection
fill_up
shape_trans
skeletiom
4形态学
opening 开
closing 闭
dilation 膨胀
erosion 腐蚀
boundary
p10
p11
画ROI区域和区域生成
保存区域,读取区域
数据值及对象输出
联合Vc进行变成输出结果,进行代码架构
1画ROI区域并生成区域
生成区域
draw_circle
gen_circle
draw_ellipse
gen_ellipse
draw_rectangle1
gen_rectangle1
draw_rectangle2
gen_rectangle2
draw_region
draw_line
gen_region_line
gen_region_points
其他
gen_grid_region
gen_checker_region
gen_region_hline
2保存区域读取区域(小件左右检测)
read_region
write_region
3生成空对象,向空对象中添加对象
gen_empty_region
gen_empty_obj
concat_obj
4区域间距离
点到点 distance_pp
点到直线 distance_pl
点到区域的最长最短 distance_pr
直线到区域最长最短 distance_lr
区域与区域最短 distance_rr
https://www.bilibili.com/video/BV1x5411H7Ns/?p=11&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p12
p13
p14
p15
p16
https://www.bilibili.com/video/BV1x5411H7Ns?p=16&vd_source=9ed5e7be5d8180766946b38de14a1781
write_region 写区域图片
p17
p19
https://www.bilibili.com/video/BV1x5411H7Ns?p=19&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
图片 image hobject
区域 region
数组 image := [,]
变量 hTuple
x的像素当量和y的像素当量不一样
工作流程
1采集图片
2预处理 二值化 形态学
3求出图像中心点 特征学
4防射变换 变成机械坐标
p22
1几何变换
平移 move_region
缩放 zoom_region
镜像 mirror_region
倒置 transpose_region
极坐标与笛卡尔坐标互转 polar_trans_region, polar_trans_region_inv
closing 闭运算 --> 先膨胀在腐蚀 去掉目标内的孔洞
opening 开运算 --> 先腐蚀在膨胀 去掉目标外的孔洞
通常当有噪声的图像使用阈值二值化后,所得到的边界是很不平滑的,物体区域具有一些错判的孔洞,背景区域散布着一些小的噪声
物体,连续的开和闭运算可用显著的改善这种情况
boundary 边界
intersection 相交
erosion 腐蚀
scale 比例
zoom 快速移动
shape_trans 区域变换
invert 反转
光度立体法
OCR识别是把区域抠出来
p23
https://www.bilibili.com/video/BV1x5411H7Ns/?p=23&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
仿射变换
区域的任意变换 affine_trans_region
创建二维变换矩阵 hom_mat2d_identity
平移 hom_mat2d_translate
缩放 hom_mat2d_scale
旋转 home_ma2d_rotate
刚性防射变换 vector_to_rigid
拓展图像变换 affine_trans_image
分为三步
1创建矩阵
p24
https://www.bilibili.com/video/BV1x5411H7Ns/?p=24&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
difference 剪掉
区域检测常用方案 结合一起使用
1 面积
2 灰度值
contours 轮廓
segment 分割
complement 补集
intensity 强度
means 方法
deviation 差
p25 一维码识别
https://www.bilibili.com/video/BV1x5411H7Ns/?p=25&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p26
https://www.bilibili.com/video/BV1x5411H7Ns?p=26&vd_source=9ed5e7be5d8180766946b38de14a1781
candidate 候选
variable 多样
cylinder 圆柱体
majory 多数的
determine 绝对
decode 解码
p28
https://www.bilibili.com/video/BV1x5411H7Ns/?p=28&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
二维码的流程和方法
掌握二维码的图像滤波
symbol 象征
XLD 边缘轮廓
p29
https://www.bilibili.com/video/BV1x5411H7Ns?p=29&vd_source=9ed5e7be5d8180766946b38de14a1781
recognition 识别
enhanced 增强模式
morphology 形态学
polaryty 极性的
contrast 对比度
均值滤波 mean_image
中值滤波 median_image
腐蚀 erosion
膨胀 dilation
//不是一个正常屏幕拍照的情况
1.极坐标转换
2.投射变换
hom_vector_hom_mat2d(...) //创建二维矩阵
projective_trans_image(...) //投射变换
https://www.bilibili.com/video/BV1x5411H7Ns/?p=29&vd_source=9ed5e7be5d8180766946b38de14a1781
时间:
52.35
灰度图没有前景背景
p30
OCR 光学字符识别
使用Halcon自带的.omc文件字库(训练好的)
invert_image() 反转像素图片
gray_range_rect() 灰度范围
p31
https://www.bilibili.com/video/BV1x5411H7Ns/?p=31&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
ocr需要白底黑字的图像,所以要反转
p32
https://www.bilibili.com/video/BV1x5411H7Ns/?p=32&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
FontNmae := ‘Industrial_0-9A-Z_NoRej’
read_ocr_class_mlp(FontNmae, OCRHandle)
for i:=1 to NumberC by 1
select_obj(SortedRegions, ObjectSelected, i)
do_ocr_single_class_mlp(ObjectSelected, ImageInvert, OCRHandle, 1, Class, Confidence)
disp_message(WindowHandle, Class, ‘image’, MeanRow-50, Column[i-1]-10, ‘yellow’, ‘false’)
endfor
decompose3(...)
clip_region(...) 剪切
sub_image(...) 图像减法
intensity() //灰度平均值
expand_gray_ref() //灰度膨胀
paint_region(...) //将图片与相交区域之外填充 可用做白底黑字
p33
https://www.bilibili.com/video/BV1x5411H7Ns/?p=33&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p34
gray_opening_rect(...)
fill_up_shape()
delete_file()
append_ocr_trainf(...) //训练文件
训练过程
1生成训练用的文件append_ocr_trainf将区域,图像和对应的字符装载到该文件中
2创建训练模型并进行训练create_ocr_class_mlp,trainf_ocr_class_mlp
3保存结果到训练文件中write_ocr_class_mlp
4清空缓存clear_ocr_class_mlp
p35
https://www.bilibili.com/video/BV1x5411H7Ns/?p=35&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p36
ocr助手使用
p37 图像滤波需求
https://www.bilibili.com/video/BV1x5411H7Ns?p=37&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
blob分析 --- 二值化的过程
1获取图片
2图像预处理
平滑处理
均值滤波 mean_image mean_n
中值滤波 median_image median_rect
Rank滤波(秩序值滤波) rank_image rank_rect
高斯滤波 gauss_filter
图像平滑 smooth_image
图像增强
均值增强 emphasize illuminate
一致性扩散增强 coherence_enhancing_diff
比例增强 scale_image_max
冲击滤波 shock_filter
图像算术
图像加法 add_image
图像减法 sub_image
图像乘法 milt_image
图像除法 div_image
图像翻转 inner_image
图像灰度取大 max_image
图像灰度取小 min_image
绘图比例拉伸 scale_image
图像几何变换
仿射变换 affine_trans_image
镜像变换 mirror_image
透射变换 projective_trans_image projective_trans_image_size
极坐标变换 polar_trans_image_ext
图像旋转 rotate_image
缩小图像 zoom_image_size
3图像分割
4区域处理
集合运算
complement
difference
intersection
symm_difference
union1
union2
几何运算
affine_trans_image
mirror_region
move_region
polar_trans_region
polar_trans_region_inv
projective_trans_image
transpose_region
zoom_region
区域变换
background_seg
clip_region
clip_region_rel
closest_point_transform
connection
distance_transform
eliminate_runs
expand_region
fill_up
fill_up_shape
hamming_change_region
interjacent
junctions_skeletion
merge_regions_line_scan
partion_dynamic
partion_rectangle
rank_region
remove_noise_region
shape_trans
skeleton
split_skeleton_lines
split_skeleton_region
特征过滤
area_center
area_holes
circularity
compactness
connect_and_holes
connection
contlength
convexity
diameter_region
eccentricity
elliptic_axis
euler_number
find_neighbors
get_region_index
get_region_thickness
hamming_distance
inner_circle
inner_rectangle
moments_region_2nd
moments_region_2nd_invar
moments_region_center_invar
orientation_region
rectangularity
region_features
roundness
runlength_distribution
runlength_features
select_region_points
select_region_spatial
select_shape
select_shape_proto
select_shape_std
smallest_circle
smallest_rectangle1
smallest_rectangle2
spatial_relation
形态学
boundary
closing
closing_circle
closing_rectangle1
dialation_circle
dialation_rectangle1
dilation1
erosion_circle
erosion_recrangle1
gen_struct_elements
opening
opening_circle
opening_rectangle
top_hat
5特征提取
定位特征找出数据
p38
https://www.bilibili.com/video/BV1x5411H7Ns?p=38&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
平滑处理
mean_image()
edges_sub_pix(...) 边缘提取
edges 边缘
rank_rect 等级
gauss_filter 高斯滤波 对周围对象进行加权平均值
p39
https://www.bilibili.com/video/BV1x5411H7Ns?p=39&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
图像增强
边缘起伏不大的增强 黑的更黑 白的更白
emphasize(...) 增强模式
coherence_enhancing_diff
min_max_gray(...) 获取最大最小灰度值
sub_image 可用于一个产品两次点胶前后判断,加工前后判断
p42
gray_erosion_rect(…) 类似秩序值滤波
gray_disc_se
gray_closing
gray_closing_rect
gray_closing_shape
gray_dilation_rect
gray_dilation_shape
gray_erosion_rect
gray_erosion_shape
gray_opening
gray_opening_shape
gray_opening_rect
gray_rangge_rect
gray_tophat
p43
图像截取合并
抠图
crop_part(...) 坐标不是原图
reduce_domain(...) 坐标还是原图
拼图
tile_images_offset
tile_images
校验畸变
桶状畸变
枕形畸变
畸变调整
change_rafial_distortion_image(...)
https://www.bilibili.com/video/BV1x5411H7Ns/?p=43&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p44
图像灰度特征
paint_region(...) 将某区域的灰度值重新填充新的灰度值
paint_gray(...) 把一幅图像的灰度填充到另一幅图中生成新的图像
select_gray(...) 灰度区域特征选择
entropy 熵 - 体系混乱程度的变量
https://www.bilibili.com/video/BV1x5411H7Ns?p=44&vd_source=9ed5e7be5d8180766946b38de14a1781
c++面试题
https://www.bilibili.com/video/BV1CE411r7oj/?p=34&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
intensity(...) 平均灰度和方差
area_center_gray(...)
gray_histo(...) 灰度直方图
gen_region_histo(....)
p45
使用相机的sdk进行开发
HAcq开头都是相机的文件
p46
重点:介绍相机的SDK开发流程
调用相机的sdk --稳定性和速度
项目初期使用HALCON 到后期使用相机的sdk 提高速度和稳定性
1安装相机
1)装相机驱动(常用接口 usb2.0,usb3.0,GIGE,CAMLINK,1394A,1394B
2)若有采集卡,还需要安装卡的驱动
3)测试驱动是否找到设备
4)测试厂商提供的测试软件
5)找到相机的SDK(dll,lib等),并测试提供的Demo
2试用相机
1)用厂商的采集软件打开相机抓图
2)用halcon的采集助手抓图
3相机的设置配置
1)分辨率和感兴趣区域
2)曝光(曝光时间第可用提高速度)及增益
3)帧率(1s采集多少图)
4)白平衡(彩色相机需要白平衡)
5)触发方式(连续、软触发、硬触发)
4看提供的例程,看抓图流程并自己写代码采图,然后转成halcon的HObject变量
1)相机厂商一般会提供MFC或者console例程
2)选择一个可用采图的程序打开运行调试,分析执行过程
3)自己创建一个新的从新开始加入头文件,库文件,及动态链接库进行采图测试
4)获取图像指针,使用halcon函数gen_image1_extern,gen_image3_extern等函数转成HObject变量并将其显示在窗口上
5使用单张采图模式将相机SDK中需要使用的代码接口做到自己的项目中
1)先调试demo中的Basic项目
p47
写一点调试一点
p48
回调函数采图
1回调函数概念
2先分析SDK中使用回调函数进行采图
1)找到使用回调函数的例子Basic,并调试看结果
3将SDK中的代码转码成自己工程中进行采图
1)添加回调函数
2)在初始化时登记回调函数到驱动
3)在回调函数中将采集的图像数据转成halcon变量
4)在抓图中直接使用图像变量,建议使用事件同步
p50
代码复用+拓展
p51
尽量选择畸变少的镜头,尽量不矫畸变
p53
使用标定板进行畸变矫正
p55
p58
crop_part 取一部分
threshold_sub_pix(...) 阈值取边缘 轮廓
convex 凸性
contours 轮廓
平滑轮廓 smooth_contours_xld
形状变换 shape_trans_xld
封闭轮廓 close_contours_xld
对轮廓排序 sort_contours_xld
修剪出矩形区域的轮廓 clip_contours_xld
修剪出轮廓两端 clip_end_points_contours_xld
按直线和圆弧的特性分割轮廓 segment_contours_xld
生成多边形 gen_contours_polygon_xld
对象组合 concat_obj
高斯轮廓线 line_gauss()
adjacent 临近的
colinear 共线的
cocircular 共圆的
cotangential 余切的
straight 直的
合并端点临近的轮廓: union_adjacent_contours_xld
合并位于同一条直线上的轮廓 union_colinear_contours_xld
合并属于同一圆上的轮廓 union_cocircular_contours_xld
合并具有切线关系的轮廓 union_cotangential_contours_xld
合并具有相似方向的轮廓 union_straight_contours_xld
拟合为圆 fit_circle_contour_xld
拟合为直线 fit_line_contour_xld
拟合为矩形2 fit_rectangle2_contour_xld
regress_counter(...)
p59
https://www.bilibili.com/video/BV1x5411H7Ns?p=59&vd_source=9ed5e7be5d8180766946b38de14a1781
clip_region_rel 减区域
p60
https://www.bilibili.com/video/BV1x5411H7Ns?p=60&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p61
获取轮廓坐标 get_counter_xld
获取轮廓点的特征 get_countour_attrib_xld
保存轮廓 write_contour_xld_dxf
读取轮廓 read_contour_xld_dxf
p62
对轮廓进行仿射变换,CAD的图纸变为halcon的像素点 affine_trans_contour_xld
实际例子
read_contour_xld_dxf(...)
初始化一个空的仿射变换矩阵 hom_mat2d_identity(...)
缩放 hom_mat2d_scale(...)
仿射轮廓 affine_trans_contour_xld(...)
二维仿射变换,顾名思义就是在二维平面内,
将对象进行平移、旋转、缩放等变换的行为(还有其他的变换,
这里仅论述这最常见的三种)。
create_shape_model_xld(...) 创建模板
find_shape_model_xld(...) 查找模版
1、仿射变换的大致流程:
① 通过hom_mat2d_identity初始化一个空的仿射变换矩阵(即[1.0, 0.0, 0.0, 0.0, 1.0, 0.0]);
② 在初始化空的仿射矩阵中增加模板和目标之间的平移、旋转和缩放关系,
用到的算子包括hom_mat2d_translate(平移)、hom_mat2d_rotate(旋转)、
hom_mat2d_scale(缩放)等生成仿射变换矩阵(这几个算子可以叠加或者重复使用)
③ 得到模板和目标的仿射变换矩阵之后,可以对图像、区域或XLD执行仿射变换,
用到的算子包括affine_trans_image(仿射图像)、affine_trans_region(仿射区域)、
affine_trans_contour_xld(仿射轮廓)。
rigid 刚性
根据两个以上点对计算计算刚性仿射变换矩阵,支持旋转和平移 vector_to_rigid(::Px,Py,Qx,Qy:HomMat2D)
根据两个以上点对计算相似仿射变换矩阵,支持旋转、平移和缩放 vector_to_similarity(::Px,Py,Qx,Qy:HomMat2D)
双MARK定位 模板匹配
https://www.bilibili.com/video/BV1x5411H7Ns?p=62&vd_source=9ed5e7be5d8180766946b38de14a1781
p63
polar_trans_contour_xld_inv
轮廓的几何变换
生成对应的平行轮廓 gen_parallel_contour_xld
极坐标转换 polar_trans_contour_xld_inv
投射转换 projective_trans_contour_xld
轮廓的集合运算
闭合轮廓的交集 intersection_closed_contours_xld
闭合轮廓的差 difference_closed_contours_xld
合并闭合轮廓 union2_closed_contours_xld
p64
https://www.bilibili.com/video/BV1x5411H7Ns/?p=64&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
一维测量的方法
重复精度
distance_cc 轮廓到轮廓的距离,c是轮廓的意思
distance_sc 计算线段与轮廓之间的距离
distance_lc 直线到轮廓的距离
distance_pc 点到轮廓的距离
两个直线角度 angle_II(...)
angle_lx(...)
deg(...) 将弧度角转为度数
p65
使用测量助手进行测量
加载图像
绘制测量对象
参数设置
代码生成
gen_measure_rectangle(...)
measure_oairs(...)
P65
p66
测量算子
创建测量对象 gen_measure_rectangles 和 gen_measure_arc (arc-弧长)
提取直线边缘对 measure_pairs(结果成对出现)
提取边缘位置 measure_pos(结果顺序出现)
释放测量工具 close_measure
p67
https://www.bilibili.com/video/BV1x5411H7Ns/?p=67&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
客户需求分析 — 内容分析
视觉单看法
1项目工程师能与客户验收负责人确认需求,为什么要放在第一位呢,是因为我们接单
拿到的往往不是最终用户给我们的需求,而是设备中间商或者业务转话,传过来的,这样的话就会出现误传或者错传,甚至丢失
信息,进而导致在做设计的时候并不是得到最终需求,需求没有清湖明白的结果只有两个:1)不停的改和商务斡旋,最终勉强成交
2)改到无能为力,直到将工程师干辞职,项目放弃。所以不管什么产品,需求是第一位的。
p68
69
https://www.bilibili.com/video/BV1x5411H7Ns/?p=69&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p70
p71模板匹配
1模板匹配原理
1)什么是模板匹配
first_example_shape_matching.hdev
print+check
find_ncc_model_exposure
2)模板匹配流程
2模板参数介绍
1)图像金字塔 Numberlever
2)起始角度 AngleStart
3)角度范围 AngleExtern
4)角度步长 AngleStep
5)优化算法 Optimization
6)极性 Metric
7)对比度 Contrast
8)最小对比度 MinContrast
9)缩放比 scale(find_scale_shape_model.hdev)
3查找模板参数
1)最小匹配分数 Score
2)匹配最大个数 NumMatches
3)最大重叠比 MaxOverlap(1-允许重叠 0-不允许重叠)
4)亚像素精度 SubPixel
5)搜索时使用的金字塔层数 NumLevels(0-最高效)
6)贪婪度 Greediness(0-慢而安全 1快且可能匹配失败)
4模板匹配与仿射变换进行定位(rectify_result.hdev)
注意模板的对称性,对仿射变换的影响
5模板匹配与仿射变换进行缺陷检测
inspect 检查
从点和角度计算刚性仿射变换 vector_angle_to_rigid
p73
https://www.bilibili.com/video/BV1x5411H7Ns/?p=73&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p74
P75
P76
基于形状的模板匹配
1)创建边缘模板 create_shape_model
2)创建边缘轮廓模板 create_shape_model_xld
3)查找边缘模板 find_shape_model
4)创建具有缩放比的模板 create_scaled_shape_model
5)查找具有缩放比的模板 find_scale_shape_model
6)查看模板图像和模板区域 inspect_shape_model
7)获取模板轮廓 get_shape_model_contours
8)获取模板的位置 get_shape_model_region
9)保存模板 write_shape_model
10)读取模板 read_shape_model
11)清除模板 clear_shape_model
基于相关性的模板匹配
1)创建模板 create_ncc_model
2)find_ncc_model
基于灰度的模版匹配
1)最小匹配分数 Score
https://www.bilibili.com/video/BV1x5411H7Ns/?p=76&spm_id_from=pageDriver&vd_source=9ed5e7be5d8180766946b38de14a1781
p81
坐标系标定
坐标系之间的转换,求两个坐标系之间的关系,比例系数
p82
vector_to_mat2d(…)
vector_to_rigid(…)
//#define USING_FILE
#define USING_HKVISION
#ifdef USING_FILE
#endif
#ifdef USING_HKVISION
#endif
halcon
1区域 --像素 region
2轮廓 --亚像素 xld