Please note, I am a complete beginner in computer vision and OpenCV(Java).
My objective is to identify parking signs, and to draw bounding boxes around them. My problem is that the four signs from the top (with red borders) were not identified (see last image). I am also noticing that the Canny edge detection does not capture the edges of these four signs (see second image). I have tried with other images, and got the same results. My approach is as follows:
Load the image and convert it to gray scale
Pre-process the image by applying bilateralFilter and Gaussian blur
Execute Canny edge detection
Find all contours
Calculate the perimeter with arcLength and approximate the contour with approxPolyDP
If approximated figure has 4 points, then assuming it is a rectangle hence adding the contour
Finally, draw the contours that has 4 points exactly.
Mat filtered = new Mat(); Mat edges = new Mat(src.size(), CvType.CV_8UC1); Imgproc.cvtColor(src, edges, Imgproc.COLOR_RGB2GRAY); Imgproc.bilateralFilter(edges, filtered, 11, 17, 17); org.opencv.core.Size s = new Size(5, 5); Imgproc.GaussianBlur(filtered, filtered, s, 0); Imgproc.Canny(filtered, filtered, 170, 200); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(filtered, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); List<MatOfPoint> rectangleContours = new ArrayList<MatOfPoint>(); for (MatOfPoint contour : contours) { MatOfPoint2f dst = new MatOfPoint2f(); contour.convertTo(dst, CvType.CV_32F); perimeter = Imgproc.arcLength(dst, true); approximationAccuracy = 0.02 * perimeter; MatOfPoint2f approx = new MatOfPoint2f(); Imgproc.approxPolyDP(dst, approx, approximationAccuracy, true); if (approx.total() == 4) { rectangleContours.add(contour); Toast.makeText(reactContext.getApplicationContext(), "Rectangle detected" + approx.total(), Toast.LENGTH_SHORT).show(); } } Imgproc.drawContours(src, rectangleContours, -1, new Scalar(0, 255, 0), 5);
Very happy to get advice on how I could resolve this issue, even if it implies changing my stratergy.