1 | initial version |
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 boundingRect compute is wrong.
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;
}
2 | No.2 Revision |
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 compute calculation is wrong.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;
}