color transform to lab then to rgb

조회 수: 9 (최근 30일)
Malini Bakthavatchalam
Malini Bakthavatchalam 2020년 5월 31일
댓글: Malini Bakthavatchalam 2020년 6월 1일
Hi i have a doubt, I am working of color transformation and trying to find a better color space for my work. I wrote the code using matlab help but all i can see is a error, i amnot sure how to modify my code. My input image is a facce of a person in white background. could you help in correcting my code. And also I have one more question, what is the difference between lab2srgb or srgbt2lab inbuilt code in matlab from using applycform?
my code
%%convert to different color spaces:
%lab color space
I = imread('facergbimg.jpg');
rgb = reshape(im2double(I)/255,[],3);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
lab = applycform(rgb,cform);
transform = makecform('lab2srgb','AdaptedWhitePoint', whitepoint('D65'));
srgb = applycform(lab,tranform);
% labresh = reshape(rgb.*lab, size(I));
% bckrgb = reshape(reshape(labresh,[],3)*inv(lab),[],size(labresh)); (here i thought of using inverse & reshape function to get back to rgb, but didnot work)
subplot(1,2,1),imshow(lab);
subplot(1,2,2),imshow(srgb);
% %
  댓글 수: 4
Rik
Rik 2020년 6월 1일
They should still look the same. If you convert a color from RGB to HSV you haven't changed the color, only the numbers describing the color. In that sense it is meaningless to try to see what that color looks like in HSV, because it is the same color as it was in RGB. Only when you do a specific operation that changes the values will you change the color.
Malini Bakthavatchalam
Malini Bakthavatchalam 2020년 6월 1일
Thhanks Rik, so if I have to display the image, we should change them to rgb(displayable image). if that is the case, can I use the inverse of the lab = applycform(rgb,cform); to get back to rgb or reshape function like this
labresh = reshape(rgb.*lab, size(I));
% bckrgb = reshape(reshape(labresh,[],3)*inv(lab),[],size(labresh));

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 6월 1일
Try this:
rgbImage = imread('peppers.png');
labImage = rgb2lab(rgbImage);
lImage = labImage(:, :, 1);
aImage = labImage(:, :, 2);
bImage = labImage(:, :, 3);
% Display everything
subplot(2, 2, 1);
imshow(rgbImage);
fontSize = 20;
title('RGB Image', 'FontSize', fontSize);
subplot(2, 2, 2);
imshow(lImage, []);
title('L Image', 'FontSize', fontSize);
% Display everything
subplot(2, 2, 3);
imshow(aImage, []);
title('A Image', 'FontSize', fontSize);
% Display everything
subplot(2, 2, 4);
imshow(bImage, []);
title('B Image', 'FontSize', fontSize);
  댓글 수: 3
Image Analyst
Image Analyst 2020년 6월 1일
Malini, note that I did convert to lab, and I did display the lab channels. It does not make sense to display an LAB image as RGB as you realize so that's why I displayed each separately.
You can make a roundtrip using rgb2lab() and then lab2rgb() and get back the original RGB image.
rgbImage = imread('peppers.png');
labImage = rgb2lab(rgbImage);
% Recover the RGB image
rgbImage2 = lab2rgb(labImage);
% It's a double image in the range 0-1.
% Display everything
subplot(2, 2, 1);
imshow(rgbImage);
fontSize = 20;
title('RGB Image', 'FontSize', fontSize);
subplot(2, 2, 2);
imshow(rgbImage2, []);
title('Recovered RGB Image', 'FontSize', fontSize);
% Get a uint8 version.
maxValue = max(rgbImage(:))
% Scale rgbImage2 to original range and cast to uint8
rgbImage2 = uint8(rescale(rgbImage2, 0, maxValue));
subplot(2, 2, 3);
imshow(rgbImage2, []);
Malini Bakthavatchalam
Malini Bakthavatchalam 2020년 6월 1일
THank you for this clear explanation..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by