Search code examples
matlabimage-resizing

Resizing image in matlab without using built in imresize() function


I am writing my own script/function in matlab without using built-in command, "imresize" but i am getting 3 output images instead of getting single one. I am sharing my code here also. Kindly someone spot out my mistake.


    %zoomin out an imagge

originalImage = imread('imggerm1.jpg');
[origImRows, origImColumns] = size(originalImage);

newImage = zeros(origImRows/2, origImColumns/2);
newImRow = 1; newImColumn = 1;

for row = 1:2:origImRows
    for column = 1:2:origImColumns
        newImage(newImRow, newImColumn)=originalImage(row, column);
        newImColumn = newImColumn+1;
    end
    newImRow = newImRow+1;
    newImColumn = 1;
end

figure; imshow(originalImage);
figure; imshow(newImage/255);

Output Image


Solution

  • This is because you originally reading a color image, where each pixel is encoded by 3 numbers. Try typing size(originalImage) and you will see that this array is 3 dimensional (the size of the last dimension is 3).

    In your code the following line:

    [origImRows, origImColumns] = size(originalImage);
    

    Produces the result you don't expect: your origImColumns appears to be 3 times bigger.

    Your code is easy to fix. Below I slightly changed 3 lines: #4, #6 and #11:

        %zoomin out an imagge
    
    originalImage = imread('1.jpg');
    [origImRows, origImColumns,~] = size(originalImage);
    
    newImage = zeros(origImRows/2, origImColumns/2,3);
    newImRow = 1; newImColumn = 1;
    
    for row = 1:2:origImRows
        for column = 1:2:origImColumns
            newImage(newImRow, newImColumn,:)=originalImage(row, column,:);
            newImColumn = newImColumn+1;
        end
        newImRow = newImRow+1;
        newImColumn = 1;
    end
    
    figure; imshow(originalImage);
    figure; imshow(newImage/255);