2013-10-02 7 views
1

我嘗試使用calcOpticalFlowSF()函數,但是當我啓動它,在PROGRAMM不repond,這裏使用它的代碼的一部分:OpencCV:calcOpticalFlowSF功能

frame1 = cv::imread("frame10.png"); 
frame2 = cv::imread("frame11.png"); 

if (frame1.empty()) { 
    cout<<"could not read image oldori"<<endl; 

    return; 
} 

if (frame2.empty()) { 
    cout<<"could not read image ori"<<endl; 
    return; 
} 

if (frame1.rows != frame2.rows && frame1.cols != frame2.cols) { 
    cout<<"images should be of equal sizes "endl; 
    return; 
} 

if (frame1.type() != 16 || frame2.type() != 16) { 
    cout<<"images should be of equal type CV_8UC3")endl; 
    return; 
} 

cv::Mat flow; 

cv::calcOpticalFlowSF(frame1, frame2, flow, 2, 2, 4); 
// calcOpticalFlowSF(frame1, frame1, // doesn't work too. 
//     flow, 
//     3, 2, 4, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10); 

我知道,該錯誤來自函數calcOpticalFlowSF,因爲如果我評論它,程序就起作用。我使用與SimpleFlow演示中使用的相同的圖片。如果你看這裏:How to get cv::calcOpticalFlowSF to work?看來,他沒有問題的功能本身...

你知道爲什麼它不起作用?

謝謝,

最好的問候。

回答

0

確保圖像是3通道,是更好地看到的文檔here

#if 1 
#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio> 
#include <cstdlib> 
#include <vector> 
#include <stdlib.h> 
#include <opencv2/opencv.hpp> 
#include "opencv2/optflow.hpp" 
#include "opencv2/highgui.hpp" 


using namespace std; 
using namespace cv; 
using namespace cv::optflow;//calcOpticalFlowSF 's namespace 
const size_t choice = 2 ; 
// choice 
// 1 2 3 
// calcOpticalFlowFarneback calcOpticalFlowSF calcOpticalFlowPyrLK 
void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step, const Scalar& color) { 
    // cflowmap is the pre frame with the line of Optical Flow 
    // flow is a xxx array, store float point 
    //  store the delta of x,y 
    // step is every step pixel 
    for (int y = 0; y < cflowmap.rows; y += step) 
     for (int x = 0; x < cflowmap.cols; x += step) 
     { 
      const Point2f& fxy = flow.at< Point2f>(y, x); 
      line(cflowmap, Point(x, y), Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), 
       color); 
      circle(cflowmap, Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), 1, color, -1); 
     } 
} 

void drawOptFlowMap(Mat& cflowmap, int step, const Scalar& color, vector<Point2f> &retPts) { 
    // same as above, retPts is the next frame point 
    auto it = retPts.begin(); 
    for (int y = 0; y < cflowmap.rows; y += step) 
     for (int x = 0; x < cflowmap.cols; x += step) 
     { 
      line(cflowmap, Point(x, y), *it, color); 
      circle(cflowmap, *it, 1, color, -1); 
      it++; 
     } 
} 

int main(int argc, char *argv[]) 
{ 
    Mat flow;//flow = aft - pre 
    Mat pre = imread("1hf.png", IMREAD_COLOR); 
    Mat aft = imread("2hf.png", IMREAD_COLOR);//   CV_LOAD_IMAGE_GRAYSCALE gray ; IMREAD_COLOR color 
    if (pre.empty() || aft.empty()){ 
     printf("Unable to load the image"); 
     return 1; 
    } 


    Mat cflow = pre; Mat cflow2 = aft;// store the 3-channel mat of frame, cflow is for show color with line 
    cvtColor(pre, pre, CV_BGR2GRAY); 
    cvtColor(aft, aft, CV_BGR2GRAY); 

    //below parameter of calcOpticalFlowPyrLK 
    vector<Point2f> prePts; 
    size_t step = 10; 
    vector<Point2f> nextPts(pre.rows * pre.cols); 
    vector<uchar> status; 
    vector<float> err; 
    TermCriteria termcrit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03); 

    switch (choice) 
    { 
    case 1:// calcOpticalFlowFarneback 
     calcOpticalFlowFarneback(pre, aft, flow, 0.5, 3, 15, 3, 5, 1.2, 0); // info in the flow; note that input mat should in 1-channel 
     drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0)); break; 
    case 2:// calcOpticalFlowSF 
     calcOpticalFlowSF(cflow, cflow2, 
      flow, 
      3, 2, 4, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);// info in the flow; note that input mat should in 3-channel 

     drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0)); break; 
    case 3:// calcOpticalFlowPyrLK 
     for (int y = 0; y < pre.rows; y += step) 
      for (int x = 0; x < pre.cols; x += step) 
      { 
      prePts.push_back(Point(x, y)); 
      } 

     // above get a point vector in step 
     calcOpticalFlowPyrLK(pre, aft, prePts, nextPts, status, err, Size(31, 31), 3, termcrit, 0, 0.001);// info in the flow; note that input mat should in 1-channel 
     drawOptFlowMap(cflow, step, CV_RGB(0, 255, 0), nextPts); 
     break; 
    default: 
     break; 
    } 

    imshow("pre", pre); 
    imshow("after", aft); 
    //cflow is the pre with OpticalFlow line 
    imshow("pre with OpticalFlow line", cflow); 
    waitKey(0); 
    return 0; 
} 
#endif