Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实时模板匹配中的问题_C++_Opencv - Fatal编程技术网

C++ 实时模板匹配中的问题

C++ 实时模板匹配中的问题,c++,opencv,C++,Opencv,我正在尝试实时匹配模板。我找到了答案 代码是: #include <iostream> #include "opencv2/opencv.hpp" #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/objdetect/objdetect.hpp> #include <sstream> usi

我正在尝试实时匹配模板。我找到了答案

代码是:

#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;


///------- template matching -----------------------------------------------------------------------------------------------

Mat TplMatch( Mat &img, Mat &mytemplate )
{
  Mat result;

  matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  return result;
}


///------- Localizing the best match with minMaxLoc ------------------------------------------------------------------------

Point minmax( Mat &result )
{
  double minVal, maxVal;
  Point  minLoc, maxLoc, matchLoc;

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

  return matchLoc;
}


///------- tracking --------------------------------------------------------------------------------------------------------

void track()
{
    if (select_flag)
    {
        //roiImg.copyTo(mytemplate);
//         select_flag = false;
        go_fast = true;
    }

//     imshow( "mytemplate", mytemplate ); waitKey(0);

    Mat result  =  TplMatch( img, mytemplate );
    Point match =  minmax( result ); 

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

    std::cout << "match: " << match << endl;

    /// latest match is the new template
    Rect ROI = cv::Rect( match.x, match.y, mytemplate.cols, mytemplate.rows );
    roiImg = img( ROI );
    roiImg.copyTo(mytemplate);
    imshow( "roiImg", roiImg ); //waitKey(0);
}


///------- 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);
        roiImg.copyTo(mytemplate);
//  imshow("MOUSE roiImg", roiImg); waitKey(0);
    }

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

}



///------- Main() ----------------------------------------------------------------------------------------------------------

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

    ///open video file
    VideoCapture cap;
    cap.open( "Megamind.avi" );
    if ( !cap.isOpened() )
    {   cout << "Unable to open video file" << endl;    return -1;    }
/*    
    /// Set video to 320x240
     cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
     cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);*/

    cap >> img;
    GaussianBlur( img, img, Size(7,7), 3.0 );
    imshow( "image", img );

    while (1)
    {
        cap >> img;
        if ( img.empty() )
            break;

    // Flip the frame horizontally and add blur
    cv::flip( img, img, 1 );
    GaussianBlur( img, img, Size(7,7), 3.0 );

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

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

    return 0;
}
#包括
#包括“opencv2/opencv.hpp”
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
点1,点2;/*边界框的垂直点*/
int-drag=0;
Rect Rect;/*边界框*/
Mat img,Roimg;/*roiImg-图像在边界框中的部分*/
int选择_标志=0;
bool go_fast=false;
Mat-mytemplate;
///-------模板匹配-----------------------------------------------------------------------------------------------
Mat TPL匹配(Mat和img、Mat和My模板)
{
Mat结果;
匹配模板(img、mytemplate、result、CV_TM_SQDIFF_NORMED);
规格化(结果,结果,0,1,NORM_MINMAX,-1,Mat());
返回结果;
}
///-------使用minMaxLoc本地化最佳匹配------------------------------------------------------------------------
最小最大点(材料和结果)
{
双最小值,最大值;
minLoc点、maxLoc点、matchLoc点;
minMaxLoc(result,&minVal,&maxVal,&minLoc,&maxLoc,Mat());
matchLoc=minLoc;
返回matchLoc;
}
///-------追踪--------------------------------------------------------------------------------------------------------
无效轨道()
{
如果(选择_标志)
{
//roimg.copyTo(mytemplate);
//选择_flag=false;
快走=真;
}
//imshow(“mytemplate”,mytemplate);waitKey(0);
Mat结果=TPL匹配(img,mytemplate);
点匹配=最小最大值(结果);
矩形(img、match、Point(match.x+mytemplate.cols、match.y+mytemplate.rows)、CV_RGB(255、255、255)、0.5);

std::您可能想在标题中提到这是一个OpenCV问题。在设置
point1
之后,请立即尝试将
point2
设置为等于
point1
,这样它就有了一个合理的值-它不应该破坏任何内容,因为您应该在鼠标移动时立即覆盖并更新它。
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x
+ roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file
/build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 303
terminate called after throwing an instance of 'cv::Exception'  
what():  /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:303:
error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <=
m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <=
m.rows in function Mat
Aborted (core dumped)