Image format converstions for processing

6 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2021-5-26
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 个评论
Walter Roberson
Walter Roberson 2021-5-30
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 2021-5-30
thank you guys very much. this has made most basics a more clearer now.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by