传统图像和深度学习最本质的区别在于传统图像通过一系列的方法获取目标,包括滤波、阈值、二值化、灰度梯度变化等。但本质上,他们都是在获取图像特征,即对特征进行表述。深度学习是通过大量的数据得到对特征的表述,所以网络越叠越大。随着训练数据的增加模型也越来越大。而传统方法则泛化性极差,曝光时间、稍微的变形或干扰都有可能导致对目标的错误输出,所以很多时候传统图像处理都是在调参过程中完成开发。但是,不管哪种方法都有其具体的特定应用场景,都是在对特征的筛选、匹配、计算以及定位。所以,本篇主要讲一讲基于halcon的特征表描述方法。
一、一般的特征表述方法:
狭义的特征可以指代图像中的某个特定的区域。基于这个出发点,在halcon中,获取Region、XLD的方式都是一种获取特征的方式。但是这种方式可能并不是很稳定,它通常会收到曝光度,干扰,变形,缺失等因素的影响。同时,特征在halcon中可以通过矩阵的形式存储及表述,矩阵实际上获取的是像素间的关联特性,可以是1、2、3阶矩也可以是长度,宽度,面积,矩形度,圆度等。废话不多说,直接上例子:
例1:基于Region的特征获取
read_image(Image,'C:/Users/Desktop/test2.jfif')
rgb1_to_gray (Image, grayImage)
copy_image (grayImage, imgG)
copy_image (grayImage, imgB)
compose3 (grayImage, imgG, imgB, rgbImage)
binary_threshold(grayImage,TargetRegion,'smooth_histo', 'dark', UsedThreshold)
connection(TargetRegion,ConnectRegion)
select_shape(ConnectRegion,SelectRegion,'area','and',1000,15000)
原图
原图链接
结果
例2:基于XLD的特征获取
结果:
代码
read_image(Image,'C:/Users/Desktop/test2.jfif')
gen_ellipse (ROI_0, 212.196, 162.996, rad(167.022), 111.16, 49.2608)
reduce_domain(Image,ROI_0,AirPlane)
edges_color_sub_pix(AirPlane,Edage,'canny', 1.5, 20, 40)
*select_shape_xld()
select_shape_xld(Edage,PlaneXLD,'contlength','and',100,1000)
二、基于匹配的特征提取
halcon提供了多种匹配方式如下图所示:
模板匹配的基本步骤都相同,大致为:1、创建ROI 2、创建模板 3、获取及模型(可以将获取的模型保存在.shm文件中,下次用的时候就无需再次创建)4、匹配目标。详情见如下实例:
* This example shows how to use the
* adapt_shape_model_high_noise operator to improve the
* matching rate for images containing very high noise.
* The operator estimates the lowest pyramid level automatically
* based on a provided sample search image.
*
ImgPath := 'crosses/'
dev_update_off ()
read_image (Image, ImgPath + 'crosses_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_color ('lime green')
dev_set_line_width (3)
NumImages := 10
dev_disp_description_text ()
stop ()
get_window_param (WindowHandle, 'flush', Flush)
set_window_param (WindowHandle, 'flush', 'false')
flush_buffer (WindowHandle)
*
* 1. 创建模型并设置参数
*
* Generate a model contour for the use in create_shape_model_xld,
* create_scaled_shape_model_xld or create_aniso_shape_model_xld.
* The quality of the model contour is crucial.
*
*读取存在文件夹里的轮廓参数
read_object (ModelContour, 'model_contour')
*创建模型
create_scaled_shape_model_xld (ModelContour, 'auto', rad(-50), rad(100), 'auto', 0.75, 1.25, 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)
*获取模型轮廓
get_shape_model_contours (ModelContours, ModelID, 1)
gen_image_const (Image1, 'byte', 512, 512)
paint_region (Image1, Image1, ImageResult1, 255, 'fill')
paint_xld (ModelContour, ImageResult1, Image, 0)
find_scaled_shape_model (Image, ModelID, rad(-50), rad(100), 0.75, 1.25, 0.1, 5, 0.0, 'least_squares', 0, 0.0, Row, Column, Angle, Scale, Score)
Index := 0
dev_display_shape_matching_results (ModelID, 'lime green', Row[Index], Column[Index], Angle[Index], Scale[Index], Scale[Index], 0)
get_hom_mat2d_from_matching_result (Row[Index], Column[Index], Angle[Index], Scale[Index], Scale[Index], HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate)
set_shape_model_metric (Image, ModelID, HomMat2DTranslate, 'use_polarity')
*
read_image (Image, ImgPath + 'crosses_01')
dev_clear_window ()
dev_display (Image)
Text := 'Sample image with high level of noise. Many matches cannot'
Text[1] := 'be tracked to the lowest pyramid level in such images.'
dev_disp_text (Text, 'window', 'top', 'left', 'black', 'box', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* 2. Adapt the model based on a sample search image.
gen_rectangle1 (Rectangle, 97, 355, 222, 455)
reduce_domain (Image, Rectangle, ImageReduced)
dev_clear_window ()
dev_display (ImageReduced)
* Estimate lowest pyramid level.
* Provide a sample search image with a reduced domain that includes
* one match and some background. The operator adapts the model's
* lowest pyramid level.
adapt_shape_model_high_noise (ImageReduced, ModelID, 'lowest_level', ResultDict)
* Note that NumLevels now contains two values:
* The upper and lower pyramid levels.
get_shape_model_params (ModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
dev_disp_text ('Estimated lowest pyramid level: ' + NumLevels[1], 'window', 'top', 'left', 'black', 'box', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* 3. Use the model to search in new images.
for Index := 1 to NumImages by 1
read_image (Image, ImgPath + 'crosses_' + Index$'02')
*//匹配目标
find_scaled_shape_model (Image, ModelID, rad(-50), rad(100), 0.75, 1.25, 0.5, 5, 0.0, 'least_squares', 0, 0.0, Row, Column, Angle, Scale, Score)
*
dev_display (Image)
dev_display_shape_matching_results (ModelID, 'lime green', Row, Column, Angle, Scale, Scale, 0)
Text := 'Detection results in search image.'
dev_disp_text (Text, 'window', 'top', 'left', 'black', 'box', 'true')
Text := 'Image ' + Index + '/' + NumImages
dev_disp_text (Text, 'window', 'top', 'right', 'black', 'box', 'true')
flush_buffer (WindowHandle)
wait_seconds (1)
endfor
* Restore default window parameter for 'flush'.
set_window_param (WindowHandle, 'flush', Flush)
匹配模型的读取与保存:
不同的匹配模型读写方式可能不同,这里以ncc模板为例
*读取
read_ncc_model('test.shm',ModelID)
*写入
write_ncc_model(ModelID,'test.shm')
三、基于halcon的深度学习方法
1、对常见物体及生物的分类
2、识别与检测
3、对未定义过或库中没有物体及生物的分类
4、对文字、数字及字母的识别
(内容过多,见下一期)
总结
实际上无论是传统方法还是深度学习方法都是在做特征的准确表述相关工作,即特征的准确表述才是机器视觉的皇冠。而基于halcon的模型表述方法相比opencv更加通俗化。但是通俗化并不意味着它更加科学有效,毕竟特征千变万化,传统的特征表述方法在模块化的基础下很难具有泛化性及扩展性。但是就目前而言,传统的基于halcon的特征表述方式模块化的优势也非常明显。这大大降低了视觉相关从业人员在很多行业及岗位中的门槛。