1
votes

I'm reading an image and doing some processing on the blue channel without changing the Red nor the green channels.

When i finished processing the blue channel, i merged back the three channels into one RGB image. and when i use imshow to view the channels, every thing is alright and i can see that the changes i've made only affect the Blue channel and they do not affect the red nor the green ones.

Up to this point every thing is alright !

But when i save the image using imwrite, the resulting image is slightly different, in that the changes made on the blue channel seem to get propagated to the red and green channels, it's like imwrite is doing some kind of mean between the 3 channels :

 image = imread('image.jpg', IMREAD_COLOR);
 split(image, channels);
// Create some changes on channels[0]

merge(channels, 3, image);
// Up to this point every thing is alright
imwrite("modified.jpg", image); // Image changes when written;

Is there any solution to avoid this behavior ?

1

1 Answers

5
votes

JPG is a lossy format: https://en.wikipedia.org/wiki/JPEG

JPEG (/ˈdʒeɪpɛɡ/ JAY-peg)1 is a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography. The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality. JPEG typically achieves 10:1 compression with little perceptible loss in image quality.

Solution: Use a lossles Format like PNG to save your image.