-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add adaptive threshold algorithm using mean method #341
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miralshah365 First round of review that only addresses the run-time failure you mentioned on Gitter.
7c0f386
to
a3054bc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miralshah365 I noticed a memory leak
@miralshah365 OpenCVThe 16x16 sample above passed through the adaptive mean threshold by OpenCV: import cv2
import numpy as np
import matplotlib.pyplot as plt
path = 'D:\\workshop\\opencv\\'
path_original = path + "rectangles.png"
img = cv2.imread(path_original, 0)
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 0)
th3 = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 0)
titles = ['Input', 'Global Thresholding (t=127)',
'Adaptive Mean', 'Adaptive Gaussian']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show() identifies both rectangles: GILUsing std::string image_dir = "d:\\workshop\\opencv\\";
{
gray8_image_t img;
read_image(image_dir + "rectangles.png", img, png_tag{});
gray8_image_t img_out(img.dimensions());
boost::gil::threshold_adaptive(const_view(img), view(img_out), 11);
write_view(image_dir + "out-threshold-adaptive.png", view(img_out), png_tag{});
} identifies only one, the upper rectangle: Here is dump of the convoluted view generated with this write_view("d:\\workshop\\opencv\\out-threshold-adaptive-convoluted-view.png", temp_view, png_tag{}); Unfortunately, I have no way to generate equivalent for OpenCV. ConclusionThere still may be some fine tuning of the algorithm necessary. |
That's because of kernel size and image size are too close and in such cases what scheme we chose for the border is really important. OpenCV's default scheme is this while GIL extends the border with I created the same type of image but in a larger size (3000x3000 pixels) and tests on this image: Somehow they are inverted I'll investigate that behavior. |
7a43b4b
to
cb23af0
Compare
cb23af0
to
7a3ff11
Compare
@miralshah365 Here is comparison of the LHS - Interestingly, OpenCV for @miralshah365 This OpenCV's boundary mode is quite alike the |
(Related to my earlier comment #341 (comment)) @miralshah365 From your yesterday comment on Gitter:
I don't think there is a single correct output because for such sharp edge cases (i.e. dark gray features on light gray background), thresholding result does depend on value of `C1: From adaptive threshold description at http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
Assuming that OpenCV defaults to C=5, one may argue that 'correct' output is black rectangles on white background. This is what OpenCV produces from our OpenCV:
GIL:
In other words, equivalent threshold tests are:
So, the major difference between behaviour of OpenCV and GIL that I can see is this flexibility due to the extra constant parameter. This is a common approach, see also https://imagej.net/Auto_Local_Threshold for the Mean formula it uses. @miralshah365 Your algorithm could also allow such parametrization. Alternative mean test using offsetLet's an alternative threshold test using
Although I don't think it is common approach and rather fine-tuned/specific to cetain applications, its properties are interesting, from http://www.roborealm.com/help/Adaptive_Threshold.php:
If you try it out for our test case, you should see that it produces thicker and gradual outline of each rectangle which closely reflects the mean values near the edges of rectangles (produced by the |
e476bbc
to
692d878
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miralshah365 Good job! Please, merge whenever you like (assuming CI-s are green, of course).
More tests will come as part of separate task #353 along with the fine-tuning of the algorithm itself, if we discover and agree as necessary.
@miralshah365 Travis CI for If you want, I can merge this PR without waiting for the Boost's Travis CI. Shall I do it? |
Description
New threshold adaptive method added.
This method allows the user to set threshold values according to neighbors. The threshold value is found by calculating the mean of all the neighbors including pixel itself. This mean value is then treated as a threshold and the binary threshold is performed.
References
Tasklist