问题
I am working with openCV on Android platform. With the tremendous help from this community and techies, I am able to successfully detect a sheet out of the image. These are the step I used.
1.Imgproc.cvtColor()
2.Imgproc.Canny()
3.Imgproc.GausianBlur()
4.Imgproc.findContours()
5.Imgproc.approxPolyDP()
6.findLargestRectangle()
7.find the vertices of the rectangle
8.find the vertices of the rectangle top-left anticlockwise order using center of mass approach
9.find the height and width of the rectangle just to maintain the aspect ratio and do warpPerspective transformation.
After applying all these steps I can easily get the document or the largest rectangle from an image. But it highly depends on the difference in the intensities of the background and the document sheet. As Canny edge works on the principle of intensity gradient, a difference in intensity is always assumed from the implementation side. That is why Canny took into the account the various threshold parameters.
- Lower Thershold
- Heigher Thershold
So if the intensity gradient of a pixel is greater than the Higher Threshold,it will be added as an edge pixel in the output image. A pixel will be rejected completely if its intensity gradient value is lower than the Lower Threshold. And if a pixel has an intensity between the lower and higher threshold, it will only be added as an edge pixel if it is connected to any other pixel having the value larger than the Higher Threshold.
My main purpose is to use Canny edge detection for the document scanning. So I want to know how to compute these threshold dynamically so that it can work with the both cases of dark and light background.
I tried a lot by manually adjusting the parameters , but I couldn't find any relationship associated with the scenarios.
Hope I clear my point, and thanks in advance
回答1:
You could calculate your Thresholds using Otsu´s Method
The (Python) Code would look like this:
high_thresh, thresh_im = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
lowThresh = 0.5*high_thresh
回答2:
Use the following snippet which I obtained from THIS BLOG:
v = np.median(gray_image)
#---- apply automatic Canny edge detection using the computed median----
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(gray_image, lower, upper)
cv2.imshow('Edges',edged)
So what am I doing here?
I am taking the median value of the gray scale image. The sigma value of 0.33 is chosen to set the lower and upper threshold. 0.33 value is generally used by statisticians for data science. So it is considered here as well.
来源:https://stackoverflow.com/questions/21324950/how-to-select-the-best-set-of-parameters-in-canny-edge-detection-algorithm-imple