Sobes.tech
Middle — Senior

Как найти треугольник на изображении без deep learning?

sobes.tech AI

Answer from AI

To find a triangle in an image without using deep learning, you can apply classical computer vision and image processing methods:

  1. Image preprocessing:

    • Convert the image to grayscale.
    • Apply blurring (e.g., Gaussian Blur) to reduce noise.
  2. Edge detection:

    • Use the Canny algorithm to highlight contours.
  3. Contour detection:

    • Find contours using functions like findContours in OpenCV.
  4. Contour analysis:

    • Approximate the shape of each contour using approxPolyDP.
    • If the approximation has 3 vertices, it is a triangle.
  5. Additional checks:

    • Check the area of the contour to exclude noise.
    • Check angles or side ratios to filter out irregular shapes.

Example in Python with OpenCV:

import cv2

img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)

contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    epsilon = 0.04 * cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, epsilon, True)
    if len(approx) == 3:
        cv2.drawContours(img, [approx], 0, (0, 255, 0), 3)

cv2.imshow('Triangles', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

This approach allows effective detection of triangles without neural networks.