Matlab-数字图像处理-基础实验-图片展缩(使用最近邻、双线性插值)
Problem Statement
1.使用MATLAB开发环境来实现缩放转换。 开发一个函数,可以实现通过最近邻插值的图像缩放:
out = nearest(img, a)
其中img是图片字符串,a是比例因子,a<1或者a>1。
2.开发一个函数,可以实现通过双线性插值的图像缩放:
out = bi_linear(img, a)
其中img是图片字符串,a是比例因子,a<1或者a>1。
Procedure
(A)out = nearest(img, a)
(1) Read image to “img_original” and get image size [row,col].
(2) Initialize the rows and columns of the new image.
(3) Use class()function to obtain the data type of the original image ( “img_original”), so that the new image (out) data type is consistent with the original image.
(4) Traverse the new image (out) and assign to it by calculating the (x,y) of the original image.
(5) Show the results.
(B)out = bi_linear(img, a)
(1) Read image to “img_original” and get image size [row,col].
(2) Initialize the rows and columns of the new image.
(3) Use class()function to obtain the data type of the original image ( “img_original”), so that the new image (out) data type is consistent with the original image.
(4)Loop through the image:
(5) x = i/a; y = j/a;
(6) Calculate x1 and y1
(7) Set the image boundary
(8) Define 4 pixel points P1, P2, P3 and P4, and then assign values to the image through Formula 6 or Formular 3,4,5
Formula 6:
f(x, y) = [ f(1, 0) – f(0, 0)] x + [ f(0, 1) – f(0, 0)]y+ [f(1, 1) – f(0, 1) - f(1, 0) + f(0, 0)]xy+f(0, 0).
Formular 3,4,5:
f(x, 0) = f(0, 0) + x [ f(1, 0) – f(0, 0)]
f(x, 1) = f(0, 1) + x [ f(1, 1) – f(0, 1)]
f(x, y) = f(x, 0) + y [ f(x, 1) – f(x, 0)].
(9)End Loop.
(10)Display image.
Source code
(A)out = nearest(img, a)
function [out] = nearest(img, a)
img_original=imread(img);%500*500
img_original=rgb2gray(img_original);
[row,col] = size(img_original);
%round()舍入到最接近的整数
row = round(row*a); % 新图像行
col = round(col*a); % 新图像列
%250*250 *0.5
% 使用class获得原图像的数据类型,使得新图像数据类型与原图像保持一致
out = zeros(row,col,class(img_original)