Armand1 / contour-extraction-for-R

0 stars 0 forks source link

implement a "try catch" loop in binarization #4

Open Armand1 opened 3 years ago

Armand1 commented 3 years ago

We need to implement a loop so that if the binarization fails the whole thing does not crash

LittleAri commented 3 years ago

This has been catered for in three ways:

  1. The binarization function now includes an extra step at the start. If the initial binarization step (init_step function) produces an all-black (>95%) photo, then it is assumed that this is because there is a very light object against a very dark background and that whiteBound and blackBound have not been appropriately adjusted to cater for this. Therefore, in this case, whiteBound becomes the greater of the two and blackBound the minimum of the two.
  2. The binarization function includes an extra step at the end. If the final image produced is all-black (>99% black) (and white_or_black has not been set to "both"), then the original unedited image is outputted instead. In this case, the image is not binarized. However, if the contour extraction function (get_outline_contour) is used next, then the function sees that the image is not fully binarized and will use the Otsu thresholding method to threshold the photo, in the hope of producing a better contour.
  3. A "try and except" has been added to the contour extraction function (get_outline_contour) so if there is an error when running it, instead of crashing, it will print "failed to extract contour".
LittleAri commented 3 years ago

Updated the try/except in get_outline_contour so that it returns a list containing only point (0,0) in case of error in addition to "failed contour" print message. This way, if the code was automated in R to plot, it won't result in an error, and it would be obvious which image failed to have a contour as the plot would just show point (0,0).

Armand1 commented 3 years ago
Screen Shot 2021-06-06 at 21 33 36

This does not work. See failure on one vase and the loop stops

LittleAri commented 3 years ago

Sorry, I misread, I thought the try/except should be the contour extraction code, which is what I originally did. I've added it to the binarization code too now.

Armand1 commented 3 years ago

It still fails. This is the image that it is failing on. Same error message as above

M0344

LittleAri commented 3 years ago

Dear Armand, I'm rather confused, since this image works perfectly for me. The contour isn't perfect but I get no errors:

image