'How to detect black object on black background using Python OpenCV

I am trying to detect a black tape on a black background.

No tape, with tape (cropped pictures):

enter image description here enter image description here

(full size pictures: no tape, with tape)

I have first cropped the area of the tape from the original image and then performing thresholding on it. Below is the image when there is no tape:

enter image description here

You can notice there is an almost solid line. Black tape is placed right next to it and when it is placed this line becomes very light. Below is the image:

enter image description here

Is there any good image processing techniques I can use to detect when the black tape is placed and when its not placed?

Below is the code I am currently using:

import cv2
import os
import imutils
from pathlib import Path
import numpy as np

def on_mouse(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("X: {} | Y: {}".format(x, y))

dirPath = Path(__file__).parents[2]
imgPath = os.path.join(dirPath, "img", "img.png")
win_name = "Image"
cv2.namedWindow(win_name)
cv2.setMouseCallback(win_name, on_mouse)

img = cv2.imread(imgPath)
img = imutils.resize(img, width=800)
roiImg = img[298:337, 520:591]

img_gray = cv2.cvtColor(roiImg, cv2.COLOR_BGR2GRAY)
rett, thresh = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)

cv2.imshow(win_name, img)
cv2.imshow("Thres", thresh)

cv2.waitKey(0)
cv2.destroyAllWindows()

Here is the link to test video: https://drive.google.com/file/d/1P3Xkx_SuHidDs1UdacS3-DZqA-CiXQOX/view?usp=sharing

Below is the image with area marked in red where tape is usually placed

enter image description here

Thanks



Sources

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

Source: Stack Overflow

Solution Source