measuring height of two color with same border. [closed]
hello, I want to measure the height of two color, I am using this code for it.
#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(int, char**)
{
cvNamedWindow( "RGB", 1 );
//cvNamedWindow( "HSV", 1 );
//cvNamedWindow( "Binary", 1 );
//cvNamedWindow( "Binary1", 1 );
cvNamedWindow( "Contour", 1 );
//cvNamedWindow( "Final", 1 );
Mat img,hsv,binary,binary1,imgToProcess, gray, dst, abs_dst;
img = imread("C:\\Users\\ankit\\Downloads\\wallpaper\\test img\\16.png"); //change this path according to your image file path
imshow("RGB",img);
GaussianBlur( img, gray, Size(3,3), 0, 0, BORDER_DEFAULT );
//convert RGB image into HSV image
cvtColor(img, hsv, CV_BGR2HSV);
//get binary image
inRange(hsv, Scalar(70, 105, 0), Scalar(226,255,110), binary);
imshow( "binary", binary );
inRange(hsv, Scalar(0,251,29), Scalar(9,255,158), binary1);
//binary.copyTo(binary1);
imshow("Binary1",binary1);
add(binary1, binary, imgToProcess, noArray(), 8);
//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 );
cout<< "height =" << boundRect[i].height <<endl;
cout<< "width =" << boundRect[i].width <<endl;
cout<< "x =" << boundRect[i].x <<endl;
cout<< "y =" << boundRect[i].y <<endl;
cout<< "pt0 (x, y) =" << boundRect[i].tl().x << ", " << boundRect[i].tl().y << endl;
cout<< "pt1 (x, y) =" << boundRect[i].tl().x + boundRect[i].width << ", " << boundRect[i].tl().y << endl;
cout<< "pt2 br (x, y) =" << boundRect[i].br().x << ", " << boundRect[i].br().y << endl; // cross checking of top right using bottom right coordinates.
cout<< "pt3 (x, y) =" << boundRect[i].tl().x << ", " << boundRect[i].tl().y + boundRect[i].height << endl;
}
imshow("Contour",drawing);
cvWaitKey();
return 0;
}
i am facing problem that how can i detect only two rectangle for two colors only and having the parameters of those two colors only. in my case i am having lots of contours but i dont want that.I want only ...