1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
   | #include "BlobResult.h"
#include <cv.h>
#include <highgui.h>
//#pragma comment(lib, "cvblobslib.lib")
//#include <stdafx.h>
int main()
{
    CBlobResult blobs;
    IplImage* bin_segmented_image = 0;
    IplImage* outputImage = 0;
    IplImage* frame = 0;
 
    cvNamedWindow("Blobs Painted of Red", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("Objects Detected", CV_WINDOW_AUTOSIZE);
 
    if( (bin_segmented_image = cvLoadImage("blob.bmp",-1)) == 0 )
        return -1;
    cvShowImage("Original", bin_segmented_image);
    cvWaitKey(400);
 
    outputImage = cvCloneImage(bin_segmented_image);
    frame =cvCloneImage(bin_segmented_image);
 
    blobs = CBlobResult( bin_segmented_image, NULL, 100, true );
 
    //blobs.PrintBlobs( "c:\\tmp\\blobs.txt" );
 
    int num_blobs = blobs.GetNumBlobs();
 
    //40 is the min area, and 20000 the max area.
    blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(),  B_INSIDE, 40 , 20000);
 
    //blobs.PrintBlobs( "c:\\tmp\\filteredBlobs.txt" );
 
    num_blobs = blobs.GetNumBlobs();
 
    CBlob blob;
 
    CvPoint rect_vertice_1, rect_vertice_2;   
 
    for(int b = 0; b < num_blobs; b++)
    {
       blob = blobs.GetBlob(b);
       blob.FillBlob( outputImage, cvScalar(125), 0, 0);
 
       rect_vertice_1.x = (int)blob.MinX();
       rect_vertice_1.y = (int)blob.MinY();
 
       rect_vertice_2.x = (int)blob.MaxX();
       rect_vertice_2.y = (int)blob.MaxY();
 
       // 'frame' is the original IplImage image(3 channels).
 
      cvRectangle( frame, rect_vertice_1, rect_vertice_2, cvScalar(125), 1, 8, 0 );   
      //cvRectangle( bin_segmented_image, rect_vertice_1, rect_vertice_2, CV_RGB(0, 255, 0), 1, 8, 0 );   
    }
 
 
    cvShowImage("Blobs Painted of Red", outputImage);   
 
     //blobs.ClearBlobs();
 
    cvShowImage("Objects Detected", frame);
 
	cvWaitKey(800);
    return(1);
} | 
Partager