Halcon 基础学习(2)

位置对准

1.模板不动,旋转图片:

//***(匹配01)
//* Matching 01: ************************************************
//* Matching 01: BEGIN of generated code for model initialization(模型初始化生成代码)
//* Matching 01: ************************************************

/*  HALCON系统参数的设置。  */
set_system ('border_shape_models', 'false')

//* (获取模型图像)
//* Matching 01: Obtain the model image

/*  读取有不同文件格式的图像。  */
read_image (Image, 'E:/桌面/第一讲/第一/bin_switch_1.png')

//* (在基本区域构建ROI)
//* Matching 01: Build the ROI from basic regions

/*  创建一个扫描宽度编码中的图像区域  */
gen_region_runs (ModelRegion,[143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226], [338,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,335,335,335,335,335,335,335,334,334,334,334,334,334,334,334,333,333,333,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,335,335,336,336,336,337], [340,354,358,361,362,363,366,367,368,370,373,375,376,377,378,379,380,381,385,386,387,392,392,393,396,397,397,400,406,407,411,412,413,413,413,413,413,413,413,413,413,413,413,412,411,410,410,409,408,408,406,405,404,402,401,399,399,397,395,395,394,393,391,390,389,385,384,383,382,371,368,365,364,362,358,357,356,354,354,353,351,348,347,345])

gen_region_runs (_TmpRegion, [208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235], [335,333,332,331,330,329,329,328,328,327,327,327,326,326,326,325,325,325,325,325,325,325,325,325,326,327,328,332], [338,337,339,341,343,345,345,346,347,347,347,348,348,348,348,348,348,348,347,347,347,347,347,346,345,343,340,339])

/*  计算两个区域的差距  */
difference (ModelRegion, _TmpRegion, ModelRegion)

gen_region_runs (_TmpRegion, [186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230], [394,392,390,390,389,389,389,388,389,387,385,382,379,378,376,374,372,371,367,366,363,360,358,355,354,353,352,351,351,351,350,350,349,349,349,349,349,349,349,349,349,349,349,350,352], [400,407,411,412,413,413,413,413,413,413,413,413,413,413,413,413,413,413,413,412,411,411,410,408,407,405,403,402,401,398,395,393,392,388,386,385,383,382,380,377,374,373,371,368,366])

difference (ModelRegion, _TmpRegion, ModelRegion)

gen_region_runs (_TmpRegion, [157,158,158,159,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185], [386,385,402,385,401,385,385,385,385,386,387,388,390,392,393,396,398,399,400,402,403,404,405,406,406,407,408,408,409,410,411], [393,397,405,399,409,411,412,413,414,415,415,416,416,417,418,419,419,419,420,420,420,420,420,420,420,420,420,420,419,419,418])

difference (ModelRegion, _TmpRegion, ModelRegion)

//* (减少模型模板)
//* Matching 01: Reduce the model template

/*  缩小一个图像的区域。  */
reduce_domain (Image, ModelRegion, TemplateImage)

//* (创建形状模型)
//* Matching 01: Create the shape model

/*  为匹配准备一个轮廓模型  */
create_shape_model (TemplateImage, 3, rad(0), rad(360), rad(2.3948), ['none','no_pregeneration'], 'use_polarity', [11,24,5], 4, ModelID)

//* (获取模型轮廓,以便稍后将其转换为图像)
//* Matching 01: Get the model contour for transforming it later into the image

/*  返回一个轮廓模型的轮廓表示  */
get_shape_model_contours (ModelContours, ModelID, 1)

//* (获取参考位置)
//* Matching 01: Get the reference position

/*  一个区域的面积(大小)和中心  */
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)

/*  创建任意方向的矩形  */
gen_rectangle2 (ROI_0, 392.75, 379.25, rad(-61.3524), 61.5325, 8.45894)

/*  列出目录中的所有文件  */
list_files ('E:/桌面/第一讲/第一', ['files','follow_links','recursive'], ImageFiles)

/*  选择符合公式的元组元素  */
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)

for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])

//***step1.校正图像
    
    /*  在一个图像中找出一个轮廓模型的最佳匹配  */
    find_shape_model (Image, ModelID, rad(0), rad(360), 0.6, 1, 0.5, 'least_squares', [3,1], 0.9, Row, Column, Angle, Score)  
    
    /*  从点和角度方面计算一个严格的仿射变换  */
    vector_angle_to_rigid(Row, Column, Angle, RefRow, RefColumn,0,HomMat2D)       
    
    /*  把任意仿射2D变换应用在图像中  */
    affine_trans_image (Image, RectifiedImage, HomMat2D, 'constant', 'false')   

//****step2.使用工具检测 
    
    /*  缩小一个图像的区域  */
    reduce_domain(RectifiedImage, ROI_0, ImageReduced)   
    
    /*  通过平均平滑一个图像  */
    mean_image(ImageReduced, ImageMean, 29, 29)
    
    /*  利用局域阀值分割图像  */
    dyn_threshold(ImageReduced,ImageMean, RegionDynThresh, 33, 'dark')   
    
    /*  关闭一个圆形结构基础的一个区域  */
    closing_circle(RegionDynThresh, RegionClosing, 5)
    
    /*  计算一个区域相连接的部分  */
    connection(RegionClosing, ConnectedRegions)
    
    /*  根据图形特征选择区域  */
    select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 40, 99999)
    
    /*  统计一个元组中的对象  */
    count_obj(SelectedRegions, Number)       
    
    /*  在现有图形窗口中显示图像目标  */
    dev_display(RectifiedImage)
    
    dev_display(SelectedRegions)
    
    /*  停止程序执行  */
    stop()
    
