필터 지우기
필터 지우기

How to change colortype truecolor to grayscale

조회 수: 11 (최근 30일)
Iqra Rizvi
Iqra Rizvi 2017년 3월 16일
댓글: Image Analyst 2017년 3월 17일
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 .

채택된 답변

Image Analyst
Image Analyst 2017년 3월 16일
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
Iqra Rizvi
Iqra Rizvi 2017년 3월 17일
no .. it is still an rgb image .. when i'm checking imfinfo
Image Analyst
Image Analyst 2017년 3월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Explore and Edit Images with Image Viewer App에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by