使用MATLAB中的最近邻重采样公式降低空间分辨率

时间:2014-09-08 16:40:44

标签: image matlab nearest-neighbor

我正在尝试在matlab中编写一个函数,该函数将返回原始图像中缩小为MS =长度x分辨率(x)分辨率的图像,NS =原始图像中的y分辨率长度使用最近邻公式的图像。

我遇到了一个超出范围的异常,并且在确定解决方案时遇到了一些困难。我假设我需要另一个条件检查。目前,对于我的图片,它正在尝试访问d(1309,27),但它不应该访问该值,因为它在for循环中受限MNfor ms = 1:M

     function  r  = imaging( s, M, N, L )
     %imaging Computes the acquisition of the image
         s= imread('Z:\file.tif');
         figure, imshow(s);


         d = im2double(s);

         [MS, NS] = size(d);

         M = 400;
         N = 100;

         dx = M/MS;
         dy = N/NS;

         for ms = 1:M
            mp= floor( (ms +0.5)/dx ) ;
               for ns = 1:N
                  if(d(ms,ns))
                  np= floor( (ns +0.5)/dy ) ;
                  r(ms,ns)= d(mp,np);
               end
         end
    end

1 个答案:

答案 0 :(得分:0)

我假设您最近邻居的大小调整不是按比例缩放的。如果要缩放,您应该使用 imresize 功能来简化您的生活。如果不进行缩放,则应使用以下代码为索引创建查找表。

round( linspace( 1, MS, M ) )

索引表示最终图像索引。输出表示它应在原始图像中查找的索引。这是针对M的情况,对于N情况也是如此。您的最终代码应如下所示。测试并按如下方式工作。

% Image init
s = imread( 'bag.png' );
figure; imshow( s );
d = im2double( s );

% Get sizes
[MS, NS] = size( d );
M = 400;
N = 100;

% Create image
r = zeros( M, N );

% Create table lookup
lookupM = round( linspace( 1, MS, M ) );
lookupN = round( linspace( 1, NS, N ) );

% Assign New R
for ms = 1:M
    for ns = 1:N
        r(ms,ns) = d( lookupM( ms ), lookupN( ns ) );
    end
end

% Display output
figure; imshow(r);

如果您有任何疑问,请与我们联系。我发布StackOverflow响应还是新手。

编辑:使用linspace函数而不是interp1函数