1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] OpenCV rectangle detection accuracy on picture

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I'm a junior developer, and for my project, I need to detect rectangles on images, specifically thumbnails from the Netflix dashboard. I'm discovering OpenCV, which seems like the best option to achieve what I want, but I'm not getting good results. Some thumbnails are not being detected, and I don't understand why. My code looks like this, and I've tested many preprocessing modifications on my image, but I got the best results with these parameters:

    import cv2
    import numpy as np

    def nothing(x):
    pass

    img = cv2.imread("./photos/photo.png")

    while True:
    img_copy = img.copy()

    hsv = cv2.cvtColor(img_copy, cv2.COLOR_BGR2HSV)

    lower_bound = np.array([1, 0, 0])
    upper_bound = np.array([180, 255, 255])

    mask = cv2.inRange(hsv, lower_bound, upper_bound)
    kernel = np.ones((5,5), np.uint8)
    erosion = cv2.erode(mask, kernel)

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

    for contour in contours:
    epsilon = 0.05 * cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, epsilon, True)

    if len(approx) == 4:
    x, y, w, h = cv2.boundingRect(contour)
    if w >= 100 and h >= 100:
    # Dessiner un rectangle autour du contour
    cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow("Image with Contours", img_copy)

    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    cv2.destroyAllWindows()


    I got this result:

    [​IMG]

    The result before contour tracing (erosion) looks like this:

    [​IMG]

    What confuses me even more is that I don't understand why it’s specifically the undetected images that aren't being detected.

    We have tried various types of preprocessing techniques: Grayscale Conversion, Thresholding, Canny Edge.

    Continue reading...

Compartilhe esta Página