1 | initial version |
The following C++ code works OK. It finds all of the 8-gons, there's three of them:
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat stopsign_img = imread("stop.png", CV_LOAD_IMAGE_GRAYSCALE);
threshold(stopsign_img, stopsign_img, 127, 255, THRESH_BINARY);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(stopsign_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> > contours_poly(contours.size());
for (int i = 0; i < contours.size(); i++)
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
Mat stopsign_contours = Mat::zeros(stopsign_img.size(), CV_8UC3);
for (int i = 0; i < contours_poly.size(); i++)
{
if(1 == contours_poly[i].size())
{
// a point
line(stopsign_contours, contours_poly[i][0], contours_poly[i][0], Scalar(0, 255, 0), 1, 8, 0);
}
else if (2 == contours_poly[i].size())
{
// a line segment
line(stopsign_contours, contours_poly[i][0], contours_poly[i][1], Scalar(0, 0, 255), 1, 8, 0);
}
else
{
if (8 == contours_poly[i].size())
cout << "8-gon found" << endl;
// an n-gon, where n = contours_poly[i].size()
for (int j = 0; j < contours_poly[i].size() - 1; j++)
line(stopsign_contours, contours_poly[i][j], contours_poly[i][j + 1], Scalar(255, 0, 0), 1, 8, 0);
line(stopsign_contours, contours_poly[i][contours_poly[i].size() - 1], contours_poly[i][0], Scalar(255, 0, 0), 1, 8, 0);
}
}
imshow("stopsign_contours", stopsign_contours);
waitKey(0);
destroyAllWindows();
return 0;
}
2 | No.2 Revision |
The following C++ code works OK. It finds all of the 8-gons, there's three of them:them. To differentiate between an octagon and some other shaped 8-gon, you would make sure that all 8 angles are roughly the same (2*pi / 8 radians).
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat stopsign_img = imread("stop.png", CV_LOAD_IMAGE_GRAYSCALE);
threshold(stopsign_img, stopsign_img, 127, 255, THRESH_BINARY);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(stopsign_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> > contours_poly(contours.size());
for (int i = 0; i < contours.size(); i++)
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
Mat stopsign_contours = Mat::zeros(stopsign_img.size(), CV_8UC3);
for (int i = 0; i < contours_poly.size(); i++)
{
if(1 == contours_poly[i].size())
{
// a point
line(stopsign_contours, contours_poly[i][0], contours_poly[i][0], Scalar(0, 255, 0), 1, 8, 0);
}
else if (2 == contours_poly[i].size())
{
// a line segment
line(stopsign_contours, contours_poly[i][0], contours_poly[i][1], Scalar(0, 0, 255), 1, 8, 0);
}
else
{
if (8 == contours_poly[i].size())
cout << "8-gon found" << endl;
// an n-gon, where n = contours_poly[i].size()
for (int j = 0; j < contours_poly[i].size() - 1; j++)
line(stopsign_contours, contours_poly[i][j], contours_poly[i][j + 1], Scalar(255, 0, 0), 1, 8, 0);
line(stopsign_contours, contours_poly[i][contours_poly[i].size() - 1], contours_poly[i][0], Scalar(255, 0, 0), 1, 8, 0);
}
}
imshow("stopsign_contours", stopsign_contours);
waitKey(0);
destroyAllWindows();
return 0;
}
3 | No.3 Revision |
The following C++ code works OK. It finds all of the 8-gons, there's three of them. To differentiate between an octagon and some other shaped 8-gon, you would make sure that the 8-gon is convex, and that all 8 angles are roughly the same (2*pi / 8 radians).
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat stopsign_img = imread("stop.png", CV_LOAD_IMAGE_GRAYSCALE);
threshold(stopsign_img, stopsign_img, 127, 255, THRESH_BINARY);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(stopsign_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> > contours_poly(contours.size());
for (int i = 0; i < contours.size(); i++)
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
Mat stopsign_contours = Mat::zeros(stopsign_img.size(), CV_8UC3);
for (int i = 0; i < contours_poly.size(); i++)
{
if(1 == contours_poly[i].size())
{
// a point
line(stopsign_contours, contours_poly[i][0], contours_poly[i][0], Scalar(0, 255, 0), 1, 8, 0);
}
else if (2 == contours_poly[i].size())
{
// a line segment
line(stopsign_contours, contours_poly[i][0], contours_poly[i][1], Scalar(0, 0, 255), 1, 8, 0);
}
else
{
if (8 == contours_poly[i].size())
cout << "8-gon found" << endl;
// an n-gon, where n = contours_poly[i].size()
for (int j = 0; j < contours_poly[i].size() - 1; j++)
line(stopsign_contours, contours_poly[i][j], contours_poly[i][j + 1], Scalar(255, 0, 0), 1, 8, 0);
line(stopsign_contours, contours_poly[i][contours_poly[i].size() - 1], contours_poly[i][0], Scalar(255, 0, 0), 1, 8, 0);
}
}
imshow("stopsign_contours", stopsign_contours);
waitKey(0);
destroyAllWindows();
return 0;
}