I'm working on computer vision and I have an image as shown below:
I want to identify the black line on the tissue. I have tried the following code
import cv2
img = cv2.imread('image.png')
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# do morphology gradient
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
morph = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)
# apply gain
morph = cv2.multiply(morph, 10)
morph=cv2.resize(morph, (1000, 552))
imgStack = stackImages(0.5, ([img ], [morph]))
cv2.imshow('Stacked Images', imgStack)
cv2.waitKey(0)
the above line of code gives:
As we can see, the existing pattern prevails and it is difficult to identify the line. How to discard the true pattern and identify the anamolies.
I did try the other answers in stackoverflow, but nothing seem to work
CodePudding user response:
In reference to the comments, I was suggesting to apply global threshold cv2.threshold()
:
Code:
img = cv2.imread(r'C:\Users\524316\Desktop\Stack\tissue.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold_value = 20
th = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow(cv2.hconcat([gray, th]))
cv2.waitKey(0)
Result:
Notice the black line highlighted while no other patterns are affected.