Find the center point of an object in a black-whit

2019-08-12 14:04发布

I have the image below. My aim is to find x,y values of the center point of the object. I tried Image moments but I couldn't find any x,y values. How can I do that?

Original image

The center point shoud be the red point or something close to it.

enter image description here

1条回答
叼着烟拽天下
2楼-- · 2019-08-12 14:32

In the link you posted, you can find the center of the image here:

///  Get the mass centers:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
    { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }

You can find the center of your image like this:

#include "opencv2\opencv.hpp"
using namespace cv;

int main()
{
    Mat1b gray = imread("path_to_image", IMREAD_GRAYSCALE);

    Moments mu = moments(gray, true);
    Point center;
    center.x = mu.m10 / mu.m00;
    center.y = mu.m01 / mu.m00;

    Mat3b res;
    cvtColor(gray, res, CV_GRAY2BGR);

    circle(res, center, 2, Scalar(0,0,255));

    imshow("Result", res);
    waitKey();

    return 0;
}

The resulting image is:

Resulting image

查看更多
登录 后发表回答