最大輪郭を取得します.
4270 ワード
opencv 3画像中の輪郭の面積を計算します.http://blog.csdn.net/qq_23880193/articale/detail/4925999
http://blog.csdn.net/davebobo/article/details/52583167
http://blog.csdn.net/davebobo/article/details/52583167
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main(){
Mat image = cvLoadImage("group.jpg");
Mat grayImage;
cvtColor(image, grayImage, CV_BGR2GRAY);
//
Mat binaryImage;
threshold(grayImage, binaryImage, 90, 255, CV_THRESH_BINARY);
// , 255 ( ), 0
Mat reverseBinaryImage;
bitwise_not(binaryImage, reverseBinaryImage);
vector <vector >contours;
findContours(reverseBinaryImage,
contours, //
CV_RETR_EXTERNAL, //
CV_CHAIN_APPROX_NONE); //
//
Mat result(reverseBinaryImage.size(), CV_8U, Scalar(255));
drawContours(result, contours,
-1, //
Scalar(0), //
2); // 2
namedWindow("contours");
imshow("contours", result);
//
int cmin = 100; //
int cmax = 1000; //
vector<vector >::const_iterator itc = contours.begin();
while (itc!=contours.end())
{
if (itc->size() < cmin || itc->size() > cmax)
itc = contours.erase(itc);
else
++itc;
}
//
Mat result_erase(binaryImage.size(), CV_8U, Scalar(255));
drawContours(result_erase, contours,
-1, //
Scalar(0), //
2); // 2
namedWindow("contours_erase");
imshow("contours_erase", result_erase);
waitKey(0);
return 0;
}