2016-12-29 129 views
-1

我想嘗試學習OpenCV(目前使用2.4.13版本),所以我設置了這個小程序從攝像機流中輸出RGB濾鏡。然而,當我運行該程序只運行約10幀,和OpenCV從invalid_parameter.cpp從Windows SDK執行的代碼,該段:爲什麼我的OpenCV程序執行Windows __fastfail()方法?

if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE)) 
{ 
    __fastfail(FAST_FAIL_INVALID_ARG); 
} 

代碼似乎停止執行管道:: findContour功能時,但是當我嘗試調試斷點時,放置在方法中時決不會命中,並且逐步執行對此特定方法不起作用。

這個錯誤函數正在執行的原因是什麼?我如何防止它再次發生?

main.cpp中:

#include <iostream> 
#include "Pipeline.h" 

int main() 
{ 
    Pipeline *pipeline = new Pipeline(); 

    VideoCapture feed; 
    Mat frame; 
    if (!feed.open(0)) 
     return -1; 

    //This for loop runs 7000 times for just for debug purposes 
    for (auto i = 0; i < 7000; i++) 
    { 
     feed >> frame; 
     if (frame.empty()) 
      break; 
     cv::waitKey(1); 
     pipeline->setsource0(frame); 
     try 
     { 
      pipeline->Process(); 
     } 
     catch (const std::exception& e) 
     { 
      cerr << e.what(); 
     } 
     Mat* image = pipeline->getrgbThresholdOutput(); 
     cv::imshow("Output", *image); 
    } 
    return 0; 
} 

pipeline.h:

#pragma once 
#include <opencv2/objdetect/objdetect.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/core/core.hpp> 
#include <opencv2/features2d.hpp> 
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <map> 
#include <math.h> 
using namespace cv; 
using namespace std; 

class Pipeline { 
    private: 
     Mat source0; 
     Mat rgbThresholdOutput; 
     vector<vector<Point> > findContoursOutput; 
     void rgbThreshold(Mat &, double [], double [], double [], Mat &); 
     void findContours(Mat &, bool , vector<vector<Point> > &); 

    public: 
     Pipeline(); 
     void Process(); 
     void setsource0(Mat &source0); 
     Mat* getrgbThresholdOutput(); 
     vector<vector<Point> >* getfindContoursOutput(); 
}; 

pipeline.cpp:

#include "Pipeline.h" 

Pipeline::Pipeline() { 
} 

void Pipeline::Process(){ 
    //Step RGB_Threshold0: 
    //input 
    Mat rgbThresholdInput = source0; 
    double rgbThresholdRed[] = {100, 240}; 
    double rgbThresholdGreen[] = {40, 233.53535353535355}; 
    double rgbThresholdBlue[] = {50, 170}; 
    rgbThreshold(rgbThresholdInput, rgbThresholdRed, rgbThresholdGreen, rgbThresholdBlue, this->rgbThresholdOutput); 
    //Step Find_Contours0: 
    //input 
    Mat findContoursInput = rgbThresholdOutput; 
    bool findContoursExternalOnly = false; 
    findContours(findContoursInput, findContoursExternalOnly, this->findContoursOutput); 
} 

void Pipeline::setsource0(Mat &source0){ 
    source0.copyTo(this->source0); 
} 

void Pipeline::rgbThreshold(Mat &input, double red[], double green[], double blue[], Mat &output) { 
    inRange(input, Scalar(red[0], green[0], blue[0]), Scalar(red[1], green[1], blue[1]), output); 
} 

void Pipeline::findContours(Mat &input, bool externalOnly, vector<vector<Point> > &contours) { 
    contours.clear(); 
    int mode = externalOnly ? CV_RETR_EXTERNAL : CV_RETR_LIST; 
    int method = CHAIN_APPROX_SIMPLE; 
    cv::findContours(input, contours, mode, method); 
} 

Mat* Pipeline::getrgbThresholdOutput(){ 
    return &(this->rgbThresholdOutput); 
} 

vector<vector<Point> >* Pipeline::getfindContoursOutput(){ 
    return &(this->findContoursOutput); 
} 

回答

0

我做了幾μm調試礦石並發現通過在findContours()方法之前添加try和catch語句,我能夠成功地逐步完成。這使我能夠在代碼中的隨機點發現輪廓對象變爲NULL,從而防止clear()方法運行。然後我將其更改爲contours.empty(),然後代碼成功運行。

+0

「輪廓對象變爲NULL」是什麼意思?只有指針可以變爲NULL。此外,調用'contours.empty()'(並忽略輸出)沒有任何影響,並可能被編譯器優化。 – chtz

相關問題