matlab 插值 nearest,基於MATLAB的近鄰插值算法。

20

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...

這個答案比力求簡潔和高效更能說明問題。我認為g初學者的解決方案在這方面是最好的。如果你想了解它是如何工作的,請繼續閱讀……

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:

現在,代碼的問題在於,您是從輸入圖像到輸出圖像的映射位置,這就是為什么您得到了錯誤的輸出。考慮一個示例,其中輸入圖像全部為白色,輸出初始化為黑色,我們得到如下:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9tNE03SC5wbmc=

What you should be doing is the opposite (from output to input). To illustrate, consider the following notation:

您應該做的是相反的(從輸出到輸入)。為了說明這一點,請考慮以下符號:

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]

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 given x to y (given all the other params):

其思想是,對於輸出圖像中的每個位置(i,j),我們希望將其映射到輸入圖像坐標中的“最近”位置。由於這是一個簡單的映射,我們使用了將給定的x映射到y(給定所有其他參數)的公式:

x-minX y-minY

--------- = ---------

maxX-minX maxY-minY

in our case, x is the i/j coordinate and y is the ii/jj coordinate. Therefore substituting for each gives us:

在我們的例子中,x是i/j坐標,y是ii/jj坐標。因此,每給我們一個替換:

jj = (j-1)*(c-1)/(scaleC*c-1) + 1

ii = (i-1)*(r-1)/(scaleR*r-1) + 1

Putting pieces together, we get the following code:

將這些片段組合在一起,我們得到以下代碼:

% 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值