我有一个包含对象的图像。我必须找出对象的尺寸(以像素为单位)。有谁知道我如何使用cpp计算?
问问题
117 次
2 回答
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;
于 2013-03-08T09:42:54.750 回答