MATLAB 中的最近邻插值算法
我正在尝试编写自己的函数,通过使用最近邻插值算法来放大输入图像。不好的部分是我能够看到它是如何工作的,但无法找到算法本身。我将不胜感激任何帮助。
这是我尝试将输入图像放大 2 倍的方法:
function output = nearest(input)
[x,y]=size(input);
output = repmat(uint8(0),x*2,y*2);
[newwidth,newheight]=size(output);
for i=1:y
for j=1:x
xloc = round ((j * (newwidth+1)) / (x+1));
yloc = round ((i * (newheight+1)) / (y+1));
output(xloc,yloc) = input(j,i);
end
end
这是 Mark 之后的输出建议
I am trying to write my own function for scaling up an input image by using the Nearest-neighbor interpolation algorithm. The bad part is I am able to see how it works but cannot find the algorithm itself. I will be grateful for any help.
Here's what I tried for scaling up the input image by a factor of 2:
function output = nearest(input)
[x,y]=size(input);
output = repmat(uint8(0),x*2,y*2);
[newwidth,newheight]=size(output);
for i=1:y
for j=1:x
xloc = round ((j * (newwidth+1)) / (x+1));
yloc = round ((i * (newheight+1)) / (y+1));
output(xloc,yloc) = input(j,i);
end
end
Here is the output after Mark's suggestion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个答案比试图简洁和高效更具解释性。我认为 gnovice 的解决方案在这方面是最好的。如果您想了解它是如何工作的,请继续阅读...
现在您的代码的问题是您正在将输入图像的位置映射到输出图像,这就是为什么您会出现斑点的原因 em> 输出。考虑一个示例,其中输入图像全是白色,输出初始化为黑色,我们得到以下结果:
您应该做什么是相反的(从输出到输入)。为了说明这一点,请考虑以下符号:
其想法是,对于输出图像中的每个位置
(i,j)
,我们希望将其映射到输入图像坐标中“最近”的位置。由于这是一个简单的映射,我们使用将给定x
映射到y
的公式(给定所有其他参数):在我们的例子中,
x
> 是i
/j
坐标,y
是ii
/jj
坐标。因此,替换每一个就可以得到:将各个部分放在一起,我们得到以下代码:
This answer is more explanatory than trying to be concise and efficient. I think gnovice's solution is best in that regard. In case you are trying to understand how it works, keep reading...
Now the problem with your code is that you are mapping locations from the input image to the output image, which is why you are getting the spotty output. Consider an example where input image is all white and output initialized to black, we get the following:
What you should be doing is the opposite (from output to input). To illustrate, consider the following notation:
The idea is that for each location
(i,j)
in the output image, we want to map it to the "nearest" location in the input image coordinates. Since this is a simple mapping we use the formula that maps a givenx
toy
(given all the other params):in our case,
x
is thei
/j
coordinate andy
is theii
/jj
coordinate. Therefore substituting for each gives us:Putting pieces together, we get the following code:
不久前,我浏览了
imresize
的代码MATLAB 图像处理工具箱 中的 函数创建一个简化版本,仅用于图像的最近邻插值。以下是将其应用于您的问题:另一种选择是使用内置的
interp2
函数,尽管您在评论之一中提到不想使用内置函数。编辑:解释
如果有人感兴趣,我想我会解释上面的解决方案是如何工作的...
首先,为了获得新的行和列大小,将旧的行和列大小乘以比例因子。此结果通过
floor
向下舍入为最接近的整数。如果比例因子小于 1,则最终可能会出现尺寸值之一为 0 的奇怪情况,这就是为什么调用max
可以用 1 替换任何小于 1 的内容。接下来,为行和行计算一组新的索引列。首先,计算上采样图像的一组索引:
1:newSize(...)
。每个图像像素被认为具有给定的宽度,例如像素 1 从 0 到 1,像素 2 从 1 到 2,等等。像素的“坐标”因此被视为中心,这就是为什么 0.5从指数中减去。然后将这些坐标除以比例因子,得到原始图像的一组像素中心坐标,然后将其添加 0.5 并四舍五入以获得原始图像的一组整数索引。调用min
确保没有这些索引中的一个大于原始图像大小oldSize(...)
。最后,通过简单地索引原始图像来创建新的上采样图像。
A while back I went through the code of the
imresize
function in the MATLAB Image Processing Toolbox to create a simplified version for just nearest neighbor interpolation of images. Here's how it would be applied to your problem:Another option would be to use the built-in
interp2
function, although you mentioned not wanting to use built-in functions in one of your comments.EDIT: EXPLANATION
In case anyone is interested, I thought I'd explain how the solution above works...
First, to get the new row and column sizes the old row and column sizes are multiplied by the scale factor. This result is rounded down to the nearest integer with
floor
. If the scale factor is less than 1 you could end up with a weird case of one of the size values being 0, which is why the call tomax
is there to replace anything less than 1 with 1.Next, a new set of indices is computed for both the rows and columns. First, a set of indices for the upsampled image is computed:
1:newSize(...)
. Each image pixel is considered as having a given width, such that pixel 1 spans from 0 to 1, pixel 2 spans from 1 to 2, etc.. The "coordinate" of the pixel is thus treated as the center, which is why 0.5 is subtracted from the indices. These coordinates are then divided by the scale factor to give a set of pixel-center coordinates for the original image, which then have 0.5 added to them and are rounded off to get a set of integer indices for the original image. The call tomin
ensures that none of these indices are larger than the original image sizeoldSize(...)
.Finally, the new upsampled image is created by simply indexing into the original image.
MATLAB 已经为您完成了。使用 imresize:
或者如果您想缩放两者x & y 同样,
MATLAB has already done it for you. Use imresize:
or if you want to scale both x & y equally,
您只需要一个更通用的公式来计算 xloc 和 yloc。
这假设您的变量对于乘法结果有足够的范围。
You just need a more generalized formula for calculating xloc and yloc.
This assumes your variables have enough range for the multiplication results.