Ask Your Question
0

background color affecting contour detection

asked 2018-01-10 12:12:36 -0600

dkeidel gravatar image

updated 2018-01-11 18:46:28 -0600

I have been successful in detecting contours and drawing those back on the original image.

Here is my original image:

C:\fakepath\input_template_masked_resized.jpg

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:

C:\fakepath\output_template_masked_contoured_exploration_resized.jpg

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: C:\fakepath\F4GSCZQJHG7K_template_masked_equalized_hist_exploration_resized.jpg

And here is image after contour detection:

C:\fakepath\F4GSCZQJHG7K_template_masked_contoured_exploration_resized.jpg

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?

edit retag flag offensive close merge delete

Comments

Have you tried the adaptiveThreshold function?

Like this, in C++ anyway:

adaptiveThreshold(frame, frame, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
sjhalayka gravatar imagesjhalayka ( 2018-01-10 17:14:06 -0600 )edit
1

Like this, in Python.

 th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
supra56 gravatar imagesupra56 ( 2018-01-10 21:39:22 -0600 )edit

@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.

dkeidel gravatar imagedkeidel ( 2018-01-11 14:47:15 -0600 )edit

I only suggested it because it's an option to try. Not all situations call for adaptiveThreshold; it's not better or worse than threshold. 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 gravatar imagesjhalayka ( 2018-01-11 20:27:59 -0600 )edit

@sjhalayka - Thank you very much for offering your suggestion to try adaptiveThreshold. It makes sense that it would be situation dependent.

dkeidel gravatar imagedkeidel ( 2018-01-11 23:32:45 -0600 )edit
2

why don't you use gradient image (module) ?

image description

LBerger gravatar imageLBerger ( 2018-01-13 03:22:23 -0600 )edit

@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.

dkeidel gravatar imagedkeidel ( 2018-01-17 00:40:07 -0600 )edit

Sorry I don't know python but I think you can try this tutorial

LBerger gravatar imageLBerger ( 2018-01-17 01:51:49 -0600 )edit

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.

dkeidel gravatar imagedkeidel ( 2018-01-17 13:40:41 -0600 )edit

I have found tutorials here and select your favourite language!

LBerger gravatar imageLBerger ( 2018-01-17 14:19:20 -0600 )edit

2 answers

Sort by » oldest newest most voted
0

answered 2018-01-12 03:28:14 -0600

Ziri gravatar image

I suggest using DOG or Variance filter to ignore background variation.

edit flag offensive delete link more
0

answered 2018-01-10 23:56:58 -0600

moHe gravatar image

Maybe you can use Histogram Equalization to make the details in the regions with dark background more clear, instead of changing the threshold one by one.

edit flag offensive delete link more

Comments

@moHe - I added results of Histogram Equalization to the original question above for your viewing with images.

dkeidel gravatar imagedkeidel ( 2018-01-11 18:48:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-10 12:12:36 -0600

Seen: 3,997 times

Last updated: Jan 12 '18