I'm trying to identify whether the gray rectangle in the image shown below contains black stain.
Stain Image:
Stain Highlighted:
import numpy
import matplotlib.pyplot as plt
import cv2
path = r'F:\stain.tif'
img = cv2.imread(path)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 131, 15)
plt.imshow(img_bin, cmap='gray')
plt.show()
Using the OpenCV code above, I was able to create a picture that only whitens the black spot I want to locate as shown below.
Binary image:
However, I'm not sure how to move any further. My final goal is sorting out pictures that only have those black stains on gray rectangles from thousands of images.
Below is an example of a clean, gray rectangle image that should not be sorted.
Clean Rectangle:
I saw on google using 'stats' array of cv2.connectedComponentsWithStats
function might help.
Any advice will be deeply appreciated!