Image format converstions for processing

5 views (last 30 days)
hello, all. i have an rgb image of class uint8 with dimension m-by-n-by-3 that i need to convert to double with dimensions m-by-n for processing. after processing i will end up with also a double image of type m-by-n. If i need to convert this back to its original color format how best can i approach this? should i separate the color channels, then convert each channle to grayscale then to double and deal with that? then how do i convert that channel back to colored uint8 so that i can later use the cat function on the three channels? thanks very much.
I am not sure whether to post it to this related post as a comment or ask a new question. So i did both and will remove whichever is inappropriate accordingly. thank you guys.

Accepted Answer

Image Analyst
Image Analyst on 26 May 2021
Alex:
If you want a double, you can use double() on an image. Or im2double().
If you want a gray scale image, you can either call rgb2gray() or take one of the color channels
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels == 3
grayImage = rgb2gray(rgbImage); % Method 1
grayImage = rgbImage(:, :, 1); % Method 2 take red channel
else
grayImage = rgbImage; % It's already gray scale
end
Once you've thrown away color information, you can't get it back. If you need it, be sure to save your original RGB image or read it back in from disk.
If you want your double image to be back in uint8 format you can pass it in to uint8. If it's not in the same range as uint8 you need to decide how to scale it. You can either divide by the max and multiply by 255
gray8 = uint8(255 * grayImage / max(grayImage(:));
or you can scale the min to zero and the max to 1 with mat2gray() or rescale():
gray8 = uint8(255 * mat2gray(grayImage)); % Method 1
gray8 = uint8(rescale(grayImage, 0, 255)); % Method 2
You can convert a gray scale image into a gray scale RGB image by concatenating your gray scale image so that it is in every color channel. It will be a 3-D RGB image though it will look gray.
rgbImage2 = cat(3, grayImage, grayImage, grayImage);
You could also use ind2rgb() if you want
rgbImage2 = ind2rgb(grayImage, yourColorMap);
  21 Comments
Walter Roberson
Walter Roberson on 30 May 2021
Note that 420 and 422 color compression schemes are "lossy" compression schemes. They throw out information that matters less to the human eye.
MatlabEnthusiast
MatlabEnthusiast on 30 May 2021
thank you guys very much. this has made most basics a more clearer now.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!