答案 0 :(得分:0)
这是cv::findContours的经典应用。
documentation中提供了一个工作示例。
对于您的特定情况,您可以这样做:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main(int argc, char** argv)
{
using namespace cv;
using namespace std;
Mat3b image;
image = imread(argv[1], 1);
imshow ("Before", image);
Mat1b gray;
cvtColor(image, gray, CV_BGR2GRAY);
threshold(gray, gray, 250, 255, THRESH_BINARY);
medianBlur(gray,gray, 3);
gray = 255-gray;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(gray, contours, hierarchy, RETR_EXTERNAL , CHAIN_APPROX_NONE);
Scalar color = Scalar( 0, 0, 255 );
for (size_t i = 0; i < contours.size(); i++) {
drawContours(image, contours, i, color, 2, 8, hierarchy, 0, Point());
}
imshow("After", image);
return 0;
}