Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I detect color and contour. if both colors are at distance then program works good detect contour for both color. but when colors having same border then is shows only one contour.

as shown in picture there are 3 contour but i want contour for each color separately. there is one contour for both read and yellow color, i want to make contour separate like others two

please guide me how can i do it.

Thank you very much in advance.

I detect color and contour. if both colors are at distance then program works good detect contour for both color. but when colors having same border then is shows only one contour.

as shown in picture there are 3 contour but i want contour for each color separately. there is one contour for both read and yellow color, i want to make contour separate like others twoas shown in picture there are 3 contour but i want contour for each color separately. there is one contour for both read and yellow color, i want to make contour separate like others two image description

please guide me how can i do it.

Thank you very much in advance.

I detect color and contour. if both colors are at distance then program works good detect contour for both color. but when colors having same border then is shows only one contour. as shown in picture there are 3 contour but i want contour for each color separately. there is one contour for both read and yellow color, i want to make contour separate like others two image description

#include <opencv2/core/core.hpp>

include <opencv2 highgui="" highgui.hpp="">

include "opencv2/imgproc/imgproc.hpp"

include <iostream>

using namespace cv; using namespace std;

//---------------------------------- Global variables Mat imgOriginal, imgConvert, imgProcessed,imgProcessed1, imgThresholded, imgDrawing, imgToProcess; RNG rng(12345); int c1 = 0, c2=1; // c1 = Center Point of Big Circle and c2 = Center Point of Small Circle //double aAreaBig = 0; //

int main( int argc, char** argv[] ) { cout.precision(3); //---------------------------------- Declare Windows and Initial Positions cvNamedWindow( "Original", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Processed", CV_WINDOW_AUTOSIZE );

//------------------------------ This default setting is for red color and variables

vector< vector<Point> > contours;
vector< Vec4i > hierarchy;

//---------------------------------- Read an image and check for invalid input int imgHeight = 480, //Default 480 imgWidth = 640; //Default 640 CvSize imgSize = cvSize(imgWidth,imgHeight); // The saiz of the image used. Default capture size - 640x480.

VideoCapture p_capwebcam(0); //capture the video from default webcam and check it
if ( !p_capwebcam.isOpened() ){cout << "Cannot open the Web Cam" << endl;return -1;}

cout<<"Display this first. Press enter to start. Then, adjust the HSV level. HSV for Red 179,160,29 "<<endl;
getchar();

//---------------------------------- Start Looping while (true) { //Grab image frame bool bSuccess = p_capwebcam.read(imgOriginal); // read a new frame from video if (!bSuccess){ //if not success, break loop cout << "Cannot read a frame from video stream" << endl;getchar(); break;}

//---------------------------------- Image pre-processing and then display

//Convert the captured frame from BGR to HSV
cvtColor(imgOriginal, imgConvert, COLOR_BGR2HSV); 
GaussianBlur(imgConvert,imgThresholded, Size(3,3), BORDER_DEFAULT );

//Checks if array elements lie between the elements of two other arrays.
inRange(imgThresholded, Scalar(0, 0, 90), Scalar(87, 118, 255), imgProcessed); 

inRange(imgThresholded, Scalar(0, 78, 90), Scalar(179, 252, 255), imgProcessed1); // red

add(imgProcessed, imgProcessed1, imgToProcess, noArray(), 8);

//morphological opening (removes small objects from the foreground)
erode(imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

//morphological closing (removes small holes from the foreground)
dilate( imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(10,10)) ); 
erode(imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(10,10)) );

imshow("Processed", imgToProcess);

//------------------------- Image Processing

//Find Contours
findContours(imgToProcess, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
vector<float>area( contours.size() );
vector<RotatedRect> minRect( 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]));
 }
/// Draw polygonal contour + bonding rects + circles
Mat imgDrawing = Mat::zeros( imgOriginal.size(), CV_8UC3 );

for( size_t i = 0; i< contours.size(); i++ )
 {
   Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
   drawContours( imgOriginal, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
   //rectangle( imgOriginal, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
   //circle( imgOriginal, center[i], (int)radius[i], color, 2, 8, 0 );
     Point2f rect_points[4]; minRect[i].points( rect_points );
   for( int j = 0; j < 4; j++ )
      line( imgOriginal, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );

   //int H = boundRect[0].height*0.0355;

   char height2[80];
   itoa(boundRect[i].height*0.0355, height2,10);
   //strcat(height2,"height");


}   


   imshow("Original",imgOriginal);


if (waitKey(30) == 27) {//wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
            cout << "ESC key is pressed by user" << endl;break;}

} //End While

} //End Main

please guide me how can i do it.

Thank you very much in advance.

