轮廓检测函数:
Imgproc. findContours(image, contours, hierarchy, mode, method)
通过轮廓检测找到面积最大的轮廓并绘制轮廓:
List<MatOfPoint> contours=new ArrayList<>();
Imgproc.findContours(blurredImage,contours,new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
double maxVal = 0;
int maxValIdx = 0;
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea)
{
maxVal = contourArea;
maxValIdx = contourIdx;
}
}
Mat mRgba=new Mat();
mRgba.create(blurredImage.rows(), blurredImage.cols(), CvType.CV_8UC3);
//绘制检测到的轮廓
Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(0,255,0), 5);