Are you struggling with extracting colored squares from a puzzle? If so, you’re not alone. Many programmers face the challenge of dealing with overlapping contours and finding an efficient way to remove them. In this article, we’ll explore a better method to detect squares and eliminate those pesky overlapping contours, providing you with a solution to your problem.
Understanding the Issue: Overlapping Contours
When working with contour detection in OpenCV, it’s common to encounter overlapping contours, especially in scenarios like extracting colored squares from a puzzle. These overlapping contours can make it difficult to accurately identify and extract the desired squares, leading to incorrect results.
The Approach: Improving Contour Detection
To improve the detection of squares and remove overlapping contours, we’ll employ a combination of techniques. Let’s walk through the steps to achieve our goal.
Step 1: Preprocessing the Image
Before detecting contours, it’s crucial to preprocess the image to enhance the quality of the squares. Here are the steps involved:
- Convert the captured frame to grayscale using the
cvtColor
function in OpenCV. - Apply blurring to reduce noise by using the
blur
function. - Apply edge detection using the Canny edge detector (
Canny
function) to identify the edges of the squares. - Dilate the edges using the
dilate
function to increase the size of the lines, making them more prominent.
Step 2: Contour Detection and Filtering
Now that we have a preprocessed image, we can proceed with contour detection and filtering. Follow these steps:
- Initialize a list to store the detected contours.
- Use the
findContours
function to detect contours in the preprocessed image. Set the retrieval mode (RETR_TREE
) and contour approximation method (CHAIN_APPROX_NONE
). - Retrieve the hierarchy matrix using the
findContours
function with an additional parameter to store the hierarchy. -
Iterate through the detected contours and hierarchy matrix to filter out unwanted contours. We’ll focus on contours with a square-like shape.
- Calculate the area and perimeter of each contour using the
contourArea
andarcLength
functions, respectively. - Calculate the squareness factor using the formula: squareness = (4 * π * area) / (perimeter^2).
- Add contours to a new list if their squareness factor falls within a desired range (e.g., between 0.7 and 0.9) and their area exceeds a certain threshold (e.g., 3000).
- Calculate the area and perimeter of each contour using the
Step 3: Removing Overlapping Contours
To remove the overlapping contours, we’ll make use of the hierarchy matrix obtained in Step 2. Follow these instructions:
- Iterate through the filtered contours and their corresponding hierarchy entries.
-
Check if the parent value in the hierarchy is -1. If it is, then the contour has no parent and is not enclosed by any other contour. Retain these contours and discard the rest.
- The hierarchy matrix format is [Next, Previous, First_Child, Parent] .
- The contours with a parent value of -1 represent the outermost contours.
Step 4: Visualization and Further Processing
At this point, you have successfully removed the overlapping contours, leaving behind the desired squares. Now, you can perform additional processing or visualization as per your requirements. For example, you can draw rectangles around the squares using the bounding rectangle information obtained from the filtered contours.
Conclusion
In this article, we’ve explored a better method to remove overlapping contours when extracting colored squares from a puzzle. By combining preprocessing techniques, contour detection, filtering based on squareness, and utilizing the hierarchy matrix, you can effectively eliminate the unwanted contours. Implement this approach in your program, and you’ll be able to extract the desired squares accurately.
Remember, when dealing with contour detection, experimentation and fine-tuning are key. Adjust the parameters and thresholds according to your specific puzzle or image characteristics to achieve optimal results.