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!