Saturday, 30 January 2021

How to remove a patterned background from an image and detect the objects?

I have an image that is a frame of a video. As you can see in the image 1 the background has a pattern that makes it challenging to detect the Lego objects. Based on my current code, the object edges are detected wrongly and messed up with the shapes of the background, the result in image 2 . The result with rectangles is image 3 .

1 2 3

import cv2
import numpy as np

main_image = cv2.imread('image.jpg', 1)
convert_to_gray = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY)
convert_to_blurred = cv2.GaussianBlur(convert_to_gray, (3, 3), 2)
a, b = cv2.threshold(convert_to_blurred, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
canny_result = cv2.Canny(convert_to_blurred, a / 6, b / 3)
k = np.ones((2, 2), np.uint8)
d = cv2.dilate(canny_result, k, iterations=3)

contours_found = cv2.findContours(d, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours_found = contours_found[0] if len(contours_found) == 2 else contours_found[1]

for cont in contours_found:
    x, y, w, h = cv2.boundingRect(cont)
    cv2.rectangle(main_image, (x, y), (x + w, y + h), (0, 0, 255), 3)

cv2.imshow('canny_result', canny_result)
cv2.imshow('main_image', main_image)
cv2.waitKey(0)

What to do to detect the objects correctly?



from How to remove a patterned background from an image and detect the objects?

No comments:

Post a Comment