OpenCV: refine contours that are similar to rectangles
I was working on rectangle detection software, that should detect any rectangle on a given surface. After doing some research I came up with the following:
This yields decent results, however in some cases contour is not perfect. Which can be seen in the following images original image , detected contours.
I was thinking about convex hull, but then even the contours that aren't rectangles may be misinterpreted as rectangles. Maybe continue the contours in a straight line and calculate intersection points?
I am stuck. Any tips on how to refine the contours?
Here's the code for reference (Java on Android):
detectedEdges = grayImg.clone();
Size size = new Size(9, 9);
Imgproc.GaussianBlur(grayImg, grayImg, size, 0);
double otsu = Imgproc.threshold(grayImg.clone(),new Mat(),0,255,Imgproc.THRESH_BINARY|Imgproc.THRESH_OTSU);
Imgproc.Canny(grayImg, detectedEdges, otsu/2, otsu);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
Imgproc.morphologyEx(detectedEdges.clone(),detectedEdges,Imgproc.MORPH_CLOSE,kernel);
contours = new ArrayList<>();
Imgproc.findContours(detectedEdges.clone(),contours,new Mat(),Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE);
if (contours.size() == 0) {
Toast.makeText(getApplicationContext(), "No contours found", Toast.LENGTH_SHORT).show();
return null;
}
for(MatOfPoint c : contours){
MatOfPoint2f contour = new MatOfPoint2f(c.toArray());
double length = Imgproc.arcLength(contour,true);
Imgproc.approxPolyDP(contour,contour,0.02*length,true);
if(contour.total() == 4){
//found a rect! DO something
}
}
Imgproc.drawContours(grayImg,contours,-1,new Scalar(255,225,255), 2);
链接地址: http://www.djcxy.com/p/58732.html
上一篇: 从Java中的java.util.stream.Stream获取列表8
下一篇: OpenCV:细化类似于矩形的轮廓