Ask Your Question
0

Template matching in webcam

asked 2014-01-08 15:50:24 -0600

updated 2020-11-02 17:17:17 -0600

Hello there, i have a question.

i have a program that does simple template matching. My question is, i want to make the source from my webcam and the result is showed on my webcam. Here's my code

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main() 
{
    cv::Mat ref = cv::imread("reference.png");
    cv::Mat tpl = cv::imread("template.png");
    if (ref.empty() || tpl.empty())
        return -1;

    cv::Mat gref, gtpl;
    cv::cvtColor(ref, gref, CV_BGR2GRAY);
    cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);

    cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
    cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
    cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);

    while (true) 
    {
        double minval, maxval, threshold = 0.8;
        cv::Point minloc, maxloc;
        cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);

        if (maxval >= threshold)
        {
            cv::rectangle(
                ref, 
                maxloc, 
                cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows), 
                CV_RGB(0,255,0), 2
            );
            cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
        }
        else
            break;
    }

    cv::imshow("reference", ref);
    cv::waitKey();
    return 0;
}

Thank you for your attention. I wait for any of your response.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-01-08 16:00:58 -0600

unxnut gravatar image

You need to move your image acquisition from webcam and template matching into the while loop. You should also have some way to terminate the loop. For example, you could make use of waitKey() to get a character from screen to terminate the loop.

edit flag offensive delete link more

Comments

how to acquisite the image from webcam ?

Kevin Phytagoras gravatar imageKevin Phytagoras ( 2014-01-09 03:02:14 -0600 )edit

Question Tools

Stats

Asked: 2014-01-08 15:50:24 -0600

Seen: 918 times

Last updated: Jan 08 '14