Question

I am trying to set up my programme to threshold for a colour (in BGR format). I have not fully decided which colour I will be looking for yet. I would also like the program to record how many pixels it has detected of that colour. My code so far is below but it is not working.

#include "cv.h"
#include "highgui.h"




int main()
{
 // Initialize capturing live feed from the camera
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);

// Couldn't get a device? Throw an error and quit
if(!capture)
{
    printf("Could not initialize capturing...\n");
    return -1;
}
 // The two windows we'll be using
cvNamedWindow("video");
cvNamedWindow("thresh");

 // An infinite loop
while(true)
{
    // Will hold a frame captured from the camera
    IplImage* frame = 0;
    frame = cvQueryFrame(capture);

            // If we couldn't grab a frame... quit
    if(!frame)
        break;


    //create image where threshloded image will be stored
    IplImage* imgThreshed = cvCreateImage(cvGetSize(frame), 8, 1);


    //i want to keep it BGR format. Im not sure what colour i will be looking for yet. this can be easily changed
    cvInRangeS(frame, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed);


    //show the original feed and thresholded feed
    cvShowImage("thresh", imgThreshed);
    cvShowImage("video", frame);

    // Wait for a keypress
    int c = cvWaitKey(10);
    if(c!=-1)
    {
        // If pressed, break out of the loop
        break;
    }

    cvReleaseImage(&imgThreshed);
}
cvReleaseCapture(&capture);
return 0;

}

Was it helpful?

Solution

To threshold for a color,

1) convert image to HSV

2) Then apply cvInrangeS

3) Once you got threshold image, you can count number of white pixels in it.

Try this tutorial to track yellow color: Tracking colored objects in OpenCV

OTHER TIPS

I can tell how to do it in both Python and C++ and both with and without converting to HSV.

C++ Version (Converting to HSV)

  1. Convert the image into an HSV image:

    // Convert the image into an HSV image IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3); cvCvtColor(img, imgHSV, CV_BGR2HSV);

  2. Create a new image that will hold the threholded image:

    IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);

  3. Do the actual thresholding using cvInRangeS

    cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed);

Here, imgHSV is the reference image. And the two cvScalars represent the lower and upper bound of values that are yellowish in colour. (These bounds should work in almost all conditions. If they don't, try experimenting with the last two values).

Consider any pixel. If all three values of that pixel (H, S and V, in that order) lie within the stated ranges, imgThreshed gets a value of 255 at that corresponding pixel. This is repeated for all pixels. So what you finally get is a thresholded image.

  1. Use countNonZero to count the number of white pixels in the thresholded image.

Python Version (Without converting to HSV):

  1. Create the lower and upper boundaries of the range you are interested in, in Numpy array format (Note: You need to use import numpy as np)

lower = np.array((a,b,c), dtype = "uint8") upper = np.array((x,y,z), dtype = "uint8")

In the above (a,b,c) is the lower bound and (x,y,z) is the upper bound.

2.Get the mask for the pixels that satisfy the range:

mask = cv2.inRange(image, lower, upper)

In the above, image is the image on which you want to work.

  1. Count the number of white pixels that are present in the mask using countNonZero:

yellowpixels = cv2.countNonZero(mask) print "Number of Yellow pixels are %d" % (yellowpixels)

Sources:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top