# How to detect foreign objects on a plane?

Hello!
My task is to detect whether table clean or not. My current method is to do some kind of shadow removal using algorithm given in a SO answer:

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dilated_img = cv2.dilate(img_gray, np.ones((7, 7), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
diff_img = 255 - cv2.absdiff(img_gray, bg_img)
norm_img = cv2.normalize(diff_img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


and it gives the following result:
after that I am performing a background subtraction using MOG2 and some morphology:

    opening_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closing_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
erode_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

res = cv2.erode(res, erode_kernel)
res = cv2.morphologyEx(res, cv2.MORPH_CLOSE, closing_kernel)


and the final mask looks like
This method kind of works and successfully detects objects, and moreover, it is invariant to shadows(sometimes), but sometimes this algorithms gives a lot of false positives without an obvious reason. I think, that the problem is in the "shadow removal" because the result looks very strange. I know, that the shadow removal task itself is a very complex and i dont think, that I need an actual shadow removal, but my program may work outside or in places with unstable lightning conditions and it is important to minimize probability of the false positives. I also think, that my morphology part is not good, but i cant understand, how to do it properly.