使用OpenCV内插一维数组 [英] Interpolating 1 dimensional array using OpenCV

查看:183
本文介绍了使用OpenCV内插一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个由2个值组成的数组,并尝试使用imgproc模块的resize函数将其大小调整为10个元素,并使用线性插值作为插值方法.

I define an array of 2 values, and try to use the imgproc module's resize function to resize it to 10 elements with linear interpolation as interpolation method.

cv::Mat input = cv::Mat(1, 2, CV_32F);
input.at<float>(0, 0) = 0.f;
input.at<float>(0, 1) = 1.f;
cv::Mat output = cv::Mat(1, 11, CV_32F);
cv::resize(input, output, output.size(), 0, 0, cv::INTER_LINEAR);
for(int i=0; i<11; ++i)
{
    std::cout<< output.at<float>(0, i) << " ";
}

我期望的输出是:

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

但是我得到的是:

0 0 0 0.136364 0.318182 0.5 0.681818 0.863636 1 1 1

很明显,我对调整大小的工作原理的理解在根本上是错误的.有人可以告诉我我做错了吗?诚然,对于这样简单的线性插值,OpenCV是一个过大的杀伤力,但是请帮我解决这里的错误.

Clearly, my understanding of how resize works is wrong at a fundamental level. Can someone please tell me what I am doing wrong? Admittedly, OpenCV is an overkill for such simple linear interpolation, but please do help me with what is wrong here.

推荐答案

这真的很简单. OpenCV是图像处理库.因此,您应该记住我们正在处理图像.

It's really simple. OpenCV is an image processing library. So you should remember that we are working on images.

当目标图像中只有8个像素时,看看输出结果

Take a look at the output when we have only 8 pixels in destination image

0 0 0.125 0.375 0.625 0.875 1 1

0 0 0.125 0.375 0.625 0.875 1 1

如果您看这张图片,很容易理解调整大小的行为

If you take a look at this image it's very straightforward to understand resize behaviour

正如您在链接中所看到的那样,您正在使用图像转换库:本节中的功能执行2D图像的各种几何转换"

As you can see in this link you're using a Image transformation library: "The functions in this section perform various geometrical transformations of 2D images"

您想要这个结果

但不能正确地对原始2像素的图像进行插值

but it will not interpolate properly the original 2 pixels image

这篇关于使用OpenCV内插一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