使用C ++ opencv在黑白图像中查找对象的中心点 [英] Find the center point of an object in a black-white image using C++ opencv

查看:1254
本文介绍了使用C ++ opencv在黑白图像中查找对象的中心点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的图片。我的目标是找到对象中心点的x,y值。我试过中,您可以在这里找到图像的中心:

  ///获取质量中心:
vector< Point2f> mc(contours.size());
for(int i = 0; i< contours.size(); i ++)
{mc [i] = Point2f(mu [i] .m10 / mu [i] .m00,mu [我] .m01 / mu [i] .m00); }

你可以找到你的图像中心:

  #includeopencv2 \ openncv.hpp
using namespace cv;

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

时刻mu =时刻(灰色,真实);
点中心;
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(结果,res);
waitKey();

返回0;
}

结果图片是:




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?

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

解决方案

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:

这篇关于使用C ++ opencv在黑白图像中查找对象的中心点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