Numpy乘数乘以标量转换为C#
基本上,我使用 opencv 在给定图像中找到轮廓。为了提高轮廓识别,我将调整大小应用于原始图像,然后将其与获得的轮廓相比,将它们“转换”到真实的图像大小:
ratio = image.shape[0] / 500.0
image = imutils.resize(original, height = 500)
contours= cv2.findContours(image , cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
approx = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(contour , True), True)
if len(approx) == 4:
selectedContour = approx
break
reshapedSelectedContour = selectedContour.reshape(4, 2) * ratio
#paint contours on original
这是按预期工作的。当我尝试将其转换为C#时,它的运行不佳:
double ratio = image.Height() / 500;
Imgproc.Resize(original, image, new Size((int)(image.Width() * 500 / (double)image.Height()), 500));
IList<MatOfPoint> contours = new JavaList<MatOfPoint>();
Imgproc.FindContours(image, contours, new Mat(), Imgproc.RetrList, Imgproc.ChainApproxSimple);
MatOfPoint2f selectedContour = new MatOfPoint2f();
foreach (MatOfPoint contour in contours)
{
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.ApproxPolyDP(new MatOfPoint2f(contour.ToArray()), approx, Imgproc.ArcLength(new MatOfPoint2f(contour.ToArray()), true) * 0.02, true);
if (approx.Total() == 4)
{
selectedContour = approx;
break;
}
}
Mat reshapedSelectedContour = new Mat();
Core.Multiply(selectedContour.Reshape(4, 2), new Scalar(ratio), reshapedSelectedContour );
//Paint contours on original
如果我通过图像(调整大小的图像)绘制轮廓,我会为Python和C#绘制相同的结果。但是,当我尝试在原始图像上绘制 reshapedselectedContour 时,Python版本正在工作,并且C#并没有给我相同的输出。
我在C#中获得 reshapedselectedContour 的方式是正确的吗?
Basically I have this python code using opencv to find the contours in a given image. To improve contours recognition, I apply a resize to the original image and then I apply a ratio to the obtained contours to "translate" them to the real image size:
ratio = image.shape[0] / 500.0
image = imutils.resize(original, height = 500)
contours= cv2.findContours(image , cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
approx = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(contour , True), True)
if len(approx) == 4:
selectedContour = approx
break
reshapedSelectedContour = selectedContour.reshape(4, 2) * ratio
#paint contours on original
This is working as expected. When I try to translate it to c# it's not working well:
double ratio = image.Height() / 500;
Imgproc.Resize(original, image, new Size((int)(image.Width() * 500 / (double)image.Height()), 500));
IList<MatOfPoint> contours = new JavaList<MatOfPoint>();
Imgproc.FindContours(image, contours, new Mat(), Imgproc.RetrList, Imgproc.ChainApproxSimple);
MatOfPoint2f selectedContour = new MatOfPoint2f();
foreach (MatOfPoint contour in contours)
{
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.ApproxPolyDP(new MatOfPoint2f(contour.ToArray()), approx, Imgproc.ArcLength(new MatOfPoint2f(contour.ToArray()), true) * 0.02, true);
if (approx.Total() == 4)
{
selectedContour = approx;
break;
}
}
Mat reshapedSelectedContour = new Mat();
Core.Multiply(selectedContour.Reshape(4, 2), new Scalar(ratio), reshapedSelectedContour );
//Paint contours on original
If I paint the contours over image (the resized image) I get the same results for python and c#. But when I try to paint reshapedSelectedContour over the original image, python version is working and c# is not giving me the same output.
Is my way of obtaining reshapedSelectedContour in c# correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过手动获取轮廓点并将其乘以比率来解决它
Solved it by manually obtaining the contour points and multiplying all them by the ratio