How do I convert a 3D RGB image to a grayscale image using the intensity formula (intensity=0.2989*red + 0.5870*green + 0.1140*blue)?

10 次查看(过去 30 天)
Here is part of the script, and I believe my use of the cat function is incorrect but I'm not sure how else to convert the RGB image to grayscale (Side question: Is a grayscale image 2D?):
red=crystal(:,:,1);
green=crystal(:,:,2);
blue=crystal(:,:,3);
red_new(:,:,:)=0.2989*red + 0.5870*green + 0.1140*blue;
green_new(:,:,:)=0.2989*red + 0.5870*green + 0.1140*blue;
blue_new(:,:,:)=0.2989*red + 0.5870*green + 0.1140*blue;
crystal_new=cat(2,crystal_red,crystal_green,crystal_blue);
Thank you! I've been trying various approaches and none seem to be working.

采纳的回答

David Goodmanson
David Goodmanson 2017-4-21
编辑:David Goodmanson 2017-4-21
Hi Janel, Given your first three lines that define red green and blue, if you just take the appropriate sum of those as in your fourth line, BW = 0.2989*red + 0.5870*green + 0.1140*blue, you get an appropriately combined 2d matrix with no third dimension. Black and white images are 2d, or at least that is one way to get them.
If you take image(BW) you will probably get some funny looking colors because the image now uses the current colormap for plotting. The command 'colorbar' will show you the current colormap, on the plot. The command 'colormap gray' gives a black and white colormap and black and white image. That image might be washed out and whitish. It may not be what is being asked for, but using imagesc instead of the image command gives an image with scaled intensity that could be more realistic looking.
This is all from my amateur point of view and people who use images all the time may well come up with something better.
  2 个评论
Jenel Darland
Jenel Darland 2017-4-21
Thank you very much, David! This is great and I appreciate your help. So how is a b&w image different than a grayscale? Or are you using them interchangeably? image(BW) in a new figure only turns out yellow - how can I tell if it should look like that? Apologies, my (lack of) coding skills are very basic.
David Goodmanson
David Goodmanson 2017-4-21
编辑:David Goodmanson 2017-4-21
First of all, a 2d image matrix is a set of one intensity for each pixel. You don't have three-color information for each pixel anymore because with the sum you combined the three color 'intensities' into one intensity for each pixel. Now Matlab has to decide how to present the result. For 2d images it uses a colormap, which you should definitely take a look at with the colorbar command. The smallest intensities get mapped toward the bottom of the colorbar and the largest intensities get mapped toward the top. If you have a colormap with colors, you get a false-color image, as with the yellow map you are seeing. In this case you want intensity to go from black to white and that is what the 'colormap gray' command will do. At that point the colorbar command will show you a black-to-white colorbar.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by