1

I found this source code for comparing images to a video it works sometimes but most of the times it doesn't and I get this error message.

"OpenCV Error: Assertion failed < == CV_8U :: img.depth<> == CV_32F> && img.type<> == templ.type<>> in cv::matchTemplate, file ........\opencv\modules\imgproc\src\templmatch.cpp, line 249"

I have no clue how to fix this..

Here is my source code can anyone point me in right direction?:

#include <iostream>
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>

#include <sstream>


using namespace cv;
using namespace std;

Point point1, point2; /* vertical points of the bounding box */
int drag = 0;
Rect rect; /* bounding box */
Mat img, roiImg; /* roiImg - the part of the image in the bounding box */
int select_flag = 0;
bool go_fast = false;

Mat mytemplate;

void track(cv::Mat &img, const cv::Mat &templ, const cv::Rect &r )
{
    static int n = 0;

    if (select_flag)
    {
        templ.copyTo(mytemplate);
        select_flag = false;
        go_fast = true;
    }


    cv::Mat result;
    /// Do the Matching and Normalize
    matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED );
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

    /// Localizing the best match with minMaxLoc
    double minVal; double maxVal; Point minLoc; Point maxLoc;
    Point matchLoc;

    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    matchLoc = minLoc;

    rectangle( img, matchLoc, Point( matchLoc.x + mytemplate.cols , matchLoc.y + mytemplate.rows ), CV_RGB(255, 255, 255), 3 );

    std::cout << matchLoc << "\n";
}

///MouseCallback function

void mouseHandler(int event, int x, int y, int flags, void *param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        /* left button clicked. ROI selection begins */
        point1 = Point(x, y);
        drag = 1;
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        /* mouse dragged. ROI being selected */
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
        imshow("image", img1);
    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        point2 = Point(x, y);
        rect = Rect(point1.x, point1.y, x - point1.x, y - point1.y);
        drag = 0;
        roiImg = img(rect);
    }

    if (event == CV_EVENT_LBUTTONUP)
    {
        /* ROI selected */
        select_flag = 1;
        drag = 0;
    }

}


///Main function

int main()
{
    int k;
    /*
        VideoCapture cap(0);
        if (!cap.isOpened())
        return 1;
    */

    VideoCapture cap;
    //cap.open("~/Downloads/opencv-2.4.4/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi");
    cap.open("./Megamind.avi");
    if (!cap.isOpened())
    {
        printf("Unable to open video file\n");
        return -1;
    }

    /*
        // Set video to 320x240
        cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
        cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
        */

    cap >> img;
    imshow("image", img);
    freopen("out.txt","w",stdout);
    while (1)
    {
        cap >> img;
        if (img.empty())
            break;

        if (rect.width == 0 && rect.height == 0)
            cvSetMouseCallback("image", mouseHandler, NULL);
        else
            track(img, roiImg, rect);

        if (select_flag == 1)
            imshow("Template", roiImg);

        imshow("image", img);
        k = waitKey(go_fast ? 30 : 10000);
        if (k == 27)
            break;

    }


    return 0;
}
RTNTVG
  • 53
  • 1
  • 2
  • 7
  • CV_8U and CV_32F are not comparable since the underlying data type is different. Please see [this question](http://stackoverflow.com/questions/8377091/what-are-the-differences-between-cv-8u-and-cv-32f-and-what-should-i-worry-about) for reference. – Baher Ramzy Oct 28 '14 at 17:39
  • As far as I know I use the same datatype, how could I check and change this in my code? – RTNTVG Oct 28 '14 at 23:07
  • Can you do an `imshow()` on `img` and `templ` right before calling `matchTemplate` in the first function of your code? – a-Jays Oct 29 '14 at 05:17

1 Answers1

1

I think the problem is, if suppose your mouseHandler() function is unable to execute last condition where you are setting select_flag = 1 variable, then your track() function would not execute this

if (select_flag)
{
    templ.copyTo(mytemplate);
    select_flag = false;
    go_fast = true;
}

and if it happens then

matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED ); 

function is going to match img which is CV_32F with mytemplate which is CV_8U because above if(select_flag) condition was not executed so nothing was copied in mytemplate. thats why you are getting this error [CV_8U != CV_32F], and as you said that it is running some time just because your handler some times work and some times not.

Michael Mairegger
  • 6,833
  • 28
  • 41
Rupesh Yadav.
  • 894
  • 2
  • 10
  • 24
  • 1
    and i think no need of using 4th "if" condition in handler, because you are setting select_flag =1, which you can put in 3rd "if" condition as they both are same event except (&& drag) situation in 3rd which doesn't look like helping much bcoz you are setting it 1 in first if. i hope i m clear ? :( – Rupesh Yadav. Oct 29 '14 at 06:46
  • Thanks for your answer, I think you were right the error message is gone but now I got another error and still it does work sometimes but most of the times it doesnt. – RTNTVG Oct 29 '14 at 10:38
  • Sorry I cant load the screenshot in this page heres the link hope you can take a look: http://s7.postimg.org/y3k6v6wrv/opencv.jpg – RTNTVG Oct 29 '14 at 10:44
  • the error is simply telling that you are passing some empty Mat to calculate something or resize some Mat say img and the coordinates you are passing is null or somthing.. hope help full – Rupesh Yadav. Oct 31 '14 at 11:28