OpenCV中文网站例程——多目标模板匹配

多目标模板匹配结果如图——



程序如下——

#include <iostream>   
#include "cv.h"   
#include "cxcore.h"   
#include "highgui.h"   
using namespace std;  
  
CvPoint getNextMinLoc(IplImage *result, CvPoint minLoc, int maxVaule, int templatW, int templatH)  
{  
  
    // 先将第一个最小值点附近两倍模板宽度和高度的都设置为最大值防止产生干扰   
    int startX = minLoc.x - templatW;  
    int startY = minLoc.y - templatH;  
    int endX = minLoc.x + templatW;  
    int endY = minLoc.y + templatH;  
    if(startX < 0 || startY < 0)  
    {  
        startX = 0;  
        startY = 0;  
    }  
    if(endX > result->width - 1 || endY > result->height - 1)  
    {  
        endX = result->width - 1;  
        endY = result->height - 1;  
    }  
    int y, x;  
    for(y = startY; y < endY; y++)  
    {  
        for(x = startX; x < endX; x++)  
        {  
            cvSetReal2D(result, y, x, maxVaule);  
        }  
    }  
    // 然后得到下一个最小值并且返回   
    double new_minVaule, new_maxValue;  
    CvPoint new_minLoc, new_maxLoc;  
    cvMinMaxLoc(result, &new_minVaule, &new_maxValue, &new_minLoc, &new_maxLoc);  
    return new_minLoc;  
  
}  
int main()  
{  
    IplImage *src = cvLoadImage("E:\\shili\\match_src.jpg", 0);  
    IplImage *srcResult = cvLoadImage("E:\\shili\\match_src.jpg", 3);  //用来显示   
    IplImage *templat = cvLoadImage("E:\\shili\\match_template.jpg", 0);  
    IplImage *result;  // 用来存放结果   
    if(!src || !templat)  
    {  
        cout << "打开图片失败" << endl;  
        return 0;  
    }  
    int srcW, srcH, templatW, templatH, resultH, resultW;  
    srcW = src->width;  
    srcH = src->height;  
    templatW = templat->width;  
    templatH = templat->height;  
    if(srcW < templatW || srcH < templatH)  
    {  
        cout << "模板不能比原图小" << endl;  
        return 0;  
    }  
    resultW = srcW - templatW + 1;  
    resultH = srcH - templatH + 1;  
    result = cvCreateImage(cvSize(resultW, resultH), 32, 1);    //  匹配方法计算的结果最小值为float   
    cvMatchTemplate(src, templat, result, CV_TM_SQDIFF);     //方差最小,匹配最好
    double minValue, maxValue;  
    CvPoint minLoc, maxLoc;  
    cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);    
    cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y+ templatH), cvScalar(0,0,255));  
    CvPoint new_minLoc;  
  
    // 计算下一个最小值   
    new_minLoc = getNextMinLoc(result, minLoc, maxValue, templatW, templatH);  
    cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));  
    // 再下一个   
    new_minLoc = getNextMinLoc(result, new_minLoc, maxValue, templatW, templatH);  
    cvRectangle(srcResult, new_minLoc, cvPoint(new_minLoc.x + templatW, new_minLoc.y+ templatH), cvScalar(0,0,255));  
    cvNamedWindow("srcResult", 1);  
    cvNamedWindow("templat", 1);  
    cvShowImage("srcResult", srcResult);  
    cvShowImage("templat", templat);  
    cvWaitKey(0);  
    cvReleaseImage(&result);  
    cvReleaseImage(&templat);  
    cvReleaseImage(&srcResult);  
    cvReleaseImage(&src);  
    return 0;  

你可以使用OpenCV中的多目标模板匹配来实现这个功能。多目标模板匹配是一种在图像中同时匹配多个目标的技术。下面是一个简单的示例代码,展示了如何使用多目标模板匹配: ```python import cv2 import numpy as np # 加载目标图像和模板图像 target_image = cv2.imread('target_image.jpg') template_image = cv2.imread('template_image.jpg') # 获取目标图像和模板图像的宽度和高度 target_height, target_width = target_image.shape[:2] template_height, template_width = template_image.shape[:2] # 使用多目标模板匹配算法进行匹配 result = cv2.matchTemplate(target_image, template_image, cv2.TM_CCOEFF_NORMED) # 设置匹配阈值 threshold = 0.8 # 找到所有匹配的位置 locations = np.where(result >= threshold) # 在目标图像中绘制矩形框标记匹配的位置 for point in zip(*locations[::-1]): cv2.rectangle(target_image, point, (point[0] + template_width, point[1] + template_height), (0, 255, 0), 2) # 显示结果 cv2.imshow('Result', target_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上述代码中,我们首先加载目标图像和模板图像,然后使用`cv2.matchTemplate()`函数进行多目标模板匹配。然后,我们设置一个匹配阈值,将匹配得分高于阈值的位置作为匹配的结果。最后,我们在目标图像中绘制矩形框来标记匹配的位置。 你只需将目标图像和模板图像替换为你自己的图像,并根据需要调整阈值即可。希望这能帮到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值