MATLAB 中的最近邻插值算法 [英] Nearest-neighbor interpolation algorithm in MATLAB

查看:60
本文介绍了MATLAB 中的最近邻插值算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写自己的函数,用于使用最近邻插值算法放大输入图像.不好的部分是我能够看到它是如何工作的,但找不到算法本身.如有任何帮助,我将不胜感激.

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.

这是我尝试将输入图像放大 2 倍的方法:

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

这是 函数在 MATLAB 图像处理工具箱 为图像的最近邻插值创建一个简化版本.以下是它如何应用于您的问题:

Here is the output after 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:

%# Initializations:

scale = [2 2];              %# The resolution scale factors: [rows columns]
oldSize = size(inputImage);                   %# Get the size of your image
newSize = max(floor(scale.*oldSize(1:2)),1);  %# Compute the new image size

%# Compute an upsampled set of indices:

rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));
colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));

%# Index old image to get new image:

outputImage = inputImage(rowIndex,colIndex,:);

另一种选择是使用内置的interp2 函数,尽管您在评论之一中提到不想使用内置函数.

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.

解释

如果有人感兴趣,我想我会解释上述解决方案的工作原理...

In case anyone is interested, I thought I'd explain how the solution above works...

newSize = max(floor(scale.*oldSize(1:2)),1);

首先,要获得新的行和列的大小,旧的行和列的大小要乘以比例因子.这个结果用 floor 向下舍入到最接近的整数.如果比例因子小于 1,您最终可能会遇到大小值之一为 0 的奇怪情况,这就是调用 max 是否可以用 1 替换小于 1 的任何内容.

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 to max is there to replace anything less than 1 with 1.

rowIndex = min(round(((1:newSize(1))-0.5)./scale(1)+0.5),oldSize(1));
colIndex = min(round(((1:newSize(2))-0.5)./scale(2)+0.5),oldSize(2));

接下来,为行和列计算一组新的索引.首先,计算上采样图像的一组索引:1:newSize(...).每个图像像素被认为具有给定的宽度,例如像素 1 从 0 跨越到 1,像素 2 从 1 跨越到 2,等等.因此,像素的坐标"被视为中心,这就是为什么 0.5从指数中减去.然后将这些坐标除以比例因子以给出原始图像的一组像素中心坐标,然后将 0.5 添加到它们并四舍五入以获得原始图像的一组整数索引.调用 min 确保没有其中的索引大于原始图像大小oldSize(...).

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 to min ensures that none of these indices are larger than the original image size oldSize(...).

outputImage = inputImage(rowIndex,colIndex,:);

最后,通过简单地索引原始图像来创建新的上采样图像.

Finally, the new upsampled image is created by simply indexing into the original image.

这篇关于MATLAB 中的最近邻插值算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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