1 #include <opencv2/opencv.hpp>  
 2 using namespace cv;
 3 using namespace std;
 4 int main()
 5 {
 6     Mat bwImg, srcImg, bwImg1;
 7     srcImg = imread("b.jpg",0);
 8      bwImg = srcImg > 100;
 9     vector<vector<Point>> contours;
10     // 查找轮廓,对应连通域
11     findContours(bwImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
12     //矩形标记
13     for (size_t i = 0; i < contours.size(); i++)
14     {
15         Rect r = boundingRect(contours[i]);
16         rectangle(bwImg, r, Scalar(200,0,0));
17     }
18 
19     imshow("largest region", bwImg);
20     waitKey();
21     return 0;
22 }