【问题标题】:Using Opencv to remove small blobs使用 Opencv 去除小斑点
【发布时间】:2012-08-31 10:31:46
【问题描述】:

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

【问题讨论】:

    标签: opencv abort blobs


    【解决方案1】:

    当我传入无效的 Mat(或 iplimage)时,我得到了这种类型的错误。我可能复制不正确,或者图像可能属于一种类型、通道、颜色数量等。

    问题似乎在于参数不正确,所以我会看看你是如何传递图像的,即而不是 &img,你可以尝试不带 & 的 img。恐怕我不熟悉 C++,但这就是我要开始的地方。

    下面的例子也是类似的,也许它会帮助展示 & 和 * 的用法。

    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,这可能需要不同的用法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多