// 【1】讀入一張圖片,載入圖像
Mat srcImage = imread("13.jpg");
cvtColor(srcImage, srcImage, CV_BGR2GRAY);
threshold(srcImage, srcImage, 180, 255, CV_THRESH_BINARY);
// namedWindow("binaryImage");
vector> vecAllContours;
findContours(srcImage, vecAllContours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat contourImage(srcImage.size(), CV_8UC1, Scalar(0));
drawContours(contourImage, vecAllContours, -1, Scalar(255), 2);
namedWindow("contourImage");
imshow("contourImage", contourImage);
vector >::const_iterator itc = vecAllContours.begin();
for (; itc != vecAllContours.end(); itc++)
{
if (itc->size() > 5)
{
Moments mom = moments(Mat(*itc));
int x = mom.m10 / mom.m00;
int y = mom.m01 / mom.m00;
cout << "x :" << x << endl;
cout << "y :" << y << endl;
}
}