2013-06-05 261 views
0

我已經在我正在研究的這個項目的一個泡菜。我的主要目標是將兩個網絡攝像頭聚合在一起,並對它們進行對象檢測 - 包圍盒等......標準的東西。OpenCV緩衝區溢出

我無法擺脫自己的緩衝區溢出雖然 - 下面的一定程度的簡化代碼(閱讀)編譯x64和後不久,我得到在控制檯緩衝區溢出錯誤,這一點:

"OpenCV Error: Assertion Failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || CV_32S) in unknown function, file...." 

如果評論列出所有與輪廓相關的行(從主要從findContoursdrawBoundingBoxes),編譯並運行正常,直到我點擊空格鍵停止程序,然後出現另一個緩衝區溢出錯誤。我在編譯x32時也遇到了相同的錯誤,以備記錄。

任何幫助?相關代碼/僞代碼粘貼下面:

// **defines.h** 
//Definitions for anything in all caps, like WIDTH, HEIGHT, ERODEIT, etc... 

// **protos.h** 
// All function prototypes, nothing else 

// **detection.cpp** 

/* This is the code that related to background subtraction operations*/ 

#include <opencv2/opencv.hpp> 
#include <iostream> 
#include "defines.h" 

using namespace std; 
using namespace cv; 

void initBackgroundSubtractor(BackgroundSubtractorMOG2 &bSub) 
{ 
    bSub.set("detectShadows", 1); 
} 

Mat doBackgroundSubtract(BackgroundSubtractorMOG2 &bSub, Mat panorama) 
{ 
    Mat foreground; 

    bSub.operator()(panorama, foreground); 
    erode(foreground, foreground, Mat(), Point(-1, -1), ERODEIT, BORDER_DEFAULT); 
    dilate(foreground, foreground, Mat(), Point(-1, -1), DILATEIT, BORDER_DEFAULT); 

    return foreground; 
} 

// **contourOps.cpp** 

/* Functions that operate on, filter, or relate to OpenCV contours vectors */ 

#include <opencv2/opencv.hpp> 
#include <vector> 
#include <fstream> 
#include "defines.h" 

using namespace std; 
using namespace cv; 

/* Returns the centroid of a contour */ 

Point getCentroid(vector<Point> contour) 
{ 
    Point centroid; 
    Moments m; 

    m = moments(contour, false); 
    centroid.x = int(m.m10/m.m00); 
    centroid.y = int(m.m01/m.m00); 

    return centroid; 
} 

/* Draws a rectangle around a contour */ 

void drawBoundingBoxes(vector<vector<Point>> contours, Mat &img) 
{ 
    vector<Rect> boundRect(contours.size()); 

    for(unsigned int j = 0; j < contours.size(); j++) 
    { 
     boundRect[j] = boundingRect(contours[j]); 
     rectangle(img, boundRect[j], Scalar(153,0,76), 2, 8, 0); 
    } 
} 

/* Removes contours from a vector if they're smaller than the argument "area" */ 

void contourSizeTrim (vector<vector<Point>> &contours, int area) 
{ 
    vector<vector<Point>>::iterator i = contours.begin(); 
    while(i != contours.end()) 
    { 
     if(contourArea(*i, false) < area) 
      i = contours.erase(i); 
     else 
      i++; 
    } 
} 

/* Removes contours from a vector if they're X % smaller than largest contour in vector */ 

void contourRelSizeTrim(vector<vector<Point>> &contours, int percent) 
{ 
    double maxArea = 0.0; 

    for(unsigned int i=0; i<contours.size(); i++) 
    { 
     if (contourArea(contours[i], false) > maxArea) 
       maxArea = contourArea(contours[i], false); 
    } 

    vector<vector<Point>>::iterator j = contours.begin(); 
    while(j != contours.end()) 
    { 
     if (contourArea(*j, false) < (double)(percent/100.0)*maxArea) 
      j = contours.erase(j); 
     else 
      j++; 
    } 
} 

// **realtimestitch.cpp** 

#include <opencv2/opencv.hpp> 
#include <opencv2/stitching/stitcher.hpp> 
#include <vector> 
#include <iostream> 
#include "defines.h" 

