Matlab-数字图像处理-基础实验-图片展缩(最近邻、双线性插值)

这篇博客介绍了如何在MATLAB中实现图像的缩放,包括使用最近邻插值和双线性插值两种方法。代码示例详细展示了从读取图像到计算新图像像素的过程,以及在不同缩放因子下图像的变化。在双线性插值中,博主遇到了图像出现网格状失真的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值