我需要提取一张图片中的最大轮廓。这是我目前正在使用的代码,收集自网上的几个片段。
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(outerBox, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
for (int idx = 0; idx < contours.size(); idx++) {
Mat contour = contours.get(idx);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxArea) {
maxArea = contourarea;
maxAreaIdx = idx;
}
}
看起来它似乎可以工作,但我不太确定接下来该怎么做。
我尝试使用Imgproc.floodFill
,但我不太确定如何操作。
这个函数需要一个与原始Mat大小相同的掩膜Mat
+2水平和+2垂直。
当我在轮廓contours.get(maxAreaIdx)
上运行它时,它给了我一个错误。
代码:
Mat mask = Mat.zeros(contour.rows() + 2, contour.cols() + 2, CvType.CV_8UC1);
int area = Imgproc.floodFill(contour, mask, new Point(0,0), new Scalar(255, 255, 255));
错误:
11-18 19:07:49.406: E/cv::error()(3117): OpenCV Error: Unsupported format or combination of formats () in void cvFloodFill(CvArr*, CvPoint, CvScalar, CvScalar, CvScalar, CvConnectedComp*, int, CvArr*), file /home/oleg/sources/opencv/modules/imgproc/src/floodfill.cpp, line 621
基本上我的问题是,我如何在找到最大面积的轮廓后,“突出显示”它? 我想让其他所有东西都变成黑色,而轮廓变成白色
谢谢!