title: Using Opencv to remove small blobs
body: <p>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.</p>
  
  <pre><code>#include &lt;C:\OpenCV2.2\modules\core\include\opencv2\core\core.hpp&gt;  
  #include &lt;C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui.hpp&gt;  
  #include &lt;iostream&gt;  
  #include &lt;string.h&gt;
  #include &lt;C:\OpenCV2.2\include\opencv\cv.h&gt;  
  #include &lt;stdlib.h&gt;
  #include &lt;C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui_c.h&gt;
  #include &lt;C:\Users\Administrator\Documents\blobs\blob.h&gt;
  #include &lt;C:\Users\Administrator\Documents\blobs\BlobResult.h&gt;
  
  using namespace cv;
  using namespace std;
  
  int MAX_KERNEL_LENGTH = 30;
  int counter=0;
  int Blurtype=0;
  int Kern=5;
  int *Kernp=&amp;Kern;
  int betaval=50;
  int *betavalp=&amp;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",&amp;Kern,MAX_KERNEL_LENGTH,BlobFunc); 
      createTrackbar("BlurType","Blurred Image",&amp;Blurtype,3,BlobFunc); 
      createTrackbar("Betaval","Sharpened Image",&amp;betaval,100,BlobFunc);
      createTrackbar("Threshold value","Binarized Image",&amp;Threshval,255,BlobFunc);
      createTrackbar("Type","Binarized Image",&amp;Threshtype,4,BlobFunc);
      createTrackbar("Size","Blobs",&amp;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 &lt; 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(&amp;img,storage,&amp;contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);                                                      
      IplImage *img_out=cvCreateImage( cvGetSize(&amp;img), 8, 3 );
      cvZero( &amp;img_out );
      for( ; contours != 0; contours = contours-&gt;h_next )
      {
       cvDrawContours( &amp;img_out, contours, black, black, -1, CV_FILLED, 8 );
      }
  
      Mat imgout=img_out;
      cvReleaseMemStorage( &amp;storage );
      imshow("Blobs",imgout);
  }
  </code></pre>