Ask Your Question

Revision history [back]

cvBlobsLib problem; wrong dimensions for blob 0

I have a small image with a white dot on a black background. I am trying to extract the white dot with cvBlobsLib. However, I get a blob with area 2 and dimensions the same as the image dimensions. This blob appears a index 0. If I ignore this blob, I have correct results. Here is the image:

image description

My code is:

    //  Example of extracting and filtering the blobs of an image

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cvblobs/BlobResult.h>

const char  IMG[]                   = "frame_seg.jpg";
const char  INPUT[]                 = "Input";
const char  THRESHOLD[]             = "Threshold";
const char  BLOBS[]                 = "Blob Extraction";

int main ( int argc, char ** argv )
{
    try
    {
        const char * img_file = ( argc == 2 ? argv[1] : IMG );

        // Load the source image and display it

        cv::Mat     image = cv::imread (  img_file, 0 );

        if ( image.empty() )
            throw ( std::string ( " File " ) +
                    std::string ( img_file ) +
                    std::string ( " not found" ) );

        cv::namedWindow ( INPUT, CV_WINDOW_AUTOSIZE );
        cv::imshow ( INPUT, image );
        cv::waitKey();

        // Apply thresholding and display it

        double thresh = 10.0;
        cv::threshold ( image, image, thresh, 255.0, cv::THRESH_BINARY );

        cv::namedWindow ( THRESHOLD, CV_WINDOW_AUTOSIZE );
        cv::imshow ( THRESHOLD, image );
        cv::waitKey();

        // Perform blob analysis

        CBlobResult     blobs;
        IplImage ipl_img = image.operator IplImage();
        blobs = CBlobResult( &ipl_img, NULL, 0 );
        blobs.Filter ( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 1.0 );

        // Display filtered blobs and print information on those

        IplImage * blob_display = cvCreateImage ( image.size(), IPL_DEPTH_8U, 1 );
        memset ( blob_display->imageData, 0, blob_display->imageSize );
        std::cout << "Number of blobs found: " << blobs.GetNumBlobs() << std::endl;
        for ( int i = 0; i < blobs.GetNumBlobs(); i++ )
        {
            CBlob * current_blob = blobs.GetBlob(i);
            current_blob->FillBlob ( blob_display, cv::Scalar ( 255 ) );
            int posx = static_cast<int>( ( current_blob->MaxX() + current_blob->MinX() ) / 2 );
            int posy = static_cast<int>( ( current_blob->MaxY() + current_blob->MinY() ) / 2 );
            std::cout << "Blob area: " << current_blob->Area() << "\t";
            std::cout << "Blob position and dimensions: " << posx << ", " << posy << "\t" << current_blob->GetBoundingBox().width << ", " <<
                current_blob->GetBoundingBox().height << "\t\t" << std::endl;
        }

        cv::imshow ( BLOBS, cv::Mat ( blob_display ) );
        cv::waitKey();
    }
    catch ( std::string msg )
    {
        std::cerr << argv[0] << ": " << msg << std::endl;
        return ( 1 );
    }

    return ( 0 );
}

This has stumped me for a couple of days now. Can anyone please help? I just want to make sure that I get proper dimensions on blob 0.

cvBlobsLib problem; wrong dimensions for blob 0

I have a small image with a white dot on a black background. I am trying to extract the white dot with cvBlobsLib. However, I get a blob with area 2 and dimensions the same as the image dimensions. This blob appears a at index 0. If I ignore this blob, I have correct results. Here is the image:

image description

My code is:

    //  Example of extracting and filtering the blobs of an image

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cvblobs/BlobResult.h>

const char  IMG[]                   = "frame_seg.jpg";
const char  INPUT[]                 = "Input";
const char  THRESHOLD[]             = "Threshold";
const char  BLOBS[]                 = "Blob Extraction";

int main ( int argc, char ** argv )
{
    try
    {
        const char * img_file = ( argc == 2 ? argv[1] : IMG );

        // Load the source image and display it

        cv::Mat     image = cv::imread (  img_file, 0 );

        if ( image.empty() )
            throw ( std::string ( " File " ) +
                    std::string ( img_file ) +
                    std::string ( " not found" ) );

        cv::namedWindow ( INPUT, CV_WINDOW_AUTOSIZE );
        cv::imshow ( INPUT, image );
        cv::waitKey();

        // Apply thresholding and display it

        double thresh = 10.0;
        cv::threshold ( image, image, thresh, 255.0, cv::THRESH_BINARY );

        cv::namedWindow ( THRESHOLD, CV_WINDOW_AUTOSIZE );
        cv::imshow ( THRESHOLD, image );
        cv::waitKey();

        // Perform blob analysis

        CBlobResult     blobs;
        IplImage ipl_img = image.operator IplImage();
        blobs = CBlobResult( &ipl_img, NULL, 0 );
        blobs.Filter ( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 1.0 );

        // Display filtered blobs and print information on those

        IplImage * blob_display = cvCreateImage ( image.size(), IPL_DEPTH_8U, 1 );
        memset ( blob_display->imageData, 0, blob_display->imageSize );
        std::cout << "Number of blobs found: " << blobs.GetNumBlobs() << std::endl;
        for ( int i = 0; i < blobs.GetNumBlobs(); i++ )
        {
            CBlob * current_blob = blobs.GetBlob(i);
            current_blob->FillBlob ( blob_display, cv::Scalar ( 255 ) );
            int posx = static_cast<int>( ( current_blob->MaxX() + current_blob->MinX() ) / 2 );
            int posy = static_cast<int>( ( current_blob->MaxY() + current_blob->MinY() ) / 2 );
            std::cout << "Blob area: " << current_blob->Area() << "\t";
            std::cout << "Blob position and dimensions: " << posx << ", " << posy << "\t" << current_blob->GetBoundingBox().width << ", " <<
                current_blob->GetBoundingBox().height << "\t\t" << std::endl;
        }

        cv::imshow ( BLOBS, cv::Mat ( blob_display ) );
        cv::waitKey();
    }
    catch ( std::string msg )
    {
        std::cerr << argv[0] << ": " << msg << std::endl;
        return ( 1 );
    }

    return ( 0 );
}

This has stumped me for a couple of days now. Can anyone please help? I just want to make sure that I get proper dimensions on blob 0.