使用Opencv删除小blob

时间:2012-08-31 10:31:47

标签: opencv abort blobs

我在visual studio 2010上使用opencv2.2。我编写了一个代码来预处理OCR的图像。我使用了很多跟踪条来改变参数。其中一个预处理功能是通过绘制轮廓并根据大小过滤掉它们来移除小斑点。但是,当我运行程序时,cvDrawContours函数给我一个错误。基本上我得到弹出窗口并且出现错误,说R6010 -abort已被调用。命令行说明在第641行的matrix.cpp中有一个未知的数组类型。我在这里包含了我的代码。该问题由BlobFunc函数中的cvDrawContours函数调用。

#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);
}

1 个答案:

答案 0 :(得分:1)

当我传入无效的Mat(或iplimage)时,我得到了那种类型的错误。我可能错误地复制了它,或者图像可能是一种类型,通道,颜色数量等。

问题似乎在于一个不正确的参数,所以我会看看你在图像中的传递方式,即。而不是&amp; img,你可以尝试img没有&amp;。我不熟悉C ++,但那是我开始的地方。

以下示例类似,也许它有助于展示&amp;的使用和*。

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);
    }

}

这是来自其中一个opencv教程,并使用Mat而不是iplImage,这可能需要不同的用法。