【发布时间】:2017-08-12 22:04:17
【问题描述】:
我正在开发一个程序,我试图从拼图中提取彩色方块。我从视频捕获中获取一帧,然后找到所有轮廓。然后我删除不是正方形的轮廓(这很好,但正在寻找更好的方法)。我面临的主要问题是轮廓重叠。我使用RETR_TREE 获取所有轮廓,但是当使用RETR_EXTERNAL 时,轮廓变得更难检测。有没有办法可以改善正方形的检测?或者一种我可以删除图像中重叠轮廓的方法。
这是重叠轮廓的图像: 在这张图片中找到了 11 个轮廓,但我只想要 9 个。(我画了矩形以便更容易看到重叠)
。如何去除内部轮廓? 这是我的代码:
public Mat captureFrame(Mat capturedFrame){
Mat newFrame = new Mat();
capturedFrame.copyTo(newFrame);
//Gray
Mat gray = new Mat();
Imgproc.cvtColor(capturedFrame, gray, Imgproc.COLOR_RGB2GRAY);
//Blur
Mat blur = new Mat();
Imgproc.blur(gray, blur, new Size(3,3));
//Canny image
Mat canny = new Mat();
Imgproc.Canny(blur, canny, 20, 40, 3, true);
//Dilate image to increase size of lines
Mat kernel = Imgproc.getStructuringElement(1, new Size(3,3));
Mat dilated = new Mat();
Imgproc.dilate(canny,dilated, kernel);
List<MatOfPoint> contours = new ArrayList<>();
List<MatOfPoint> squareContours = new ArrayList<>();
//find contours
Imgproc.findContours(dilated, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
//Remove contours that aren't close to a square shape.
//Wondering if there is a way I can improve this?
for(int i = 0; i < contours.size(); i++){
double area = Imgproc.contourArea( contours.get(i));
MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
double perimeter = Imgproc.arcLength(contour2f, true);
//Found squareness equation on wiki...
//https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);
//add contour to new List if it has a square shape.
if(squareness >= 0.7 && squareness <= 0.9 && area >= 3000){
squareContours.add(contours.get(i));
}
}
MatOfPoint2f approxCurve = new MatOfPoint2f();
for(int n = 0; n < squareContours.size(); n++){
//Convert contours(n) from MatOfPoint to MatOfPoint2f
MatOfPoint2f contour2f = new MatOfPoint2f( squareContours.get(n).toArray());
//Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
//Convert back to MatOfPoint
MatOfPoint points = new MatOfPoint( approxCurve.toArray());
// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(points);
//length and width should be about the same
if(rect.height - rect.width < Math.abs(10)){
System.out.printf("%s , %s \n", rect.height, rect.width);
}
// draw enclosing rectangle (all same color, but you could use variable i to make them unique)
Imgproc.rectangle(newFrame, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar (255, 0, 0, 255), 3);
}
return newFrame;
}
【问题讨论】:
-
嘿.. 我们再次从 OpenCV 见面 ;)。所以如果我理解正确的话,你想去掉红色的轮廓吗?
-
是的,我们又见面了:)。当我检测到一些轮廓相互重叠时,我会绘制矩形以更容易地查看重叠的轮廓。我想删除重叠的轮廓。正如您在图像中看到的那样,我有 2 个相互重叠的矩形,因为有 2 个轮廓重叠。 @eshirima
-
为什么不按他们的区域过滤掉它们呢?我很确定会有一些轮廓的area 太小。您必须弄乱不同的值才能看到哪个有意义
-
我已经按区域过滤掉了它们。如果轮廓区域小于 3000,则不会添加。有时它会将黑色外部检测为单独的轮廓,然后将贴纸检测为黑色轮廓内的另一个轮廓。 @eshirima
-
我的错...我真的很抱歉.. 我来晚了。一种不同的方法。与其删除它们,不如尝试将它们组合在一起?一种是使用他们的bounding boxes。 Here's 另一种方式。
标签: java opencv opencv-contour