'OpenCV: Floor removal from image

I am trying to detect an object on the floor (the floor has a pattern). The object is the cup-like thing in the middle.

I tried to use Canny and Sobel edge detection, however it detects some parts of the floor too. I also tried to HSV color filtering, however it is prone to the room's lighting. SIFT couldn't detect the object too.

How can I remove the floor from the image?

Thanks enter image description here

enter image description here



Solution 1:[1]

Here is how you can use the Canny edge detector:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (5, 5), 6)
    img_canny = cv2.Canny(img_blur, 119, 175)
    kernel = np.ones((9, 3))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=7)
    return cv2.erode(img_dilate, kernel, iterations=7)

img = cv2.imread("lPNAJ.jpg")
contours = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
cnt = sorted(contours, key=cv2.contourArea)[-2]
cv2.drawContours(img, [cv2.convexHull(cnt)], -1, (0, 0, 255), 2)
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output for the first image as input:

enter image description here

Output for the second image as input:

enter image description here

Note that the code takes the second largest contour present in the image, as the largest one belongs to the table on the right.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ann Zen