Regenerating an RGB image from its components

1 view (last 30 days)
I extracted the red component of an image and displayed the image with just the red component and I got the following result:
if true
f=imread('Parrot.jpg');
[r,c,d]=size(f);
a=zeros(size(f,1),size(f,2));
red=f(:,:,1);
just_red=cat(3,red,a,a);
figure, imshow(just_red), title('Red component');
end
However, if I save the red component values in a matrix and then use the matrix to form the red image, I get the following result:
if true
f=imread('Parrot.jpg');
[r,c,d]=size(f);
a=zeros(size(f,1),size(f,2));
dred=[values of the 100x100 matrix];
just_red=cat(3,dred,a,a);
figure, imshow(just_red), title('Red component');
end
To give a background of what I am actually doing - I am trying to encrypt and decrypt an image. 'red' is the original red component, which I encrypted and am able to decrypt it successfully till the matrix form of dred (I have checked that red==dred). But dred is not producing the red image that it should.
Any help as to why this is happening will be much appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Mar 2017
You are changing the data class. Your f is almost certainly an integer data type but your a is double precision. When you extract directly from f like you do in the first code, the result in red retains that integer class and then the cat() uses the most restrictive class between the values so the results are the integer data class.
In your second code your dred is double and your a is double so cat is going to leave everything double rather than converting to integer data class.
The data class makes a difference for graphics. uint8 data will expect 0 as the lowest value and 255 as the highest value. double will expect 0. 0as the lowest value and 1.0 as the highest value. But in the second code you will be using 255.0 as your highest value. All double values above 1.0 will be treated as the maximum.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!