Ask Your Question
0

Bounding box 2

asked 2013-01-26 11:03:30 -0600

Tomazi gravatar image

updated 2013-01-26 11:05:03 -0600

Hey peps with a help of some online resources and this forum i created a program that grabs an image process it all the way till contours are drown and the next step i wonted to undertake are the Bounding Boxes. Everything works all the way till i attempt drawing Bounding boxes. I put together a code for this but no success can anyone look at it ant explain where am going wrong

    #include "iostream"
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>
#include <iostream> 
#include <vector>
#include <string> 
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp> // Video write


using namespace cv;
using namespace std;

Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
int thresh=100, max_thresh=255;


int main(int argc, char** argv) {
//Load Image
image =imread(argv[1]);

//Convert Image to gray & blur it
cvtColor( image, 
    image_gray, 
    CV_BGR2GRAY );

blur( image_gray, 
    image_gray2,
    Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2, 
    threshold_output, 
    thresh, 
    max_thresh, 
    THRESH_BINARY);

//2D Container
vector<vector<Point>> contours;

//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
    contours, // a vector of contours
    CV_RETR_EXTERNAL,// retrieve the external contours
    CV_CHAIN_APPROX_NONE,
    Point(0, 0)); // all pixels of each contours    

// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));
drawContours(result,contours,
    -1, // draw all contours
    Scalar(0), // in black
    2); // with a thickness of 2

//Bounding Box
vector<Point> points;

Mat_<uchar>::iterator it = result.begin<uchar>(); Mat_<uchar>::iterator end = result.end<uchar>(); for (; it != end; ++it) { if (*it) points.push_back(it.pos()); }

RotatedRect box = minAreaRect(Mat(points));

Point2f vertices[4];
box.points(vertices);

for (int i = 0; i < 4; ++i)
    {
        line(result, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    }



Rect roi;
roi.x = box.center.x - (box.size.width / 2);
roi.y = box.center.y - (box.size.height / 2);
roi.width = box.size.width;
roi.height = box.size.height;   

//Create Window
char* DisplayWindow = "Source";
namedWindow(DisplayWindow, CV_WINDOW_AUTOSIZE);
imshow(DisplayWindow, result);



waitKey(5000);
return 1;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-01-26 13:25:30 -0600

updated 2013-01-26 13:28:34 -0600

You don't need to convert.

Mat_<uchar>::iterator it = result.begin<uchar>(); Mat_<uchar>::iterator end = result.end<uchar>(); for (; it != end; ++it) { if (*it) points.push_back(it.pos()); }

your method for the boundingRect calculation is incorrect.

Rect roi;
roi.x = box.center.x - (box.size.width / 2);
roi.y = box.center.y - (box.size.height / 2);
roi.width = box.size.width;
roi.height = box.size.height;

Revised Code:

#define bh_red Scalar(0,0,255)
#define bh_green Scalar(0,255,0)
#define bh_blue Scalar(255,0,0)
#define bh_yellow Scalar(0,255,255)

void bhDrawColorLabel(Mat src,string title,Scalar color, int pos,int size=20)
{
    int lineSize = 20;
    Point offset( 10,10);
    int position = pos * size;
    line(src,Point(offset.x,position+offset.y),Point(offset.x+lineSize,position+offset.y),color);
    putText(src,title,Point(offset.x+lineSize,position+10),CV_FONT_HERSHEY_COMPLEX,0.4,Scalar(255,255,255));
}

int main( int argc, char *argv[] )  
{  

Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
int thresh=100, max_thresh=255;


//Load Image
//image =imread(argv[1]);
image_gray =imread("c:\\test100.bmp",0);


blur( image_gray, 
    image_gray2,
    Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2, 
    threshold_output, 
    thresh, 
    max_thresh, 
    THRESH_BINARY);

//2D Container
vector<vector<Point>> contours;

//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
    contours, // a vector of contours
    CV_RETR_EXTERNAL,// retrieve the external contours
    CV_CHAIN_APPROX_NONE,
    Point(0, 0)); // all pixels of each contours    

// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));
drawContours(result,contours,
    -1, // draw all contours
    Scalar(0), // in black
    2); // with a thickness of 2

//Bounding Box
//vector<Point> points;
//Mat_<uchar>::iterator it = result.begin<uchar>(); Mat_<uchar>::iterator end = result.end<uchar>(); for (; it != end; ++it) { if (*it) points.push_back(it.pos()); }

//RotatedRect box = minAreaRect(Mat(points));
RotatedRect box = minAreaRect(contours[0]);

Point2f vertices[4];
box.points(vertices);
 Mat ViewMat;
 cvtColor(image_gray,ViewMat,CV_GRAY2BGR);
for (int i = 0; i < 4; ++i)
    {
        line(ViewMat, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    }



Rect your_roi;
your_roi.x = box.center.x - (box.size.width / 2);
your_roi.y = box.center.y - (box.size.height / 2);
your_roi.width = box.size.width;
your_roi.height = box.size.height;   
rectangle(ViewMat,your_roi,bh_red);

//Create Window
Rect roi=  boundingRect(contours[0]);
rectangle(ViewMat,roi,bh_blue);

rectangle(ViewMat,box.boundingRect(),bh_yellow);

bhDrawColorLabel(ViewMat,"your method",bh_red,0);
bhDrawColorLabel(ViewMat,"your method2",bh_green,1);
bhDrawColorLabel(ViewMat,"boundingRect",bh_blue,2);
bhDrawColorLabel(ViewMat,"RotatedRect.boundingRect",bh_yellow,3);

char* DisplayWindow = "Source";
namedWindow(DisplayWindow, CV_WINDOW_AUTOSIZE);
imshow(DisplayWindow, ViewMat);



waitKey(0);
return 1;
}

image description

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-26 11:03:30 -0600

Seen: 3,580 times

Last updated: Jan 26 '13