I this is example image this is example code to detect the contour for red color and contour. if both colors are at distance then program works good detect contour for both color. but when colors having same border then orange which is shows only one contour. as shown joint in picture there are 3 contour but the picture. i want contour for each orange and for red color separately. there is one contour for both read and yellow color, i want to make contour separate like others two image descriptionseparately.

` #include <opencv2/core/core.hpp>

include <opencv2 highgui="" highgui.hpp="">"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>"iostream"

include "stdio.h"

include "string"

include <stdlib.h>

using namespace cv; using namespace std;

//---------------------------------- Global variables Mat imgOriginal, imgConvert, imgProcessed,imgProcessed1, imgThresholded, imgDrawing, imgToProcess; std; RNG rng(12345); int c1 = 0, c2=1; // c1 = Center Point of Big Circle and c2 = Center Point of Small Circle //double aAreaBig = 0; //

int main( int argc, char** argv[] ) { cout.precision(3); //---------------------------------- Declare Windows and Initial Positions main ()
{
cvNamedWindow( "Original", CV_WINDOW_AUTOSIZE ); "RGB", 1 );
//cvNamedWindow( "HSV", 1 );
//cvNamedWindow( "Binary", 1 ); //cvNamedWindow( "Binary1", 1 );
cvNamedWindow( "Processed", CV_WINDOW_AUTOSIZE );

//------------------------------ This default setting is for red color and variables"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 > contours; 
vector<Vec4i> hierarchy;

//---------------------------------- Read an image and check for invalid input int imgHeight = 480, //Default 480 imgWidth = 640; //Default 640 CvSize imgSize = cvSize(imgWidth,imgHeight); // The saiz of the image used. Default capture size - 640x480.

VideoCapture p_capwebcam(0); //capture the video from default webcam and check it
if ( !p_capwebcam.isOpened() ){cout << "Cannot open the Web Cam" << endl;return -1;}

cout<<"Display this first. Press enter to start. Then, adjust the HSV level. HSV for Red 179,160,29 "<<endl;
getchar();

//---------------------------------- Start Looping while (true) { //Grab image frame bool bSuccess = p_capwebcam.read(imgOriginal); // read a new frame from video if (!bSuccess){ //if not success, break loop cout << "Cannot read a frame from video stream" << endl;getchar(); break;}

//---------------------------------- Image pre-processing and then display

//Convert the captured frame from BGR to HSV
cvtColor(imgOriginal, imgConvert, COLOR_BGR2HSV); 
GaussianBlur(imgConvert,imgThresholded, Size(3,3), BORDER_DEFAULT );

//Checks if array elements lie between the elements of two other arrays.
inRange(imgThresholded, Scalar(0, 0, 90), Scalar(87, 118, 255), imgProcessed); 

inRange(imgThresholded, Scalar(0, 78, 90), Scalar(179, 252, 255), imgProcessed1); // red

add(imgProcessed, imgProcessed1, imgToProcess, noArray(), 8);

//morphological opening (removes small objects from the foreground)
erode(imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

//morphological closing (removes small holes from the foreground)
dilate( imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(10,10)) ); 
erode(imgToProcess, imgToProcess, getStructuringElement(MORPH_ELLIPSE, Size(10,10)) );

imshow("Processed", imgToProcess);

//------------------------- Image Processing

//Find Contours
findContours(imgToProcess, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
/// Approximate Point(0, 0)); //find contours to polygons + get bounding rects and circles
 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() );
vector<float>radius( contours.size() );
vector<float>area( contours.size() );
vector<RotatedRect> minRect( 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]));
 }
/// Draw polygonal contour + bonding rects + circles
 Mat imgDrawing drawing = Mat::zeros( imgOriginal.size(), imgToProcess.size(), CV_8UC3 );
 for( size_t int i = 0; i< contours.size(); i++ )
 {
   Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
   drawContours( imgOriginal, contours_poly, (int)i, drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
   //rectangle( imgOriginal, ellipse( drawing, minEllipse[i], color, 2, 8 );// ellipse
   rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
   //circle( imgOriginal, center[i], (int)radius[i], color, 2, 8, 0 );
     Point2f //Point2f rect_points[4]; minRect[i].points( rect_points );
   for( //for( int j = 0; j < 4; j++ )
    //   line( imgOriginal, drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );

   //int H = boundRect[0].height*0.0355;

   char height2[80];
   itoa(boundRect[i].height*0.0355, height2,10);
   //strcat(height2,"height");


}   


   imshow("Original",imgOriginal);


if (waitKey(30) == 27) {//wait   }
imshow("Contour",drawing);  


//wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
            cout << "ESC key is pressed by user" << endl;break;}
 
cvWaitKey();  
return 0;

} //End While} `

} //End Main

please guide me how can i do it.

Thank you very much in advance.

click to hide/show revision 5
No.5 Revision

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>

#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

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;
0;    
 }

} `