Ask Your Question
0

HELP!!!! having issues tracking 2 colors

asked 2013-10-19 17:57:36 -0600

I need to add a new circle to the video window that will track a different color, in my getThresholdedImage function whenever i add another line to put in a new hue,sat,value it overrides the other color so i only detect the one that is the last to be read by the compiler. Also How do i set up a new coordinates for the new circle, below is the code from my main scrip.

// This function takes in an image and returns a // black and white image where the color was found. Mat getThresholdedImage(Mat img) { Mat thresholded;

// Convert the image into an HSV image
Mat hsvImage;

cvtColor(img, hsvImage, CV_BGR2HSV);


//Set pixel to white if in the range specified below. See the notes.
//        source    min(hue sat value)    max(hue sat value)     destination
inRange(hsvImage, Scalar(92, 50, 50), Scalar(95, 255, 255), thresholded); // LightBlueColor

    //inRange(hsvImage_2, Scalar(14, 0, 0), Scalar(16, 255, 255), thresholded);// Orange Color

the above commented line is where im trying to look for the orange color in the same object screen.

    // Get rid of single pixel errors
erode(thresholded, thresholded, Mat(), Point(-1,-1), 2);
dilate(thresholded, thresholded, Mat(), Point(-1,-1), 3);

return thresholded;

}

int main() { // Setting initialized to false bool initialized = false;

// Creating an object for the light blue color
ExponentialFilter colorLIghtBlue;
// ExponentialFilter colorOrange;

// Setting the weight value
ExponentialFilter( .5 );

// The two windows we'll be using
namedWindow("video");
namedWindow("Object");

VideoCapture camera(0); // open the default camera

// Make sure camera is setup
waitKey(1000);


if(!camera.isOpened())  // check if we succeeded
    return -1;

do
{ 
    Mat frame, colorDetect;

    // Send data from the camera to the frame
    camera >> frame; // get a new frame from camera

    // This function does the work of finding a specific color
    colorDetect = getThresholdedImage( frame );

    // This is some magic that turns all the found pixels into a center of gravity location
    cv::Moments moment; //moments variable
    moment=moments(colorDetect,1); //calculate all the moment of image

    double x,y;
    // Store the center of the pixels
    x = moment.m10/moment.m00;
    y = moment.m01/moment.m00;

    // Taking the x and y values and running them throught the exponential
    // smooting filter, if the if conditional statement is correct
        if ( x >= 0 && y >= 0 )
        {
             colorLIghtBlue.methodUpdate(x,y);
        }
        else 
        {
            std::cout<< "x & y are equal to zero" << std::endl;
        }
    //outputting the blues colors center coordinates:
    std::cout << x << " , " << y;

    // Draw a circle at the center of the blue color
    cv::circle(frame, cvPoint(x,y), 4, cvScalar(0,255,255), 2);

    // Draw a circle at the center of the orange color
    //cv::circle(frame, cvPoint(x,y), 4, cvScalar(0,255,255), 2);

    imshow( "Video", frame);
    imshow("Object", colorDetect);

} while (waitKey(10) < 0); // waitkey returns -1 if no key pressed and a positive value if pressed

return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-10-21 03:37:10 -0600

antonio gravatar image
  1. "whenever i add another line to put in a new hue,sat,value it overrides the other color so i only detect the one that is the last to be read by the compiler": In the following line you are using the same image as in the previous line (I mean the "thresholded).

    //inRange(hsvImage_2, Scalar(14, 0, 0), Scalar(16, 255, 255), thresholded);// Orange Color

Try to create a second thresholded image (say "thresholded2"), and use:

inRange(hsvImage_2, Scalar(14, 0, 0), Scalar(16, 255, 255), thresholded2);// Orange Color

2.In this link you can find a basic tutorial for color object tracking where a circle is drawn around a color object tracked using OpenCV (it also explains how to set the center coordinates of the circle).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-10-19 17:57:36 -0600

Seen: 1,563 times

Last updated: Oct 21 '13