how to convert from grayscale to rgb by lightness method ??

how to convert from grayscale to rgb by lightness (desaturation) method (matlab code)??

2 Comments

This is my last attempt, but it didn't work
i=imread('peppers.png');
for j=1:size(i,1)
for k=1:size(i,2)
if i(j,k,1)>i(j,k,2)&&i(j,k,3)
max=i(j,k,1);
elseif i(j,k,2)>i(j,k,1)&&i(j,k,3)
max=i(j,k,2);
else
max=i(j,k,3);
end
if i(j,k,1)<i(j,k,2)&&i(j,k,3)
min=i(j,k,1);
elseif i(j,k,2)<i(j,k,1)&&i(j,k,3)
min=i(j,k,2);
else
min=i(j,k,3);
end
newimage=(max+min)/2;
end
end
imshow(newimage);

Sign in to comment.

 Accepted Answer

You can do the following:
I=imread('peppers.png');
newImage = uint8(( double(min(I,[],3)) + double(max(I,[],3)) ) ./ 2);
imshow(newImage)

5 Comments

The original image has the following dimensions
size(imread('peppers.png'))
ans = 1×3
384 512 3
which is a 3D matrix of values that define the image. The third dimension has 3 pages, one for each colour stream; R, G and B.
In this case, they are values between 0 and 255 representing the R,G,B intensity.
max(I,[],3)
gives the max from the R,G and B intensitites and similarly for the entire image, I, while
min(I,[],3)
gives the min. Note, the outputs for the two previous function calls are arrays of size 384 by 512.
You can add the results of these arrays together using the + operator alone, however in order to divide you need to specify element-wise division which is where the ./ comes from.
As you can plainly see, this doesn't work. The bright areas are clipped at 128. I'm giving @Turlough Hughes a chance to fix it.
Thanks @Image Analyst. I should have converted to a datatype not capped at 255 in order to add the values. I've edited the answer with the correction.
Actually, one could also just do the following without converting datatypes:
I=imread('peppers.png');
newImage = min(I,[],3)./2 + max(I,[],3)./2;
imshow(newImage)
Thank you, I benefited a lot 💜💜💜💜

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!