endfor

2.图片不动,旋转模板:

//* Matching 01: ************************************************
//* Matching 01: BEGIN of generated code for model initialization
//* Matching 01: ************************************************
set_system ('border_shape_models', 'false')
//* 
//* Matching 01: Obtain the model image
read_image (Image, 'E:/桌面/第一讲/第一/bin_switch_1.png')
//* 
//* Matching 01: Build the ROI from basic regions

gen_region_runs (ModelRegion, [143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226], [338,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,336,335,335,335,335,335,335,335,334,334,334,334,334,334,334,334,333,333,333,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,334,335,335,336,336,336,337], [340,354,358,361,362,363,366,367,368,370,373,375,376,377,378,379,380,381,385,386,387,392,392,393,396,397,397,400,406,407,411,412,413,413,413,413,413,413,413,413,413,413,413,412,411,410,410,409,408,408,406,405,404,402,401,399,399,397,395,395,394,393,391,390,389,385,384,383,382,371,368,365,364,362,358,357,356,354,354,353,351,348,347,345])

gen_region_runs (_TmpRegion, [208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235], [335,333,332,331,330,329,329,328,328,327,327,327,326,326,326,325,325,325,325,325,325,325,325,325,326,327,328,332], [338,337,339,341,343,345,345,346,347,347,347,348,348,348,348,348,348,348,347,347,347,347,347,346,345,343,340,339])

difference (ModelRegion, _TmpRegion, ModelRegion)

gen_region_runs (_TmpRegion, [186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230], [394,392,390,390,389,389,389,388,389,387,385,382,379,378,376,374,372,371,367,366,363,360,358,355,354,353,352,351,351,351,350,350,349,349,349,349,349,349,349,349,349,349,349,350,352], [400,407,411,412,413,413,413,413,413,413,413,413,413,413,413,413,413,413,413,412,411,411,410,408,407,405,403,402,401,398,395,393,392,388,386,385,383,382,380,377,374,373,371,368,366])

difference (ModelRegion, _TmpRegion, ModelRegion)

gen_region_runs (_TmpRegion, [157,158,158,159,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185], [386,385,402,385,401,385,385,385,385,386,387,388,390,392,393,396,398,399,400,402,403,404,405,406,406,407,408,408,409,410,411], [393,397,405,399,409,411,412,413,414,415,415,416,416,417,418,419,419,419,420,420,420,420,420,420,420,420,420,420,419,419,418])

difference (ModelRegion, _TmpRegion, ModelRegion)
//* 
//* Matching 01: Reduce the model template

reduce_domain (Image, ModelRegion, TemplateImage)
//* 
//* Matching 01: Create the shape model

create_shape_model (TemplateImage, 3, rad(0), rad(360), rad(2.3948), ['none','no_pregeneration'], 'use_polarity', [11,24,5], 4, ModelID)
//* 
//* Matching 01: Get the model contour for transforming it later into the image

get_shape_model_contours (ModelContours, ModelID, 1)

//* 
//* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
gen_rectangle2 (ROI_0, 392.75, 379.25, rad(-61.3524), 61.5325, 8.45894)
list_files ('E:/桌面/第一讲/第一', ['files','follow_links','recursive'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)

for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
//***step1.生成工具变换矩阵
    find_shape_model (Image, ModelID, rad(0), rad(360), 0.6, 1, 0.5, 'least_squares', [3,1], 0.9, Row, Column, Angle, Score)
        vector_angle_to_rigid(RefRow, RefColumn, 0, Row, Column, Angle,HomMat2D)

        (旋转识别区域)
        affine_trans_region(ROI_0, RegionAffineTrans, HomMat2D, 'nearest_neighbor')

    
    
//****step2.使用工具检测
    
    
    reduce_domain(Image, RegionAffineTrans, ImageReduced)
    
    mean_image(ImageReduced, ImageMean, 29, 29)
    dyn_threshold(ImageReduced,ImageMean, RegionDynThresh, 33, 'dark')
    
    
    
    closing_circle(RegionDynThresh, RegionClosing, 5)
    connection(RegionClosing, ConnectedRegions)
    select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 40, 99999)
    count_obj(SelectedRegions, Number)
    
    
    
    dev_display(Image)
    dev_display(SelectedRegions)
    stop()
    
endfor

a、将图片放入文件夹,导入halcon看一遍图

b、roi绘制所需图形

c、reduce_domain   缩小图片区域   ; threshold 二值化 (也可使用动态阈值   mean_image  先平滑  ,dyn_threshold 再设置动态阈值  );closing_circle 填充孔径 ;connection 连通域分析 ;select_shape  图形筛选 ; dev_display  选取所需图形  ;

d、助手/打开新的matching    形状匹配 ;dev_disp_text  显示文本 ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值