Question

I am using opencv2.2 on visual studio 2010. I have written a code to preprocess images for OCR. I'm using a lot of trackbars to vary parameters. One of the preprocessing functions is to remove small blobs by drawing contours and filtering them out according to size. However the cvDrawContours function is giving me an error when I run the program. Basically I get popup and an error saying R6010 -abort has been called.The command line says that there's an unknown array type in matrix.cpp on line 641. I'm including my code here. The issue is called by the cvDrawContours function within the BlobFunc function.

#include <C:\OpenCV2.2\modules\core\include\opencv2\core\core.hpp>  
#include <C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui.hpp>  
#include <iostream>  
#include <string.h>
#include <C:\OpenCV2.2\include\opencv\cv.h>  
#include <stdlib.h>
#include <C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui_c.h>
#include <C:\Users\Administrator\Documents\blobs\blob.h>
#include <C:\Users\Administrator\Documents\blobs\BlobResult.h>

using namespace cv;
using namespace std;

int MAX_KERNEL_LENGTH = 30;
int counter=0;
int Blurtype=0;
int Kern=5;
int *Kernp=&Kern;
int betaval=50;
int *betavalp=&betaval;
int Threshtype=0;
int Threshval=30;
int size=10;

Mat src1, src2, dst1, dst2, dst3;
CvScalar black=CV_RGB( 0, 0, 0 ); // black color
CvScalar white=CV_RGB( 255, 255, 255 );   // white color
double area;

void BlobFunc(int,void*);
int main( int argc, char** argv )
{
    //Read Input
    src1 = imread(argv[1]);
    if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
    //Create Windows
    namedWindow("Original Image",1);
    namedWindow("Binarized Image",1);
    namedWindow("Gray Image",1);
    namedWindow("Sharpened Image",1);
    namedWindow("Blurred Image",1);
    imshow("Original Image",src1);
    namedWindow("Blobs",1);
    //Create Trackbars
    cvtColor(src1,src2,CV_RGB2GRAY);
    imshow("Gray Image",src2);
    threshold( src2, dst1, Threshval, 255,Threshtype);
    imshow("Binarized Image",dst1);
    createTrackbar("Kernel","Blurred Image",&Kern,MAX_KERNEL_LENGTH,BlobFunc); 
    createTrackbar("BlurType","Blurred Image",&Blurtype,3,BlobFunc); 
    createTrackbar("Betaval","Sharpened Image",&betaval,100,BlobFunc);
    createTrackbar("Threshold value","Binarized Image",&Threshval,255,BlobFunc);
    createTrackbar("Type","Binarized Image",&Threshtype,4,BlobFunc);
    createTrackbar("Size","Blobs",&size,100,BlobFunc); //Size of Blob
    waitKey(0);
    return 0;
}

void BlobFunc(int,void*)
{
    CvMemStorage *storage=cvCreateMemStorage(0);
    CvSeq *contours=0;
    cvtColor(src1,src2,CV_RGB2GRAY);
    imshow("Gray Image",src2);
    threshold( src2, dst1, Threshval, 255,Threshtype);
    imshow("Binarized Image",dst1);
    for ( int i = 1; i < Kern; i = i + 2 )
    {
        if (Blurtype==0)
        {
            blur(dst1,dst2, Size( i, i ), Point(-1,-1) );
        }
    else if (Blurtype==1)
        {
            GaussianBlur( dst1, dst2, Size( i, i ), 0, 0 );
        }
    else if (Blurtype==2)
        {
            medianBlur ( dst1, dst2, i );
        }
    else if (Blurtype==3)
        {
            bilateralFilter ( dst1, dst2, i, i*2, i/2 );
        }       
    }
    imshow("Blurred Image",dst2);
    addWeighted( dst1, 1, dst2, -double(betaval)/100, 0.0, dst3);
    imshow("Sharpened Image",dst3);
    IplImage img=dst3;
    cvFindContours(&img,storage,&contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);                                                      
    IplImage *img_out=cvCreateImage( cvGetSize(&img), 8, 3 );
    cvZero( &img_out );
    for( ; contours != 0; contours = contours->h_next )
    {
     cvDrawContours( &img_out, contours, black, black, -1, CV_FILLED, 8 );
    }

    Mat imgout=img_out;
    cvReleaseMemStorage( &storage );
    imshow("Blobs",imgout);
}
Was it helpful?

Solution

I get that type of error when i'm passing in an invalid Mat (or iplimage). I might have copied it incorrectly, or the image might be of the one type, channels, number of colours etc.

The issue appears to lie with an incorrect parameter so i would look at how your passing in the image, I.e. instead of &img, you could try img without the &. Afraid i'm not familiar with C++ but thats where i'd start.

The example below is similar, perhaps it will help show the use of & and *.

void displayContours(const Mat &src, Mat features) {
    RNG rng(12345);
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    Mat s;
    src.copyTo(s);
    findContours(s, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f> center(contours.size());
    vector<float> radius(contours.size());

    for (int i = 0; i < contours.size(); i++) {
        approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
        boundRect[i] = boundingRect(Mat(contours_poly[i]));
        minEnclosingCircle(contours_poly[i], center[i], radius[i]);
    }

    /// Draw polygonal contour + bonding rects + circles
    for (int i = 0; i < contours.size(); i++) {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
                rng.uniform(0, 255));
        drawContours(features, contours_poly, i, color, 1, 8, vector<Vec4i>(),
                0, Point());
        rectangle(features, boundRect[i].tl(), boundRect[i].br(), color, 2, 8,
                0);
        circle(features, center[i], (int) radius[i], color, 2, 8, 0);
    }

}

This was from one of the opencv tutorials, and is using Mat instead of iplImage, which may require a different usage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top