Clear Filters
Clear Filters

implement imresize function

1 view (last 30 days)
성근 송
성근 송 on 27 Sep 2021
Answered: Vatsal on 22 Feb 2024
I want to scale the screen via Nearest Neighbor interpolation without using imresize. The effect of the photo is adjustable. However, the resulting photo is presented in black and white. I want to express the color of the original photo as it is.
function NN = myResizeNN(picture,scale)
I=imread(picture);
a= size(I,1);
b= size(I,2);
IR = round([1:(b*scale)]./scale);
IC = round([1:(a*scale)]./scale);
output = I(:,IR);
output = output(IC,:);
figure(1); imshow(I);title('Before interpolation');
figure(2); imshow(output);title('After interpolation');
NN = [IR,IC];
end

Answers (1)

Vatsal
Vatsal on 22 Feb 2024
Hi,
The provided function does not preserve the color information because the function currently processes only one dimension of the image. Images in MATLAB are 3D matrices, with the third dimension representing color channels (Red, Green, Blue).
Here is a modified function which preserves the color:
function NN = myResizeNN(picture, scale)
I = imread(picture);
[a, b, ~] = size(I);
IR = round([1:(b*scale)]./scale);
IC = round([1:(a*scale)]./scale);
output = zeros(length(IC), length(IR), 3, 'like', I);
for channel = 1:3
temp = I(:,:,channel);
output_temp = temp(:,IR);
output(:,:,channel) = output_temp(IC,:);
end
figure(1); imshow(I); title('Before interpolation');
figure(2); imshow(output); title('After interpolation');
NN = [IR,IC];
end
I hope this helps!

Categories

Find more on 이미지 in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!