MATLAB 中的最近邻插值算法
-
20-09-2019 - |
题
我正在尝试编写自己的函数,通过使用最近邻插值算法来放大输入图像。不好的部分是我能够看到它是如何工作的,但无法找到算法本身。我将不胜感激任何帮助。
以下是我尝试将输入图像放大 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
这是之后的输出 标记的建议
解决方案
不久前我浏览了代码 imresize
函数在 MATLAB 图像处理工具箱 为图像的最近邻插值创建简化版本。以下是它将如何应用于您的问题:
%# 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
函数,尽管您在评论之一中提到不想使用内置函数。
编辑:解释
如果有人感兴趣,我想我会解释上面的解决方案是如何工作的......
newSize = max(floor(scale.*oldSize(1:2)),1);
首先,要获得新的行和列大小,请将旧的行和列大小乘以比例因子。该结果向下舍入为最接近的整数 floor
. 。如果比例因子小于 1,您最终可能会遇到一种奇怪的情况,即其中一个大小值为 0,这就是调用 max
是否可以将小于 1 的值替换为 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(...)
.
outputImage = inputImage(rowIndex,colIndex,:);
最后,通过简单地索引原始图像来创建新的上采样图像。
其他提示
这个答案比试图简洁和高效更具解释性。我认为 诺维斯的解决方案在这方面是最好的。如果您想了解它是如何工作的,请继续阅读...
现在您的代码的问题是您正在将输入图像的位置映射到输出图像,这就是您得到的原因 参差不齐的 输出。考虑一个输入图像全白而输出初始化为黑色的示例,我们得到以下结果:
你应该做的是相反的(从输出到输入)。为了说明这一点,请考虑以下符号:
1 c 1 scaleC*c
+-----------+ 1 +----------------------+ 1
| | | | | |
|----o | <=== | | |
| (ii,jj) | |--------o |
+-----------+ r | (i,j) |
inputImage | |
| |
+----------------------+ scaleR*r
ouputImage
Note: I am using matrix notation (row/col), so:
i ranges on [1,scaleR*r] , and j on [1,scaleC*c]
and ii on [1,r], jj on [1,c]
这个想法是对于每个位置 (i,j)
在输出图像中,我们希望将其映射到输入图像坐标中“最近”的位置。由于这是一个简单的映射,我们使用映射给定的公式 x
到 y
(给定所有其他参数):
x-minX y-minY
--------- = ---------
maxX-minX maxY-minY
在我们的例子中, x
是个 i
/j
协调和 y
是个 ii
/jj
协调。因此,将每一个替换为:
jj = (j-1)*(c-1)/(scaleC*c-1) + 1
ii = (i-1)*(r-1)/(scaleR*r-1) + 1
将各个部分放在一起,我们得到以下代码:
% read a sample image
inputI = imread('coins.png');
[r,c] = size(inputI);
scale = [2 2]; % you could scale each dimension differently
outputI = zeros(scale(1)*r,scale(2)*c, class(inputI));
for i=1:scale(1)*r
for j=1:scale(2)*c
% map from output image location to input image location
ii = round( (i-1)*(r-1)/(scale(1)*r-1)+1 );
jj = round( (j-1)*(c-1)/(scale(2)*c-1)+1 );
% assign value
outputI(i,j) = inputI(ii,jj);
end
end
figure(1), imshow(inputI)
figure(2), imshow(outputI)
MATLAB 已经为您完成了。使用 调整大小:
output = imresize(input,size(input)*2,'nearest');
或者如果你想同等地缩放 x 和 y,
output = imresize(input,2,'nearest');
您只需要一个更通用的公式来计算 xloc 和 yloc。
xloc = (j * (newwidth+1)) / (x+1);
yloc = (i * (newheight+1)) / (y+1);
这假设您的变量对于乘法结果有足够的范围。