Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triangle detection using OpenCV

I have the following sample image:

enter image description here

I want to fill these triangles in the corners with white color. How could I detect them using OpenCV? Of course, in this particular sample, I can just lean on the gradient or brightness. Nevertheless, in the future images won't be such perfectly shaped, so I am thinking of some shape detection.

I heard that shaped can usually be detected with, for example, Hough transform. But I do not know what I should start with.

Contour detection in OpenCV does not help since it finds too many candidates. I tried to use approxPolyDP with size = 3, but there was also no result (there were no such objects found).

These triangles will always be triangles, but they do not need to touch bars every time. They always will be at the edges of the image. They share approximately the same area among them.

I would like to be able to detect triangles and collect points corresponding to these triangles in some container.

like image 229
newt Avatar asked Sep 19 '17 12:09

newt


People also ask

How do you find triangles in OpenCV?

Use the findContours() and approxPolyDP() Functions of OpenCV to Detect Shapes Present in an Image. We can find shapes present in an image using the findContours() and approxPolyDP() function of OpenCV. We can detect shapes depending on the number of corners it has.

What function is used to detect polygons OpenCV?

Contours – convex contours and the Douglas-Peucker algorithm The first facility OpenCV offers to calculate the approximate bounding polygon of a shape is cv2. approxPolyDP. This function takes three parameters: A contour.

What is contour OpenCV?

What are contours? Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition. For better accuracy, use binary images.


1 Answers

I am able to detect the triangle with below code. I am finding all the contours in the image and then using approxPolyDP, I am able to find the triangle.

import cv2
import numpy as np
image_obj = cv2.imread('image.jpg')

gray = cv2.cvtColor(image_obj, cv2.COLOR_BGR2GRAY)

kernel = np.ones((4, 4), np.uint8)
dilation = cv2.dilate(gray, kernel, iterations=1)

blur = cv2.GaussianBlur(dilation, (5, 5), 0)


thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

# Now finding Contours         ###################
_, contours, _ = cv2.findContours(
    thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
coordinates = []
for cnt in contours:
        # [point_x, point_y, width, height] = cv2.boundingRect(cnt)
    approx = cv2.approxPolyDP(
        cnt, 0.07 * cv2.arcLength(cnt, True), True)
    if len(approx) == 3:
        coordinates.append([cnt])
        cv2.drawContours(image_obj, [cnt], 0, (0, 0, 255), 3)

cv2.imwrite("result.png", image_obj)

Output image enter image description here

You can get contours in coordinates list.

like image 85
Amarpreet Singh Avatar answered Sep 28 '22 00:09

Amarpreet Singh