krammer
krammer

Reputation: 2668

MATLAB imresize increases image size on disc on downscaling to half

I was trying to downscale a .png image using imresize by 0.5 which was originally 25 kB. However, on saving the scaled image using imwrite, the size of the saved image becomes 52 kB.

The following is the image and the commands:

Original Image

image=imread('image0001.png');
B = imresize(image, 0.5);
imwrite(B,'img0001.png','png');

This also happens if the resolution is specified as follows:

B = imresize(image, [400 300]);

What is the reason for this? It seems to work fine when scaled to 0.15.

Upvotes: 2

Views: 562

Answers (1)

Marcin
Marcin

Reputation: 238507

The reason is that imresize uses bicubic interpolation, thus producing additional pixel values. Your original image is small since it has small number of unique pixel values. After interpolation the number will increase, thus increasing the file size.

To preserve the number of unique values you can use: B = imresize(image, 0.5, 'nearest');. You can check it as follows:

image=imread('image0001.png');
B = imresize(image, 0.5);

numel(unique(image)); % gives 18
numel(unique(B));     % gives 256

with new interpolation:

image=imread('image0001.png');
B = imresize(image, 0.5, 'nearest');

numel(unique(image)); % gives 18    
numel(unique(B));   % gives 18

Saving B now should produce smaller size.

Upvotes: 4

Related Questions