MATLAB: Writing a jpg image to a graphic file

image processingImage Processing Toolboxjpg artifacts

Hello,
I want to extract the rgb color information from a jpg image do some processing and then construct the image back. I did it and i wanted to check the changes before and after then i noticed that there is something went wromg. So to cut to the chase, I just want to know why the following simple task is not as i expect it to be? What did i miss?
img=imread('IMGs/HD1.jpg');
r=img(:,:,1);
g=img(:, :, 2);
b=img(:,:,3);
rgb=cat(3,r,g,b);
imwrite(rgb,'IMGs/test.jpg');
MyImage = imread('IMGs/HD1.jpg');
MarkedImage = imread('IMGs/test.jpg');
MyRedChannel = MyImage(:, :, 1);
MarkedRedChannel = MarkedImage(:, :, 1);
MyRedChannel = reshape(MyRedChannel', 1, []);
MarkedRedChannel = reshape(MarkedRedChannel', 1, []);
[r,c] = find(MyRedChannel~=MarkedRedChannel);
MyRedChannel = reshape(MyRedChannel', 1, []);
MarkedRedChannel = reshape(MarkedRedChannel', 1, []);
[r,c] = find(MyRedChannel~=MarkedRedChannel);
///////////////////////////////////////////////////////////////////////////////////////////////////
The result: c=1×4655332 double
///////////////////////////////////////////////////////////////////////////////////////////////////
What i expect is to have c=[], empty. Because i just isolated the color information and reconstruct the image without any change. But obviously there is something that i'm missing something about the jpg structure.

Best Answer

Try this to see the difference between how MATLAB does lossy compression and how your original image had it done:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
echo off;
MyImage = imread('HD1.jpg'); % Read in original image.
subplot(2, 2, 1);
imshow(MyImage);
title('Original Image', 'FontSize', fontSize);
impixelinfo;
[r, g, b] = imsplit(MyImage);
rgb = cat(3,r,g,b);
imwrite(rgb,'test.jpg'); % Write out a lossy image.
% Read back in output image and see how different it is.
MarkedImage = imread('test.jpg');
MyRedChannel = MyImage(:, :, 1);
[MarkedChannelR, MarkedChannelG, MarkedChannelB] = imsplit(MarkedImage);
% Find locations of all pixels that are different:
[diffRows, diffCols] = find(r ~= MarkedChannelR);
% Find differences
diffImage = double(MyImage) - double(MarkedImage);
subplot(2, 2, 2);
imshow(diffImage);
title('Difference Image', 'FontSize', fontSize);
% Show results in a histogram
subplot(2, 1, 2);
histogram(diffImage);
grid on;
title('Histogram of Difference Image', 'FontSize', fontSize);
xlabel('Difference', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
You'll note that from the upper right image that there are many, many placed where there are differences, and the bottom histogram shows you the distribution of those differences. Most are zero but there are plenty of non-zero differences.