将代码从 Python 转换为 C# 时遇到问题
Having trouble converting code from Python to c#
已解决:我正在检测护照的机读区。我有一个策略,但由于缺乏文档,我无法转换以下代码行。我的项目正在使用 OpenCvSharp 和 NumSharp,它们是 numpy 到 c# 的端口。任何帮助将不胜感激。
Python代码:
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#代码:
// 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);
// 计算黑帽图像的 Scharr 梯度并缩放
// 结果在 [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);
具体来说,我在处理 Python 代码的最后一行时遇到了问题。我不确定如何在 C# 中执行该计算。我也不确定我是否正确获取了 minVal
和 maxVal
值。
8 位无符号整数在 C# 中是 byte
。你可以这样做:
var gradX = Convert.ToByte(255 * ((gradX - minVal) / (maxVal - minVal)));
我不确定那些 min 和 max 函数在做什么,所以我无能为力。
这是我原来问题的解决方案:
// 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);
已解决:我正在检测护照的机读区。我有一个策略,但由于缺乏文档,我无法转换以下代码行。我的项目正在使用 OpenCvSharp 和 NumSharp,它们是 numpy 到 c# 的端口。任何帮助将不胜感激。
Python代码:
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#代码:
// 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);
// 计算黑帽图像的 Scharr 梯度并缩放 // 结果在 [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);
具体来说,我在处理 Python 代码的最后一行时遇到了问题。我不确定如何在 C# 中执行该计算。我也不确定我是否正确获取了 minVal
和 maxVal
值。
8 位无符号整数在 C# 中是 byte
。你可以这样做:
var gradX = Convert.ToByte(255 * ((gradX - minVal) / (maxVal - minVal)));
我不确定那些 min 和 max 函数在做什么,所以我无能为力。
这是我原来问题的解决方案:
// 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);