using namespace std; 
using namespace cv; 

void initStitcher(VideoCapture &capture1, VideoCapture &capture2, Stitcher &stitch) 
{ 
    capture1.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH); 
    capture1.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT); 
    capture2.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH); 
    capture2.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT); 

    detail::OrbFeaturesFinder *featureFinder = new detail::OrbFeaturesFinder(Size(3,1), 1000, 1.5f, 4); 
    stitch.setFeaturesFinder (featureFinder); 
} 

void calcCamTransform(VideoCapture &capture1, VideoCapture &capture2, Stitcher &stitch) 
{ 
    int64 t; 
    Mat fr1, fr2, copy1, copy2; 
    vector<Mat> imgs; 

    capture1 >> fr1; 
    capture2 >> fr2; 
    fr1.copyTo(copy1); 
    fr2.copyTo(copy2); 
    imgs.push_back(copy1); 
    imgs.push_back(copy2); 
    stitch.estimateTransform(imgs); 
} 

Mat doStitch(VideoCapture &capture1, VideoCapture &capture2, Stitcher &stitch) 
{ 
    Mat fr1, fr2, copy1, copy2, panorama; 
    vector<Mat> imgs; 

    capture1 >> fr1; 
    capture2 >> fr2; 
    fr1.copyTo(copy1); 
    fr2.copyTo(copy2); 
    imgs.push_back(copy1); 
    imgs.push_back(copy2); 
    Stitcher::Status status = stitch.composePanorama(imgs, panorama); 

    if (status != Stitcher::OK) 
     cout << "Error Stitching: Code: " << int(status) << endl; 

    return panorama; 
} 

// **main.cpp** 

#include <opencv2/opencv.hpp> 
#include <iostream> 
#include <vector> 
#include "defines.h" 
#include "protos.h" 

using namespace cv; 

int main() 
{ 
    bool doTransform = true, doSizeFilter = true, doRelSizeFilter = true; 
    Mat pano, fGround; 
    vector<vector<Point>> contours; 
    VideoCapture cap1(0); 
    VideoCapture cap2(1); 
    Stitcher stitcher = Stitcher::createDefault(); 
    BackgroundSubtractorMOG2 bGround; 

    initStitcher(cap1, cap2, stitcher); 
    initBackgroundSubtractor(bGround); 

    while (true) 
    { 
     if (doTransform) 
     { 
      calcCamTransform(cap1, cap2, stitcher); 
      doTransform = !doTransform; 
     } 

     pano = doStitch(cap1, cap2, stitcher); 
     fGround = doBackgroundSubtract(bGround, pano); 


     findContours(fGround, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 

     if (doSizeFilter) 
      contourSizeTrim(contours, AREATHRESH); 
     if (doRelSizeFilter) 
      contourRelSizeTrim(contours, RELSIZEPERCENT); 

     drawBoundingBoxes(contours, pano); 

     imshow("Stitched Image", pano); 

     if(waitKey(1) >= 0) 
      break; 
    } 
    return 0; 
} 
+0

你可以在你的代碼中標記導致錯誤的功能嗎? 看着你的代碼我懷疑,那個問題是從背景減法器返回的fGroung圖像不是8位1通道圖像。 http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours 因此,首先檢查fGrounf的類型,並根據需要進行一些閾值或類型轉換。 – jnovacho

+0

在我的原始文章中,我標記了至少部分的錯誤 - 它在find.contours和drawBoundingBoxes之間的main.cpp中。 我試圖添加行: foreground.converTo(前景,CV_8UC1)在detect.cpp doBackgroundSubtract()函數中無濟於事。 – TonyRo

+0

那麼,首先嚐試找到導致錯誤的確切行。不知道哪條線路導致問題,我們只能猜測。 – jnovacho

回答

0

這是關係到OpenCV的和VS2012的一個問題 - 在VS2010,不存在任何問題,和代碼運行完美!

0

我對此的看法是,您嘗試使用空輪廓矢量做某些事情。你有證實嗎?嘗試

if (contours.empty()) continue; // or here you can display the image to see if it is empty or not 

我有同樣的問題,因爲我想給一個epty載體cv::IsContourConvex(...)功能(see here)。