一.问题描述
用halcon形状模版匹配,红色矩形框是搜索范围,ROI 矩形框中间的是训练的模版,按理说应该只会匹配到ROI中中间的那个为什么会搜到搜索区域之外的部分,而且匹配分数还很高,即模板在搜索区域外仍能匹配成功。
二.原因分析
使用reduce_domain裁切搜索区域部分的图像时,只是将搜索区域以外的图像遮盖了,并不是彻底消失了。所以在参数设置没问题,匹配分数依然很高的情况下,只要模板的重心还在搜索Roi内,依然能搜索模板。
三.解决措施
将搜索区域以外的图像彻底遮盖掉,这样即使模板的重心还在搜索Roi内,也不能匹配到搜索区域以外的图像了。
Halcon代码:
dev_close_window () dev_open_window (0, 0, 648, 512, 'black', WindowHandle) read_image (Image, '图像1.png') *建模 *draw_region (OutRegion, WindowHandle) *draw_region (InRegion, WindowHandle) *difference (OutRegion, InRegion, RegionDifference) read_region (RegionDifference, 'ModelRegion.hobj') reduce_domain (Image, RegionDifference, ImageReduced1) create_shape_model (ImageReduced1, 5, 0, 0, 'auto', 'auto', 'ignore_global_polarity', 'auto', 50, ModelID2) *匹配 read_image (Image, '图像1.png') *draw_circle (WindowHandle, Row2, Column2, Radius) gen_circle (SerRegion, 433, 620, 100) ***↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*** *这里取消掉reduce_domain得到搜索图像,而是采用paint_region方式 *reduce_domain (Image, SerRegion, ImageReduced3) get_domain (Image, Domain) difference (Domain, SerRegion, RegionDifference1) paint_region (RegionDifference1, Image, ImageReduced3, 1, 'fill') ***↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*** find_shape_model (ImageReduced3, ModelID2, -0.39, 0.79, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row7, Column7, Angle3, Score3) get_shape_model_contours (ModelContours4, ModelID2, 1) vector_angle_to_rigid (0, 0, 0, Row7, Column7, Angle3, HomMat2D) affine_trans_contour_xld (ModelContours4, ContoursAffineTrans, HomMat2D) dev_display (Image) dev_set_color ('red') dev_display (SerRegion) dev_set_color ('green') dev_set_line_width (2) dev_display (ContoursAffineTrans)
C++源码:
if (m_RegionSer.AreaCenter(&HTuple(), &HTuple()) > 0) { //_reduceImg = _inImg.ReduceDomain(_inRegion); HObject Domain, RegionDifference; GetDomain(m_TriggerImg, &Domain); Difference(Domain, m_RegionSer, &RegionDifference); PaintRegion(RegionDifference, m_TriggerImg, &imgReduced, 0, "fill"); } else { imgReduced = m_TriggerImg; }
戳戳小手帮忙点个免费的赞吧,嘿嘿。 |