Search code examples
opencvobject-detection

opencv cvblob -- render blobs other than white


I am trying to detect objects with cvblob. Somehow, my code only marks the white objects. How to mark objects of other colors, like a can of beer or a bottle of water.

Here is my code:

#include "highgui.h"
#include "cv.h"
#include "cvaux.h"
#include "iostream"
#include <stdio.h>
#include <ctype.h>

#include <cvblob.h>

using namespace cv;
using namespace std;
using namespace cvb;

int main(int argc, char** argv) {
    CvTracks tracks;

    cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("frame", 50, 100);

    CvCapture* capture;

    IplImage* frame = 0;

    capture = cvCreateCameraCapture( 0 ); //capture frames from cam on index 0: /dev/video0/

    if (!capture) {
        return -1;
    }

    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240);

    frame = cvQueryFrame(capture);

    while(frame) {
        IplImage *gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
        cvCvtColor(frame, gray, CV_BGR2GRAY);
        cvThreshold(gray, gray, 150, 255, CV_THRESH_BINARY);

        IplImage *labelImg=cvCreateImage(cvGetSize(gray), IPL_DEPTH_LABEL, 1);
        CvBlobs blobs;
        unsigned int result=cvLabel(gray, labelImg, blobs);

        cvFilterByArea(blobs, 500, 1000000);
//        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_BOUNDING_BOX);
        cvRenderBlobs(labelImg, blobs, frame, frame, CV_BLOB_RENDER_CENTROID);
        cvUpdateTracks(blobs, tracks, 200., 5);
        cvRenderTracks(tracks, frame, frame, CV_TRACK_RENDER_ID|CV_TRACK_RENDER_BOUNDING_BOX);


        for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it) {
            cout << "Blob #" << it->second->label << ": Area=" << it->second->area << ", Centroid=(" << it->second->centroid.x << ", " << it->second->centroid.y << ")" << endl;
        }

        cvShowImage("frame", frame);

        cout << "----------------------------" << endl;

        frame = cvQueryFrame(capture);

        char c = cvWaitKey(10);
        if(c==27)
            break;

    }
}

Any tip is appreciated.

Milo


Solution

  • That's the option by default and you cannot change it if you don't change the source code in cvblob library.

    If you really want to change this is so easy, you can create a copy of the same method adding a new input var like CvScalar to select output color. It's so easy.

    The method cvRenderBlob will be in cvcontour.cpp.

    I've been made many improvement in cvblob library and in next months I will push it to the creator.