Filter löschen
Filter löschen

writing a jpg image to a graphic file

1 Ansicht (letzte 30 Tage)
RMT
RMT am 15 Nov. 2020
Bearbeitet: RMT am 15 Nov. 2020
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=1x4655332 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.
  2 Kommentare
Image Analyst
Image Analyst am 15 Nov. 2020
Bearbeitet: Image Analyst am 15 Nov. 2020
You forgot to attach 'HD1.jpg' so I'm not doing anything yet.
But since you're using JPG images, against all advice, you can expect that 'HD1.jpg' and test.jpg will be different. How MATLAB compresses and decompresses the JPG image might be different than whoever made 'HD1.jpg'. And of course there will be JPG block artifacts in both images that make them different. That's why you never use JPG format images if you can at all help it.
By the way, reshaping is not needed.
RMT
RMT am 15 Nov. 2020
Bearbeitet: RMT am 15 Nov. 2020
Thanks for you're reply but actually i didn't forget to attach the image; but when i tied to attach it, it was embedd in the body of the question so i remopved it. And yes i should have attached it correctly.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Nov. 2020
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.
  1 Kommentar
RMT
RMT am 15 Nov. 2020
Bearbeitet: RMT am 15 Nov. 2020
Thanks a lot Image Analyst for your response. I always look for your comments in the MATLAB community. You have the experience and what makes it even better is the clear and neat codes you provide to explain the situation and provide best possible solutions. So thanks again and best regards.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by