怎样用matlab实现图像的缩放,matlab实现图像缩放

【实例简介】不调用方法,手动实现图像缩放

【实例截图】

d38ca3c8cf05c762d8ef355e2df02434.png

【核心代码】

% MATLAB script for Assessment Item-1

% Task-1

clear; close all; clc;

%% Step-1: Load input image and conversion of input image to grey-scale image

I = imread('Zebra.jpg');

figure;

imshow(I);

title('Load input image', 'FontSize', 18);

Igray = rgb2gray(I);

figure;

imshow(Igray);

title('Conversion of input image to greyscale', 'FontSize', 18);

%% Step-2: Nearest-neighbor interpolation

n = 3; % multiple of expand

[w,h] = size(Igray); % the size of greyscale

Inearest = zeros(w*n,h*n); % build a matrix which size is triple times of original image

for row1 = 1:w*n % build a circluation to traverse every pixel

for col1 = 1:h*n

row = round(row1/n); % copy the value of pixel based on index

col = round(col1/n);

if row<1

row = 1;

end

if row > w

row = w;

end

if col < 1

col = 1;

end

if col > h

col = h;

end

% ensure the index of pixel is positive integer

Inearest(row1,col1) = Igray(row,col); % use pixels of original image to fill output image

end

end

Inearest=uint8(Inearest); % Conversion of double to uint8

figure;

imshow(Inearest);

colorbar;

axis on;

title('Figure 1: Nearest-neighbor interpolation', 'FontSize', 22);

%% Step-3: Bilinear Interpolation

Ibilinear = zeros(w*n,h*n); % build a matrix which size is triple times of original image

for row2 = 1:w*n % build a circluation to traverse every pixel

for col2 = 1:h*n

row = row2/n; % copy the value of pixel based on index

col = col2/n;

dec1 = row-floor(row); % get fractional part for bilinear interpolation formula

dec2 = col-floor(col);

if row < 1

row = 1;

end

if row > w

row = w;

end

if col < 1

col = 1;

end

if col > h

col = h;

end

% ensure the index of pixel is positive integer

Ibilinear(row2,col2) = (1-dec2)*(1-dec1)*Igray(floor(row),floor(col)) ...

dec2*(1-dec1)*Igray(floor(row),ceil(col)) ...

(1-dec2)*dec1*Igray(ceil(row),floor(col)) ...

dec2*dec1*Igray(ceil(row),ceil(col));

% use pixels of original image to fill output image based on bilinear interpolation formula

end

end

Ibilinear = uint8(Ibilinear); % Conversion of double to uint8

figure;

imshow(Ibilinear);

colorbar;

axis on;

title('Figure 2: Bilinear interpolation', 'FontSize', 22);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值