writing a jpg image to a graphic file
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2020-11-15
编辑:Image Analyst
2020-11-15
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.
采纳的回答
Image Analyst
2020-11-15
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/414973/image.png)
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!