1 | initial version |
you can try the code below which finds a center point between all contours and sorts contours circularly ( maybe this will be a starting point )
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Point center;
struct contour_sorter
{
bool operator ()(const vector<Point>& a, const vector<Point>& b)
{
double angle1 = atan2(a[0].y - center.y, a[0].x - center.x) * 180.0 / CV_PI;
double angle2 = atan2(b[0].y - center.y, b[0].x - center.x) * 180.0 / CV_PI;
return (angle1 < angle2);
}
};
int main(int argc, char** argv)
{
Mat src = imread("15931824191786043.jpg");
Mat bw;
cvtColor(src, bw, COLOR_BGR2GRAY);
bw = bw < 127;
Rect r = boundingRect(bw);
center = (r.tl() + r.br()) * 0.5;
// Find contours
vector<vector<Point> > contours;
vector<Point> allcontours;
findContours(bw, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
sort(contours.begin(), contours.end(), contour_sorter());
for (size_t i = 0; i < contours.size()-1; i++)
{
drawContours(src, contours, i, Scalar(0, 0, 255), 1);
imshow("src", src);
waitKey(0);
}
waitKey(0);
}