# bounding rect around detected eye region

Hi

I'm trying to do the following :

1.detect face and then eyes
2. after doing somw threshol to the detected eye region obtain morphologyEx (open) of eye region . 3. pass the result of morphologyEx (open) so that I can bound the eye region with a rectangle like this image .

using function below I'm not getting this result .

how can this be done ?

Thanks for help

void find_contour(Mat image)
{

Mat src_mat, gray_mat, canny_mat;
Mat contour_mat;
Mat bounding_mat;

contour_mat = image.clone();
bounding_mat = image.clone();

cvtColor(image, gray_mat, CV_GRAY2BGR);

// apply canny edge detection

Canny(gray_mat, canny_mat, 30, 128, 3, false);

//3. Find & process the contours
//3.1 find contours on the edge image.

vector< vector< cv::Point> > contours;
findContours(canny_mat, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

//3.2 draw contours & property value on the source image.

double area, length;
drawContours(contour_mat, contours, -1, cv::Scalar(0), 2);  //draw contours on the image

//3.3 find bounding of each contour, and draw it on the source image.

for (int i = 0; i < contours.size(); ++i)
{

Rect rect;

//compute the bounding rect, rotated bounding rect, minum enclosing circle.

rect = boundingRect(contours[i]);

//draw them on the bounding image.

cv::rectangle(bounding_mat, rect, Scalar(255, 0, 0), 2);

}

imshow("Bounding ", bounding_mat);

}

edit retag close merge delete

Sort by ยป oldest newest most voted

i think you need just a simple function like

Rect find_boundingRect( Mat image ) // returns bounding box of non-zero pixels
{
Mat Points;
findNonZero( image, Points );
Rect _boundingRect = boundingRect( Points );
return _boundingRect;
}


instead of your void find_contour(Mat image)

you can understand the usage by testing the code below. i tried to explain by comments.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

Rect find_boundingRect( Mat image )
{
Mat Points;
findNonZero( image, Points );
Rect _boundingRect = boundingRect( Points );
return _boundingRect;
}
int main( int argc, char** argv )
{
char* filename = argc >= 2 ? argv[1] : (char*)"14521348155732744.jpeg";

Mat mSource_Bgr,mSource_Gray,mThreshold;

cvtColor( mSource_Bgr, mSource_Gray, COLOR_BGR2GRAY );

// note #1
// here i assume you find eye location and do some image processing
// eyeRect is just to simulate it
Rect eyeRect( 5, 5, mSource_Bgr.cols - 10, mSource_Bgr.rows -10 );
Mat mEye = mSource_Gray( eyeRect );

threshold( mEye, mThreshold, 127, 255, THRESH_BINARY_INV );

Rect _boundingRect = find_boundingRect( mThreshold );
_boundingRect.x += eyeRect.x; // see note #1
_boundingRect.y += eyeRect.y; // see note #1

rectangle( mSource_Bgr, _boundingRect, Scalar(0,255,0), 2 );

imshow("Result",mSource_Bgr);
waitKey(0);

return 0;
}


- --

more

@sturkmen thanks very much for great answer. I will try it and update you thanks again

( 2016-01-07 06:01:36 -0600 )edit

How the width and height of the _boundingRect can be changed ? I tried to change values of Rect eyeRect( 5, 5, mSource_Bgr.cols - 10, mSource_Bgr.rows -10 ); the result was not so different

( 2016-01-07 08:39:03 -0600 )edit

Official site

GitHub

Wiki

Documentation