2014-10-28 50 views
1

我發現這個源代碼用於比較圖像和它有時可用的視頻,但大多數時候它沒有,我得到這個錯誤信息。OpenCV錯誤信息(Matchtemplate)

「OpenCV的錯誤:斷言在CV失敗< == CV_8U :: img.depth <> == CV_32F> & & img.type <> == templ.type < >> :: matchTemplate,文件.. ...... \ OpenCV的\模塊\ imgproc的\ src \ templmatch.cpp,行249"

我不知道如何解決這個問題..

這裏是我的源代碼,任何人都可以點我正確的方向?:

#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; 
} 
+0

由於底層數據類型不同,因此CV_8U和CV_32F不具有可比性。請參閱[這個問題](http://stackoverflow.com/questions/8377091/what-are-the-differences-between-cv-8u-and-cv-32f-and-what-should-i-worry-about ) 以供參考。 – 2014-10-28 17:39:40

+0

據我所知我使用相同的數據類型,我怎麼能檢查和改變我的代碼? – RTNTVG 2014-10-28 23:07:43

+0

在代碼的第一個函數中調用'matchTemplate'之前,你可以在'img'和'templ'上執行'imshow()'嗎? – 2014-10-29 05:17:16

回答

1

我認爲問題是,如果假設你mouseHandler()功能無法 執行最後一個條件,你要設置select_flag = 1變量, 那麼你track()功能將無法執行此

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

,如果它發生,然後

matchTemplate(img, mytemplate, result, CV_TM_SQDIFF_NORMED); 

函數將要匹配img這是CV_32Fmytemplate這是因爲CV_8U以上if(select_flag)條件未執行,因此沒有任何內容在mytemplate中被複制。這就是爲什麼你得到這個錯誤[CV_8U != CV_32F],正如你所說,它運行一段時間,只是因爲你的處理程序有時工作,有時不工作。

+1

,我認爲沒有必要在處理程序中使用第4個「if」條件,因爲您正在設置select_flag = 1,您可以將其放在第3個「if」條件中,因爲它們都是相同的事件,除了(&& drag)看起來好像幫助很多bcoz,如果你先設置它1。我希望我明白嗎? :( – 2014-10-29 06:46:55

+0

感謝您的回答,我認爲你是正確的錯誤信息已經消失,但現在我得到了另一個錯誤,但它仍然有效,但它的大部分時間它does not。 – RTNTVG 2014-10-29 10:38:42

+0

對不起,我無法加載截圖在這個頁面heres the鏈接希望你可以看看:http://s7.postimg.org/y3k6v6wrv/opencv.jpg – RTNTVG 2014-10-29 10:44:39