background color affecting contour detection
I have been successful in detecting contours and drawing those back on the original image.
Here is my original image:
Link to full size image There is download button in lower right
Portion of Python script where I detect contours:
image = cv2.imread("/Users/donaldkeidel/Downloads/input_template_masked.jpg")
# grayscale the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# apply a Gaussian blur
blur = cv2.GaussianBlur(gray, (3, 3), 0)
# threshold the image
(t, binary) = cv2.threshold(blur, 100, 255, cv2.THRESH_BINARY)
# find contours
(_, contours, _) = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
image_cropped_contoured_drawn = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 3)
cv2.imwrite("F4GSCZQJHG7K_template_masked_contoured_exploration.jpg", image_cropped_contoured_drawn)
Here is the output image:
Link to full size image There is download button in lower right
As you can see in the image, almost all the contours were captured where the background color is a lighter gray. However, where the background is an almost black in the middle of the image, less contours were detected.
Are there some techniques that can be applied to detect more of these contours? I played with the threshold value and found that 100 works the best. I also used a few other values for the parameters in the GaussianBlur method.
Thank you.
Update:
I applied equalizeHist
by doing the following:
image = cv2.imread("/Users/donaldkeidel/Downloads/input_template_masked.jpg")
# grayscale the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Histogram equalization
equ = cv2.equalizeHist(gray)
# threshold the image
(t, binary) = cv2.threshold(equ, 100, 255, cv2.THRESH_BINARY)
# find contours
(_, contours, _) = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
image_cropped_contoured_drawn = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 3)
cv2.imwrite("F4GSCZQJHG7K_template_masked_contoured_exploration.jpg", image_cropped_contoured_drawn)
The following image shows the gray scale image compared to Histogram Equalized image:
And here is image after contour detection:
Any further suggestions on how to proceed are appreciated. I like the suggestion about increasing the contrast using Histogram Equalizatioon, however, drawing contours on this image proves to be difficult. Should I preprocess the image more?
Have you tried the
adaptiveThreshold
function?Like this, in C++ anyway:
Like this, in Python.
@sjhalayka and @supra56 - I have tried
adaptiveThreshold
with different parameter values for the last two parameters (blocksize and C). I have read the documentation for what these parameters are:blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.
But still not 100% sure on what they mean and how
adaptiveThreshold
might be a better option.I only suggested it because it's an option to try. Not all situations call for
adaptiveThreshold
; it's not better or worse thanthreshold
. I'm not entirely sure how to interpret the last parameter either, other than it should have no effect if the value is 0.@sjhalayka - Thank you very much for offering your suggestion to try
adaptiveThreshold
. It makes sense that it would be situation dependent.why don't you use gradient image (module) ?
@LBerger - would you be able to point me to some documentation on the web maybe that describes the process/theory? Or if you have some sample code on how you generated the above image that might help also. Thank you.
Sorry I don't know python but I think you can try this tutorial
LBerger thank you for the link to the tutorial. If you want to post your C code (assuming you wrote it in C) that would be fine too. I can read it and translate it to the equivalent in Python.
I have found tutorials here and select your favourite language!