imagematlabrgbresize-image

Shades of gray image instead of RGB


I need to resize image without imresize() function. But the result comes in black&white colors even if i upload RGB image. What should I change to receive RGB image?

clc;
clear;

[FileName, PathName] = uigetfile('*.JPG');
I=imread([PathName FileName]);  

ms=input('Input index of compression(>1)');
[m,n,v]=size(I);

if mod(m,ms)~=0
    m=ms*floor(m/ms);
end
if mod(n,ms)~=0
    n=ms*floor(n/ms);
end
C=I(1:m-1,1:n-1,:);
A=double(C);

figure
imshow(C)

[x,y,~]=size(A);
result=zeros(floor(x/ms),floor(y/ms));
p=1;
t=1;
for i=ms+1:ms:x
    for j=ms+1:ms:y
        arr=A(i-ms:i,j-ms:j);
        k=max(max(arr));
        result(t,p)=k;
        p=p+1;
    end
    t=t+1;
    p=1;
end

Ci=uint8(result);
figure
imshow(Ci) ```

Solution

  • An RGB image has 3 dimensions. The image matrix returned by the imread function has the size of height × width × channels where the number of channels is 3 (red, green and blue).

    If you want to get a result which is also an RGB image, you have to initialize it as such, and fill its values along the third dimension with the R, G, and B color values:

    result=zeros(floor(x/ms),floor(y/ms), 3);    % it has 3 color layers
    p=1;
    t=1;
    for i=ms+1:ms:x
        for j=ms+1:ms:y
            arr=A(i-ms:i,j-ms:j, :);    % the color info remains unchanged
            k=max(max(arr));
            result(t,p,:)=k;    % `result` is a 3D array
            p=p+1;
        end
        t=t+1;
        p=1;
    end