OpenCV find position of points in image

410 Views Asked by At

I havent found a good answer for this problem yet. Im trying to find the position of a few points in a image. Most answers use a simple threshold. Although this is creating problems for me as contrast between the points and background changes making the threshold value be different. Otsu solves this but sometimes the threshold is too low making noise a big problem. Im trying to use adaptiveThresholding and that gives me this:

It seems to work reliably, only problem is i need to now find the position of these points. FindContours gives me multiple contours per point for some reason. Is there an algorithm that can maybe simply label the regions in the binary image? Code:

vector<Point2f> pointsImage;

Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);

Mat blur;
GaussianBlur(gray, blur, cv::Size(5, 5), 0);

Mat bw;
cv::adaptiveThreshold(blur, bw, 255, cv::AdaptiveThresholdTypes::ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 7, 5);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(bw, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++ ) {

    if (isContourConvex(contours[i])) {

        Moments M = moments(contours[i]);
        Point2f c(M.m10/M.m00, M.m01/M.m00);

        if (!isnan(c.x) && !isnan(c.y) && !isinf(c.x) && !isinf(c.y)) pointsImage.push_back(c);

    }

}
0

There are 0 best solutions below