Hel*_*nar 20 matlab interpolation image-processing nearest-neighbor resize-image
我正在尝试使用最近邻插值算法编写自己的函数来放大输入图像.不好的部分是我能够看到它是如何工作但无法找到算法本身.我将不胜感激任何帮助.
这是我尝试将输入图像放大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
Run Code Online (Sandbox Code Playgroud)
这是马克建议之后的输出
Amr*_*mro 20
这个答案比试图简洁有效更具解释性.我认为gnovice的解决方案在这方面是最好的.如果您试图了解它是如何工作的,请继续阅读......
现在您的代码的问题在于您将位置从输入图像映射到输出图像,这就是您获得不稳定输出的原因.考虑一个示例,其中输入图像全白,输出初始化为黑色,我们得到以下结果:

你应该做的是相反的(从输出到输入).为了说明,请考虑以下符号:
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]
Run Code Online (Sandbox Code Playgroud)
我们的想法是,对于(i,j)输出图像中的每个位置,我们希望将其映射到输入图像坐标中的"最近"位置.由于这是一个简单的映射,我们使用映射给定的公式x,以y(给出的所有其他PARAMS):
x-minX y-minY
--------- = ---------
maxX-minX maxY-minY
Run Code Online (Sandbox Code Playgroud)
在我们的例子中,x是i/ j坐标,y是ii/ jj坐标.因此,替换每一个给我们:
jj = (j-1)*(c-1)/(scaleC*c-1) + 1
ii = (i-1)*(r-1)/(scaleR*r-1) + 1
Run Code Online (Sandbox Code Playgroud)
把碎片放在一起,我们得到以下代码:
% 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)
Run Code Online (Sandbox Code Playgroud)
gno*_*ice 19
前段时间我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,:);
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用内置interp2函数,尽管您提到不想在其中一条注释中使用内置函数.
编辑:解释
如果有人有兴趣,我想我会解释上面的解决方案是如何工作的......
newSize = max(floor(scale.*oldSize(1:2)),1);
Run Code Online (Sandbox Code Playgroud)
首先,为了获得新的行和列大小,旧的行和列大小乘以比例因子.此结果向下舍入到最接近的整数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));
Run Code Online (Sandbox Code Playgroud)
接下来,为行和列计算一组新的索引.首先,计算上采样图像的一组索引:1:newSize(...).每个图像像素被认为具有给定的宽度,使得像素1跨越0到1,像素2跨越1到2等.因此像素的"坐标"被视为中心,这就是为什么0.5从指数中减去.然后将这些坐标除以比例因子,以给出原始图像的一组像素中心坐标,然后将其添加到其中并舍入以获得原始图像的一组整数索引.调用以min确保这些索引都不大于原始图像大小oldSize(...).
outputImage = inputImage(rowIndex,colIndex,:);
Run Code Online (Sandbox Code Playgroud)
最后,通过简单地索引到原始图像来创建新的上采样图像.
| 归档时间: |
|
| 查看次数: |
55742 次 |
| 最近记录: |