Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Bounding box 2

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. 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;

}

Bounding box 2

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;

}