How to change colortype truecolor to grayscale
11 views (last 30 days)
Show older comments
Iqra Rizvi
on 16 Mar 2017
Commented: Image Analyst
on 17 Mar 2017
I am converting rgb image to grayscale using rgb2gray but at the image info its showing true color instead of grayscale. what should i do ? i'm new to matlab .
0 Comments
Accepted Answer
Image Analyst
on 16 Mar 2017
You have to display the gray scale image with imshow() after you do the conversion.
grayImage = rgb2gray(rgbImage);
imshow(grayImage, []); % You probably forgot to do this step.
7 Comments
Image Analyst
on 17 Mar 2017
Show me all your code, including the lines where you save grayImage (not A like you showed already) with imwrite(), and where you call imfinfo() on the newly saved gray scale image file (not the "A" image file which we don't care about.) Here's demo that I know for a fact works:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
rgbImage = imread('peppers.png');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(rgbImage);
end
% Display the color image.
subplot(2, 1, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the gray scale image.
subplot(2, 1, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Save it out
fullFileName = fullfile(pwd, 'Gray Scale Image.bmp');
imwrite(grayImage, fullFileName);
fileInfo = imfinfo(fullFileName)
uiwait(helpdlg('Check out the command window'));
In the command window you will see the proof that it is a gray scale image because it's an 8 bit (BitDepth=8), single plane (NumPlanes=1) image:
fileInfo =
struct with fields:
Filename: 'C:\Users\Mark\Documents\MATLAB\work\Tests\Gray Scale Image.bmp'
FileModDate: '17-Mar-2017 11:53:48'
FileSize: 197686
Format: 'bmp'
FormatVersion: 'Version 3 (Microsoft Windows 3.x)'
Width: 512
Height: 384
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'BM'
NumColormapEntries: 256
Colormap: [256×3 double]
RedMask: []
GreenMask: []
BlueMask: []
ImageDataOffset: 1078
BitmapHeaderSize: 40
NumPlanes: 1
CompressionType: 'none'
BitmapSize: 196608
HorzResolution: 0
VertResolution: 0
NumColorsUsed: 256
NumImportantColors: 0
So now you need to basically do the same thing and show me how when you do it, it says NumPlanes = 3 or BitDepth = 24.
More Answers (0)
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!