2017-04-18 14 views
0

この単純なコードは実行できません。私は2枚の画像の間のオプティカルフローを計算しようとしています。単純な高密度オプティカルフロープログラムcalcOpticalFlowFarneback()openCV 3.2 cpp

アサーションがimg1や大きさの異なるimg2によるものである添付画像

OpenCV Error: Assertion failed (prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyrScale_ < 1) in cv::`anonymous-namespace'::FarnebackOpticalFlowImpl::calc, file C:\Users\krato\Desktop\OpenCV\opencv-master\modules\video\src\optflowgf.cpp, line 1114

#include <Windows.h> 
#include "opencv2\highgui.hpp" 
#include "opencv2\imgproc.hpp" 
#include "opencv2\objdetect\objdetect.hpp" 
#include "opencv2/video/tracking.hpp" 
#include <vector> 
#include <stdio.h> 
#include <Windows.h>  
#include <iostream> 
    using namespace std; 
    using namespace cv; 

    // Display the results of the matches 
    // 
    int main(int argc, char* argv[]) 
    { 
     cv::Mat img1 = cv::imread(argv[1], cv::IMREAD_GRAYSCALE); 
     cv::Mat img2 = cv::imread(argv[2], cv::IMREAD_GRAYSCALE); 
     cv::Mat res; 

     cv::calcOpticalFlowFarneback(img1,img2,img1,.4,1,12,2,8,1.2, 0); 

     cv:imshow("cat", res); 
     cv::waitKey(0); 


    } 

Error Picture 1

Error Picture 2

答えて

1

を確認してください。あなたが与えた最初のイメージでは、img1には762行、img2には768行があることがはっきりと分かります。
cv::resizeを使用してimg2img1と同じサイズにしてみてください。 cv::calcOpticalFlowFarneback()の前に次の行を追加します。
cv::resize(img2, img2, img1.size());

0

shawshank

のおかげで、コードの最終バージョンは次のとおりです。アクションのコードを示し

#include "opencv2\highgui.hpp" 
#include "opencv2\imgproc.hpp" 
#include "opencv2\objdetect\objdetect.hpp" 
#include "opencv2/video/tracking.hpp" 
#include <vector> 
#include <stdio.h> 
#include <Windows.h> 
#include <iostream> 
using namespace std; 
using namespace cv; 

// Display the results of the matches 
// 
int main(int argc, char* argv[]) 
{ 
    cv::Mat res, img1, img2, img2Original, img2OriginalC; 
    cv::VideoWriter writer; 

    cv::VideoCapture cap; 
    cap.open(std::string(argv[1])); 
    //cv::cap.open(0); 


    cv::namedWindow("cat", cv::WINDOW_AUTOSIZE); 

    cap >> img1; 
    cvtColor(img1, img1, COLOR_BGR2GRAY); 
    double fps = cap.get(cv::CAP_PROP_FPS); 
    cv::Size tamano((int)cap.get(cv::CAP_PROP_FRAME_WIDTH), (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT)); 
    writer.open("mouse.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, tamano); 

    for (;;) { 
     cap >> img2; 
     if (img2.empty()) break; 

     img2.copyTo(img2OriginalC); 
     cvtColor(img2, img2, COLOR_BGR2GRAY); 
     img2.copyTo(img2Original); 
     cv::calcOpticalFlowFarneback(img1, img2, res, .4, 1, 12, 2, 8, 1.2, 0); 
     for (int y = 0; y < img2.rows; y += 5) { 
      for (int x = 0; x < img2.cols; x += 5) 
      { 
       // get the flow from y, x position * 3 for better visibility 
       const Point2f flowatxy = res.at<Point2f>(y, x) * 1; 
       // draw line at flow direction 
       line(img2OriginalC, Point(x, y), Point(cvRound(x + flowatxy.x), cvRound(y + flowatxy.y)), Scalar(255, 0, 0)); 
       // draw initial point 
       circle(img2OriginalC, Point(x, y), 1, Scalar(0, 0, 0), -1); 
      } 
     } 
     img2Original.copyTo(img1); 
     imshow("cat", img2OriginalC); 
     //writer << img2OriginalC;  
     if (cv::waitKey(1) == 27) break; 
    } 
    cap.release(); 
    return 0; 
} 

はビデオ: https://www.youtube.com/watch?v=rfuP-z2OR8I

関連する問題