Rashmi Sargar
Rashmi Sargar

Reputation: 61

find out dimension of object in image in cpp

I have a image which contains object. I have to find out the dimensions of the object (in pixels). Does anyone know how I could calculate that using cpp?

Upvotes: 1

Views: 210

Answers (2)

Rashmi Sargar
Rashmi Sargar

Reputation: 61

I got the solution This is the code..

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;

Upvotes: 1

Rahul Banerjee
Rahul Banerjee

Reputation: 2363

I am a search engine. Using me, you can locate all sorts of useful information, like API references manuals and even sample code.

Have a nice day!

Upvotes: 2

Related Questions