implement imresize function

1 次查看(过去 30 天)
성근 송
성근 송 2021-9-27
回答: Vatsal 2024-2-22
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

回答(1 个)

Vatsal
Vatsal 2024-2-22
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!

类别

Help CenterFile Exchange 中查找有关 이미지 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!