MFC调用halcon实现几何匹配

来源:互联网 发布:数据分析 java 编辑:程序博客网 时间:2024/05/16 19:05


1、使用halcon助手快速导出几何匹配实现流程

1)选择助手->打开新Matching

2)选择创建模板,加载图片,比如D:/1.bmp

3)选择绘制轴平行矩形,生产ROI

4)选择代码生成,将产生如下代码


* Matching 01: BEGIN of generated code for model initialization
set_system ('border_shape_models', 'false')
* Matching 01: Obtain the model image
read_image (Image, 'D:/1.bmp')
* Matching 01: build the ROI from basic regions
gen_rectangle1 (ModelRegion, 234.167, 234.833, 293.722, 351.5)
* Matching 01: reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* Matching 01: create the shape model
create_shape_model (TemplateImage, 4, rad(0), rad(360), rad(1.7774), ['none','no_pregeneration'], 'use_polarity', [34,42,4], 10, ModelId)
* Matching 01: store the model contour for displaying the search results later
get_shape_model_contours (ModelContours, ModelId, 1)
* Matching 01: END of generated code for model initialization
* Matching 01: BEGIN of generated code for model application
* Matching 01: the following operations are usually moved into that loop where the aquired images are processed
* Matching 01: Find the model

find_shape_model (Image, ModelId, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 4, 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
* Matching 01: transform the model contours into the detected positions
for MatchingObjIdx := 0 to |ModelScore| - 1 by 1
    hom_mat2d_identity (HomMat)
    hom_mat2d_rotate (HomMat, ModelAngle[MatchingObjIdx], 0, 0, HomMat)
    hom_mat2d_translate (HomMat, ModelRow[MatchingObjIdx], ModelColumn[MatchingObjIdx], HomMat)
    affine_trans_contour_xld (ModelContours, TransContours, HomMat)
    dev_display (TransContours)
endfor
* Matching 01: Clear model when done
clear_shape_model (ModelId)
* Matching 01: END of generated code for model application

以上代码包含了几何匹配需要用到的主要函数,包含了几何匹配实现的主要流程。接下来可以根据实际需要,这些函数添加到MFC中,

在自身应用环境中实现几何匹配。


2、匹配参数设置

1)匹配参数结构体

typedef struct _struct_Tool_Param_Geo_Mattch

{

stDoubleRect rcModelRect;//Model搜索区域

stDoubleRect rcROIRect;//ROI搜索区域

}


2)Model搜索区域获取

//HTuple* windowHandle;窗口句柄

set_color(*windowHandle,"green“);

stDoubleRect m_stTemRect;

Halcon::draw_rectangle1(*windowHandle,&(m_stTemRect.Row1),&(m_stTemRect.Col1),&(m_stTemRect.Row2),&(m_stTemRect.Col1));

获取的矩形区域存于m_stTemRect中。ROI搜索区域的获取方式相同。


3)创建模板

gen_rectangle1(&rect,rcModelRect.Row1,rcModelRect.Col1,,rcModelRect.Row2,rcModelRect.Col2);

reduce_domain(*pImageForModel,rect,&Image);

crop_domain(Image,&m_pStToolParam_GeoMatch->tModelImage);

create_shape_model(Image,"auto",m_pStToolParam_GeoMatch->dAngleStart,

m_pStToolParam_GeoMatch->dAngleExtent,"auto","auto","use_polarity","auto","auto",&m_pStToolParam_GeoMatch->tModelID);


4)ROI获取

gen_rectangle1(&rect,rcRORect.Row1,rcROIRect.Col1,,rcROIRect.Row2,rcROIRect.Col2);

reduce_domain(*pImageToSearch,rect,&Image);


5)匹配搜索

find_shape_model(ImageROI,m_pStToolParam_GeoMatch->tModelID,

m_pStToolParam_GeoMatch->dAngleStart,m_pStToolParam_GeoMatch->dAngleExtent,0.8,1,0.2,

"interpolation",2,0.5,&m_pStToolParam_GeoMatch->tPosRow,&m_pStToolParam_GeoMatch->tPosCol,

&m_pStToolParam_GeoMatch->tAngle,&m_pStToolParam_GeoMatch->tScore);


6)结果显示

get_shape_model_contours (&ModelContours, HTuple(m_pStToolParam_GeoMatch->tModeID[Index]), 1);

hom_mat2d_identity (&HomMat2DIdentity);

hom_mat2d_scale (&HomMat2DIdentity,HTuple(1.0),HTuple(1.0),0,0,&HomMat2DScale);

hom_mat2d_rotate (HomMat2DIdentity, HTuple(m_pStToolParam_GeoMatch->tAngle),0,0,&HomMat2DRotate);

hom_mat2d_translate (HomMat2DRotate,HTuple(m_pStToolParam_GeoMatch->tPosRow),HTuple(m_pStToolParam_GeoMatch->tPosCol),&HomMat2DTranslate);

affine_trans_contour_xld (ModelContours, &ContoursAffinTrans, HomMat2DTranslate);

disp_obj(ContoursAffinTrans,*pWnd);

0 0
原创粉丝点击