Debdeep Basu
Debdeep Basu

Reputation: 33

pixel values changing after doing imwrite in MATLAB

The imwrite function is behaving in a weird way. I have modified a single pixel value of an image. After performing imwrite, however, the pixel value is either getting changed to a entirely new value OR is remaining unchanged.

function imwriteCheck(input_image,output_image)
a=imread(input_image);
fprintf('\nBefore modification a(1,1,1)=%d\n',a(1,1,1));
a(1,1,1)=50;
fprintf('\nAfter modification a(1,1,1)=%d\n',a(1,1,1));
imwrite(a,output_image);
b=imread(output_image);
fprintf('\nValue at b(1,1,1)=%d\n',b(1,1,1));
end

I've tested this function with two images and the outputs are as follows:

>> imwriteCheck('MOM.jpg','MOMout.jpg')

Before modification a(1,1,1)=206

After modification a(1,1,1)=50

Value at b(1,1,1)=170

>> imwriteCheck('durga.jpg','durgaout.jpg')

Before modification a(1,1,1)=63

After modification a(1,1,1)=50

Value at b(1,1,1)=63

I cannot understand why this is happening. Thank you for your help.

Upvotes: 3

Views: 694

Answers (1)

Dibyabiva Seth
Dibyabiva Seth

Reputation: 74

if you write to a jpg file, pixel values get changed because of their lossy compression technique. you can write to a jpg file using lossless mode but then you wont be able to view the image anywhere else. try writing to a bmp or png file, you'll see the pixel values are NOT changing.

Upvotes: 4

Related Questions