数字图像处理之最近邻内插算法的实现

这是本人的第一篇个人笔记,忘指正错误。

利用matlab实现最近邻内插实现图像的缩放,利用仿射变换的思想,采用后向映射法进行缩放。

原理如下:

src代表原图像,dst代表需要缩放后的图像。w0,h0分别是原图像src的列数以及行数,w1,h1是dst的列数以及行数

对应着图像的宽和高。fw=w0/w1,代表宽度缩放因子,fh=h0/h1代表高度缩放因子。利用dst中的每个像素坐标点后向映射找到在src中的坐标点,寻找最近的像素点,将该点像素值赋值给dst中的点。


matlab代码如下:

% 实现最近邻内插
% 利用仿射变换的思想,采用后向映射法
src = imread('lena.jpeg');
% src = rgb2gray(src);
% 获取图像的宽和高,即图像的列数和行数
[h0, w0, z0] = size(src);
% 定义我们想放大的图像的尺寸
dst = zeros(800,800,3);
[h1, w1, z1] = size(dst);
% 分别得到x轴(即高)的缩放因子和y轴(宽)的缩放因子
fh = h0/h1;
fw = w0/w1;
% 进行差值处理
for i=1:800
    for j=1:800
        index_x=round(i*fh);
        index_y=round(j*fw);
        % 防止下标出现0和上标超过src的大小
        if index_x==0 
            index_x=index_x+1;
        end
        if index_y==0
            index_y=index_y+1;
        end
        if index_x>h0
            index_x=index_x-1;
        end
        if index_y>w0
            index_y=index_y-1;   
        end    
        dst(i,j,:) = src(index_x,index_y,:);
    end
end
figure('name','source image')
imshow(src)
figure('name','target image')
% 在matlab中必须先转换一下数据类型,才可以正常显示
dst = im2uint8(mat2gray(dst));
imshow(dst)

 
 


猜你喜欢

转载自blog.csdn.net/qq_30666517/article/details/78474109