Ask Your Question
1

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

asked 2015-04-14 10:26:41 -0600

Akki gravatar image

updated 2015-10-27 09:21:26 -0600

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.

edit retag flag offensive close merge delete

Comments

1

just use the inRange() function again with the second color...

theodore gravatar imagetheodore ( 2015-04-14 10:47:25 -0600 )edit

Thank you very much for comment. I have already tried with it, but its not working fine. its give error Native' has exited with code 0 (0x0).

Akki gravatar imageAkki ( 2015-04-14 13:42:39 -0600 )edit

can you write the inRange() command that you tried and gave this error.

theodore gravatar imagetheodore ( 2015-04-14 16:25:50 -0600 )edit

inRange(hsv, Scalar(115, 0, 0), Scalar(156, 256, 256), binary); inRange(hsv, Scalar(171, 0, 0), Scalar(256, 256, 256), binary); imshow("Binary",binary);

like this i tried to give command. it not working as its show me only one color detection.

Akki gravatar imageAkki ( 2015-04-15 03:44:08 -0600 )edit

@Akki you cannot have 256 value the maximum value you can use is 255

theodore gravatar imagetheodore ( 2015-04-15 08:58:37 -0600 )edit

thank you for comment. I change it 256 to 255, but still its take only first inRange() function and show contour only for it. not for both.

Akki gravatar imageAkki ( 2015-04-15 11:09:10 -0600 )edit

it work fine with adding two frame for different color and then draw contour for that frame. :)

Akki gravatar imageAkki ( 2015-04-15 14:51:35 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-04-16 10:27:44 -0600

Akki gravatar image

updated 2015-04-16 15:55:32 -0600

theodore gravatar image

this is example image this is example code to detect the contour for red color and for orange which is shown joint in the picture. i want contour for orange and for red color separately.

#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "stdio.h"
#include "string"
#include <stdlib.h>

using namespace cv;
using namespace std;
RNG rng(12345);
int main ()    
 {    
    cvNamedWindow( "RGB", 1 );   
    //cvNamedWindow( "HSV", 1 );   
    //cvNamedWindow( "Binary", 1 ); 
    //cvNamedWindow( "Binary1", 1 );
    cvNamedWindow( "Contour", 1 );  
    //cvNamedWindow( "Final", 1 );  
    Mat img,hsv,binary,binary1,imgToProcess;  

    img = imread("C:\\Users\\ankit\\Downloads\\wallpaper\\2.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, 85, 241), Scalar(18, 255, 255), binary);
        // imshow("Binary",binary);
    inRange(hsv, Scalar(171, 0, 0), Scalar(255, 255, 255), binary1);

    //binary.copyTo(binary1);
    //imshow("Binary1",binary1);
     add(binary1, binary, imgToProcess, noArray(), 8);

     //absdiff(binary1, binary, imgToProcess);
     imshow("Binary2",imgToProcess);


    //find contours from binary image  
    vector< vector<Point> > contours; 
    vector<Vec4i> hierarchy;
    findContours(imgToProcess, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //find contours  

    vector<vector<Point> > contours_poly( contours.size() );
    vector<RotatedRect> minRect( contours.size() );
    vector<RotatedRect> minEllipse( contours.size() );
    vector<Rect> boundRect( contours.size() );
    vector<float>radius( contours.size() );
    vector<float>area( contours.size() );
    vector<Point2f>center( contours.size() );

    for( size_t i = 0; i < contours.size(); i++ )
     { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
       boundRect[i] = boundingRect( Mat(contours_poly[i]) );
       minEnclosingCircle( contours_poly[i], center[i], radius[i] );
       area[i]= contourArea(Mat(contours_poly[i]));
     }


    Mat drawing = Mat::zeros( imgToProcess.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
       ellipse( drawing, minEllipse[i], color, 2, 8 );// ellipse
       rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
    //Point2f rect_points[4]; minRect[i].points( rect_points );
     //for( int j = 0; j < 4; j++ )
       //   line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );

         }
    imshow("Contour",drawing);  


    //wait for key press  
    cvWaitKey();  
    return 0;    
 }
edit flag offensive delete link more

Comments

Please add the original image to allow others to do some tests

essamzaky gravatar imageessamzaky ( 2015-04-16 10:51:08 -0600 )edit

thank you very much for comment. original image means code or what??

Akki gravatar imageAkki ( 2015-04-16 11:07:58 -0600 )edit

i edit the code, which i used to detect the color. you have to give HSV value for detecting color to it.

Akki gravatar imageAkki ( 2015-04-16 11:12:06 -0600 )edit

i mean upload the your input image "C:\Users\ankit\Downloads\wallpaper\p.png"

essamzaky gravatar imageessamzaky ( 2015-04-16 12:03:56 -0600 )edit

@Akki instead of adding the inRange() extracted masks and then apply any processing procedure, do the opposite first process individually the masks and then add them or do whatever you want.

theodore gravatar imagetheodore ( 2015-04-16 16:00:00 -0600 )edit

Thank you very much for comment. can you explain little bit. and please advice which algorithm is good to do color detection when both color has same border. how to do it more accurately. because I am trying to detect color form pipette, it seem very difficult to get accurate color detection and contour is always fluctuated not be stable for one color.

Akki gravatar imageAkki ( 2015-04-17 13:18:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-14 10:26:41 -0600

Seen: 10,385 times

Last updated: Apr 16 '15