Issue
SOLVED: I'm working on detecting the MRZ of a passport. I've got a strategy but due to lack of documentation I haven't been able to convert the following lines of code. My project is using OpenCvSharp and NumSharp which is a port of numpy to c#. Any help would be much appreciated.
Python Code:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# smooth the image using a 3x3 Gaussian, then apply the blackhat
# morphological operator to find dark regions on a light background
gray = cv2.GaussianBlur(gray, (3, 3), 0)
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)
# compute the Scharr gradient of the blackhat image and scale the
# result into the range [0, 255]
gradX = cv2.Sobel(blackhat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")
C# Code:
// smooth the image using a 3x3 Gaussian, then apply the blackhat
// morphological operator to find dark regions on a light background
smallImage = smallImage.GaussianBlur(new Size(3, 3), 0);
Cv2.MorphologyEx(smallImage, blackhat, MorphTypes.BlackHat, rectKernel);
// compute the Scharr gradient of the blackhat image and scale the // result into the range [0, 255] Cv2.Sobel(blackhat, gradX, MatType.CV_32F, 1, 0, -1, 1, 0);
//(minVal, maxVal) = (np.min(gradX), np.max(gradX))
double minVal, maxVal;
gradX.MinMaxLoc(out minVal, out maxVal);
//gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")
gradX.ConvertTo(gradX, MatType.CV_8U, 255.0 / (maxVal - minVal), -255.0 / minVal);
Specifically I'm having trouble with the last line of Python code. I'm not sure how to perform that calculation in c#. I'm also not sure if I'm getting the minVal
and maxVal
values correctly.
Solution
Here is the solution to my original problem:
// compute the Scharr gradient of the blackhat image and scale the
// result into the range [0, 255]
Cv2.Sobel(blackhat, gradX, MatType.CV_32F, 1, 0, -1, 1, 0);
//(minVal, maxVal) = (np.min(gradX), np.max(gradX))
double minVal, maxVal;
gradX.MinMaxLoc(out minVal, out maxVal);
//gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")
gradX.ConvertTo(gradX, MatType.CV_8U, 255.0 / (maxVal - minVal), -255.0 / minVal);
Answered By - Matt M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.