Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to detect contour for two or more then two specific color ?

Hello, I want to detect contours for two colors simultaneously, here in code there is one contour for one color is detected but i want the same for second color. Please guide me.

int main ()
{
//cvNamedWindow( "RGB", 1 );
cvNamedWindow( "HSV", 1 );
//cvNamedWindow( "Binary", 1 );
cvNamedWindow( "LargestContour", 1 );
cvNamedWindow( "Final", 1 );
Mat img,hsv,binary;

img = imread("C:\\Users\\ankit\\Downloads\\wallpaper\\p.png"); //change this path according to your image file path

// imshow("RGB",img);

//convert RGB image into HSV image  
cvtColor(img, hsv, CV_BGR2HSV);  
imshow("HSV",hsv);  

//get binary image  
inRange(hsv, Scalar(0, 255, 255), Scalar(179, 255, 255), binary);

// imshow("Binary",binary);

//find contours from binary image  
int i;  
vector< vector<Point> > contours;  
findContours(binary, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); //find contours  
vector<double> areas(contours.size());  
//find largest contour area  
for(i = 0; i < contours.size(); i++)  
{  
    areas[i] = contourArea(Mat(contours[i]));  
}  
//get index of largest contour  
double max;  
Point maxPosition;  
minMaxLoc(Mat(areas),0,&max,0,&maxPosition);  
//draw largest contour.  
drawContours(binary, contours, maxPosition.y, Scalar(255), CV_FILLED);  
imshow("LargestContour",binary);  

//draw bounding rectangle around largest contour  
Point center;  
Rect r;  
if (contours.size() >= 1)  
{  
    r = boundingRect(contours[maxPosition.y]);  
    rectangle(img, r.tl(),r.br(), CV_RGB(255, 0, 0), 3, 8, 0); //draw rectangle  
}  
//get centroid  
center.x = r.x + (r.width/2);  
center.y= r.y + (r.height/2);  

//print x and y co-ordinates on image  
char x[15],y[6];  
itoa(center.x,x,10);  
itoa(center.y,y,10);  
strcat(x,",");  
putText(img, strcat(x,y), center, FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255,0,0), 1, CV_AA);  
imshow("Final",img);  

//wait for key press  
cvWaitKey();  
return 0;

}

Thank you very much in advance.

click to hide/show revision 2
No.2 Revision

how to detect contour contours for two or more then than two specific color colors ?

Hello, I want to detect contours for two colors simultaneously, here in code there is one contour for one color is detected but i want the same for second color. Please guide me.

int main () 
{
//cvNamedWindow( "RGB", 1 );
cvNamedWindow( "HSV", 1 );
//cvNamedWindow( "Binary", 1 );
cvNamedWindow( "LargestContour", 1 );
cvNamedWindow( "Final", 1 );
Mat img,hsv,binary;

 img = imread("C:\\Users\\ankit\\Downloads\\wallpaper\\p.png"); //change this path according to your image file path

path // imshow("RGB",img);

 //convert RGB image into HSV image
 cvtColor(img, hsv, CV_BGR2HSV);
 imshow("HSV",hsv);
 //get binary image
 inRange(hsv, Scalar(0, 255, 255), Scalar(179, 255, 255), binary);

binary); // imshow("Binary",binary);

 //find contours from binary image
 int i;
 vector< vector<Point> > contours;
 findContours(binary, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); //find contours
 vector<double> areas(contours.size());
 //find largest contour area
 for(i = 0; i < contours.size(); i++)
 {
  areas[i] = contourArea(Mat(contours[i]));
 }
 //get index of largest contour
 double max;
 Point maxPosition;
 minMaxLoc(Mat(areas),0,&max,0,&maxPosition);
 //draw largest contour.
 drawContours(binary, contours, maxPosition.y, Scalar(255), CV_FILLED);
 imshow("LargestContour",binary);
 //draw bounding rectangle around largest contour
 Point center;
 Rect r;
 if (contours.size() >= 1)
 {
  r = boundingRect(contours[maxPosition.y]);
  rectangle(img, r.tl(),r.br(), CV_RGB(255, 0, 0), 3, 8, 0); //draw rectangle
 }
 //get centroid
 center.x = r.x + (r.width/2);
 center.y= r.y + (r.height/2);
 //print x and y co-ordinates on image
 char x[15],y[6];
 itoa(center.x,x,10);
 itoa(center.y,y,10);
 strcat(x,",");
 putText(img, strcat(x,y), center, FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255,0,0), 1, CV_AA);
 imshow("Final",img);
 //wait for key press
 cvWaitKey();
 return 0;
0;
}

}

Thank you very much in advance.

how to detect contours for two or more than two specific colors ?

Hello, I want to detect contours for two colors simultaneously, here in code there is one contour for one color is detected but i want the same for second color. Please guide me.

int main ()    
 {    
    //cvNamedWindow( "RGB", 1 );   
    cvNamedWindow( "HSV", 1 );   
    //cvNamedWindow( "Binary", 1 );  
    cvNamedWindow( "LargestContour", 1 );  
    cvNamedWindow( "Final", 1 );  
    Mat img,hsv,binary;  

    img = imread("C:\\Users\\ankit\\Downloads\\wallpaper\\p.png"); //change this path according to your image file path  
   // imshow("RGB",img);  

    //convert RGB image into HSV image  
    cvtColor(img, hsv, CV_BGR2HSV);  
    imshow("HSV",hsv);  

    //get binary image  
    inRange(hsv, Scalar(0, 255, 255), Scalar(179, 255, 255), binary);  
   // imshow("Binary",binary);  

    //find contours from binary image  
    int i;  
    vector< vector<Point> > contours;  
    findContours(binary, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); //find contours  
    vector<double> areas(contours.size());  
    //find largest contour area  
    for(i = 0; i < contours.size(); i++)  
    {  
        areas[i] = contourArea(Mat(contours[i]));  
    }  
    //get index of largest contour  
    double max;  
    Point maxPosition;  
    minMaxLoc(Mat(areas),0,&max,0,&maxPosition);  
    //draw largest contour.  
    drawContours(binary, contours, maxPosition.y, Scalar(255), CV_FILLED);  
    imshow("LargestContour",binary);  

    //draw bounding rectangle around largest contour  
    Point center;  
    Rect r;  
    if (contours.size() >= 1)  
    {  
        r = boundingRect(contours[maxPosition.y]);  
        rectangle(img, r.tl(),r.br(), CV_RGB(255, 0, 0), 3, 8, 0); //draw rectangle  
    }  
    //get centroid  
    center.x = r.x + (r.width/2);  
    center.y= r.y + (r.height/2);  

    //print x and y co-ordinates on image  
    char x[15],y[6];  
    itoa(center.x,x,10);  
    itoa(center.y,y,10);  
    strcat(x,",");  
    putText(img, strcat(x,y), center, FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(255,0,0), 1, CV_AA);  
    imshow("Final",img);  

    //wait for key press  
    cvWaitKey();  
    return 0;    
 }

Thank you very much in advance.