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)?
조회 수: 8 (최근 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.
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Blue에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!