这个例子其实是在比较两种ROI“还原”的方式;

模板匹配后,获得仿射变换矩阵T,则对于ROI的分析有两种:

1.全局的,将T.inv()*Img_target得到目标图片全局还原后的图片,在利用Region_Template,进行后续计算;

2.局部的,将T*Region_Template再对区域进行还原;

方法一计算量更大,但是Src简单,

* This example program shows how to rectify an image based on the matching
* results. The task is to extract and rectify the serial number on CDs.
* The recitification and extraction is shown in two different ways.
* In the first approach the full search image is rectified and then
* the numbers are extracted.
* In the second approach only the region of the numbers is rectified
* before the numbers are extracted.
*
*
dev_update_off ()
dev_close_window ()
*
* Read the model image and set visualization settings
read_image (ModelImage, 'cd_cover/cd_cover_01.png')
get_image_size (ModelImage, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (ModelImage)
disp_message (WindowHandle, 'Model image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Define the ROIs
* ---------------
* Select the ROI of the model object
Row1 := 96
Row2 := 149
Column1 := 87
Column2 := 179
gen_rectangle1 (ModelROI, Row1, Column1, Row2, Column2)
area_center (ModelROI, Area, CenterModelROIRow, CenterModelROIColumn)
dev_set_color ('blue')
dev_display (ModelROI)
disp_message (WindowHandle, 'ROI of the model', 'window', 40, 12, 'blue', 'true')
*
* Define the ROI of the numbers relative to the model object
gen_rectangle1 (NumberROI, Row2, Column1, Row2 + 30, Column2)
dev_set_color ('magenta')
dev_display (NumberROI)
disp_message (WindowHandle, 'ROI of the numbers', 'window', 60, 12, 'magenta', 'true')
stop ()
*
* Create the shape model
* -----------------------------------------------
reduce_domain (ModelImage, ModelROI, ImageReduced)
create_shape_model (ImageReduced, 4, 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
inspect_shape_model (ImageReduced, ShapeModelImage, ShapeModelRegion, 1, 50)
get_shape_model_contours (ShapeModel, ModelID, 1)
*
* Display the model contours
dev_set_color ('blue')
dev_set_line_width (1)
dev_display (ModelImage)
dev_display (ShapeModelRegion)
disp_message (WindowHandle, 'Model image', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Shape model', 'window', 40, 12, 'blue', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Display description
dev_clear_window ()
Message := 'The shape model has been successfully created.'
Message[1] := 'In the next step each image is searched for the'
Message[2] := 'best match of the shape model followed by a'
Message[3] := 'rectification based on the matching results'
Message[4] := 'and the extraction of the numbers.'
Message[5] := ' '
Message[6] := 'The rectification is demonstrated in two'
Message[7] := 'different ways:'
Message[8] := '- rectification of the full search image'
Message[9] := '- rectification of only the ROI of the numbers'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Find the object in each image and rectify the results
ImageFiles := 'cd_cover/cd_cover_'
for I := 1 to 4 by 1dev_set_line_width (1)* * Read the search image and search the best* match of the shape model in the search imageread_image (SearchImage, ImageFiles + I$'.2d')find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 0, 1, RowMatch, ColumnMatch, AngleMatch, Score)* * If a match was found, rectify the resultsif (|Score| > 0)* * Compute two affine transformations based on the position* of the new match:* --------------------------------------------------------* 'MovementOfModel' for the shape model and* 'MovementOfObject' for the rectification of the numbersvector_angle_to_rigid (0, 0, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfModel)vector_angle_to_rigid (CenterModelROIRow, CenterModelROIColumn, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfObject)* * Apply both affine transformations:* --------------------------------------------------------* one to the XLD contours of the shape model and* one to the ROI of the numbersaffine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfModel)affine_trans_region (NumberROI, NumberROIAtNewPosition, MovementOfObject, 'nearest_neighbor')* * Display the model ROI and the ROI of the numbers at the (new)* position in which the match was found in the current search imagedev_display (SearchImage)dev_set_color ('blue')dev_display (ModelAtNewPosition)dev_set_color ('magenta')dev_set_line_width (2)dev_display (NumberROIAtNewPosition)disp_message (WindowHandle, 'Search image ' + I, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, 'Found match', 'window', 40, 12, 'blue', 'true')disp_message (WindowHandle, 'ROI of the numbers', 'window', 60, 12, 'magenta', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Approach 1: Rectification of the full search image* *********************************************************** First the full search image is rectified. Then the numbers* are extracted.* *********************************************************** * Step 1: Rectify search image* ----------------------------* Invert the transformation matrix 'MovementOfObject' computed* for the rectification of the numbers so it can be used for the* rectification of the full search imagehom_mat2d_invert (MovementOfObject, InverseMovementOfObject)affine_trans_image (SearchImage, RectifiedSearchImage, InverseMovementOfObject, 'constant', 'false')* * Display the rectified search image and the ROI of the numbersdev_clear_window ()dev_display (RectifiedSearchImage)dev_display (NumberROI)disp_message (WindowHandle, '1. Approach: Rectified search image ' + I, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, 'ROI of the numbers', 'window', 40, 12, 'magenta', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Step 2: Extract the numbers* ---------------------------* Reduce the domain of the rectified image to the region of the* numbers and extract the numbers using a global gray value tresholdreduce_domain (RectifiedSearchImage, NumberROI, RectifiedNumberROIImage)threshold (RectifiedNumberROIImage, Numbers, 0, 128)connection (Numbers, IndividualNumbers)* * Display the extracted numbers in the reduced rectified imagedev_set_colored (12)dev_set_draw ('fill')dev_display (IndividualNumbers)disp_message (WindowHandle, 'Extracted numbers', 'window', CenterModelROIRow, Column1 - 50, 'black', 'true')stop ()* * Approach 2: Rectify only the number ROI* ************************************************************ In this approach the search image is first cropped to the* region of the numbers. The new cropped search image is then* rectified and the numbers are extracted.* ************************************************************ * Display the original search image (no rectification) and* the corresponding ROI of the numbersdev_set_draw ('margin')dev_set_color ('magenta')dev_display (SearchImage)dev_display (NumberROIAtNewPosition)* * Step 1: Crop the search image* -----------------------------* Compute the smallest rectangle surrounding the ROI* of the numbers parallel to the coordinate axessmallest_rectangle1 (NumberROIAtNewPosition, RowRect1, ColumnRect1, RowRect2, ColumnRect2)dev_set_color ('lime green')disp_rectangle1 (WindowHandle, RowRect1, ColumnRect1, RowRect2, ColumnRect2)disp_message (WindowHandle, '2. Approach: Crop search image ' + I, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, 'ROI of the numbers', 'window', 40, 12, 'magenta', 'true')* * Crop the image to the determined rectangle around the numberscrop_rectangle1 (SearchImage, CroppedSearchImage, RowRect1, ColumnRect1, RowRect2, ColumnRect2)* * Open a new window displaying the cropped image of the numbersWidth2 := ColumnRect2 - ColumnRect1 + 1Height2 := RowRect2 - RowRect1 + 1dev_open_window (0, Width + 10, Width2, Height2, 'black', CroppedWindowHandle)dev_set_part (0, 0, Height2 - 1, Width2 - 1)dev_display (CroppedSearchImage)disp_rectangle1 (CroppedWindowHandle, 0, 0, Height2, Width2)* * Display the corresponding messageMessage := 'Cropped image part'Message[1] := '(See also the upper right window)'disp_message (WindowHandle, Message, 'window', 65, 12, 'lime green', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Step 2: Rectifiy the cropped search image* ------------------------------------------* Prepare the transformation matrix needed for the* rectification. Add the translation of the cropping to* the transformation matrix and then invert the matrix.hom_mat2d_translate (MovementOfObject, -RowRect1, -ColumnRect1, MoveAndCrop)hom_mat2d_invert (MoveAndCrop, InverseMoveAndCrop)* * Rectify the cropped search image using the* inverted transformation matrixaffine_trans_image (CroppedSearchImage, RectifiedROIImage, InverseMoveAndCrop, 'constant', 'true')* * Display the rectified cropped search image in a new windowget_image_size (RectifiedROIImage, Width3, Height3)dev_set_part (0, 0, Height3 - 1, Width3 - 1)dev_open_window (Height2 + 60, Width + 10, Width3, Height3, 'black', WindowHandle1)set_display_font (WindowHandle1, 11, 'mono', 'true', 'false')dev_clear_window ()dev_display (RectifiedROIImage)disp_message (WindowHandle1, 'Rectified cropped image', 'window', 5, 5, 'black', 'true')stop ()* * Step 3: Extract the numbers* ---------------------------* Reduce the domain of the rectified and cropped image to the region of* the numbers and extract the numbers using a global gray value tresholdreduce_domain (RectifiedROIImage, NumberROI, RectifiedNumberROIImage)threshold (RectifiedNumberROIImage, Numbers, 0, 128)connection (Numbers, IndividualNumbers)dev_clear_window ()dev_display (RectifiedNumberROIImage)dev_set_colored (12)dev_set_draw ('fill')dev_display (IndividualNumbers)disp_message (WindowHandle1, 'Extracted numbers', 'window', 5, 5, 'black', 'true')stop ()endifif (I < 4)dev_set_window (CroppedWindowHandle)dev_close_window ()dev_set_window (WindowHandle1)dev_close_window ()endif
endfor
disp_end_of_program_message (WindowHandle, 'black', 'true')
*
* Clear the model
clear_shape_model (ModelID)

halcon基于形状的模板匹配第三例:rectify_result.hdev相关推荐

  1. Qt与halcon联合开发实现基于形状的模板匹配

    目录 前言 一.基于形状的模板匹配是什么? 二.具体实现 1.算子介绍 2.关键代码实现 总结 前言 第一次在CSDN写博客,准备写一个简单的形状匹配算子的用法及实现的介绍. 一.基于形状的模板匹配是 ...

  2. OpenCV基于形状的模板匹配

    OpenCV基于形状的模板匹配 引言 基于形状的匹配算法 具体代码 KcgMatch.h KcgMatch.cpp main.cpp 匹配的结果 引言 在OpenCV中有个用于模板匹配的基本函数mat ...

  3. OpenCV实现基于形状的模板匹配(附源码)

    效果预览 OpenCV实现基于形状的模板匹配(多角度+不同亮度) 实例演示一: 实例演示二: 实例演示三: 实例演示四: 实例演示五: 实例演示六ÿ

  4. HALCON基于形变的模板匹配实现

    基于形变的模板匹配 先看匹配结果: 下面是HALCON的代码,用匹配助手生成的,现在得到的就是模板上61个点的坐标,还有仿射矩阵,利用仿射矩阵可以算出61个点匹配上的像素坐标. * * Matchin ...

  5. 基于形状的模板匹配(用于字符识别)研究

    一.各模板匹配方法对比 1.基于灰度的匹配一般被称为模版匹配,直接以灰度进行匹配效果不好,对光照很敏感,所以一般会以灰度归一化互相关(NCC)作为匹配依据,增强光照变化下的鲁棒性,为了降低计算量,多采 ...

  6. HALCON联合C#检测表面缺陷——基于形状的模板匹配设置流程

    接上一篇文章,上两篇文章我们把匹配助手中生成的代码导出到C#文件,导出的过程如果有不清楚的可以给我留言. 上一节我们也说过,代码是分为两个部分的,前一部分是设置模板,后一部分是检测流程. 这里多说两句 ...

  7. 【Halcon】基于形状的模板匹配的定位.md

    看到一句非常入心的话:天雨大不润无根之草,道法宽只度有缘之人! 说的就是这个理,所有的因果都要追至事物的本质.从古至今,从国家到个人,如果只是华而不实.外强中干,就算机会.机遇在你身旁,你能做的也只能 ...

  8. 基于Halcon学习的基于灰度值模板匹配【一】exhaustive_match.hdev例程

    * 模板与图像的匹配 read_image (Image, 'fabrik') gen_rectangle1 (Rectangle, 365, 300, 390, 330) *将图像的定义域缩小为创建 ...

  9. HALCON基于形状匹配详解

    HALCON基于形状的模板匹配详细说明 很早就想总结一下前段时间学习HALCON的心得,但由于其他的事情总是抽不出时间.去年有过一段时间的集中学习,做了许多的练习和实验,并对基于HDevelop的形状 ...

最新文章

  1. PowerDesigner 15 进行 数据库反转到 数据库模型
  2. Load average in Linux的精确含义
  3. Android Hacks:在代码中隐藏软键盘
  4. 每小时的定时任务变成了每分钟
  5. JDK API实践:Spring怎样取舍Java I-O、集合、反射、动态代理等API的使用
  6. 昨日搬至办公室的书籍
  7. LVS-DR模型实现调度
  8. idea中 mybatis 的 mapper.xml 新建没有 头文件
  9. [转载] 算法导论:分治法,python实现合并排序MERGE-SORT
  10. Elastic Search Java Api 创建索引结构,添加索引
  11. error: not found: value sc
  12. 走进波分 -- 14.OSN902产品介绍
  13. 手机工商银行怎么转账_通过工行手机银行如何开通对外转账功能?
  14. 富文本编辑器CKEditor配置与使用
  15. 数据中心网络架构 — 网络带宽的收敛比
  16. PanDownload——最新修订版复活了。60MB/s,附下载地址
  17. 数据库底层原理-------数据结构
  18. 做第三方软件测评的意义
  19. 什么是DC,以及CreateCompatibleDC,CreateCompatibleBitmap,SelectObject的作用
  20. java冒泡排序(java冒泡排序经典代码)

热门文章

  1. 看Linus骂人,真解气
  2. C++堆空间和栈空间的区别
  3. 美国:19世纪的“山寨”大国
  4. 2017.3.29组合数学学习——帕斯卡三角形、二项式定理
  5. CopyTranslator——一个PDF文本翻译神器
  6. 数组c语言抓小偷,警察抓小偷C语言源码
  7. 友华光猫设置虚拟服务器,中兴光猫sendcmd常用命令
  8. 文字游戏编程作业-----王明哲
  9. 《TCPIP网络编程》学习笔记
  10. Shamir 门限秘密共享