找出cpp中图像中对象的维度

时间:2013-03-08 06:37:52

标签: c++ image opencv

我有一个包含对象的图像。我必须找出对象的尺寸(以像素为单位)。有谁知道如何使用cpp计算出来?

2 个答案:

答案 0 :(得分:2)

我是一名搜索引擎。使用我,您可以找到各种有用的信息,例如API references manuals甚至sample code

度过美好的一天!

答案 1 :(得分:1)

我得到了解决方案这是代码..

Mat src = imread("C:/ball.jpg");
    if (src.empty())
        return -1;

    Mat hsv;
    cvtColor(src, hsv, CV_BGR2HSV);

    Mat bw;
    inRange(hsv, Scalar(19, 204, 153), Scalar(27, 255, 255), bw);

    vector<vector<Point> > contours;

    findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
     Mat dst = Mat::zeros(src.size(), src.type());
      Mat coordinates = Mat::zeros(src.size(), src.type());
    drawContours(dst, contours, -1, Scalar::all(255), CV_FILLED);

    dst &= src;
    imshow("src", src);
    imshow("dst", dst);


     for(unsigned int i=0;i<contours.size();i++)
   {
     cout << "# of contour points: " << contours[i].size() << endl ;

     for(unsigned int j=0;j<contours.size();j++)
     {
         cout << "Point(x,y)=" << contours[i][j] << endl;
     }

     cout << " Area: " << contourArea(contours[i]) << endl;
}
     imshow("coordinates",coordinates);
    waitKey(0);

    return 0;