Algorithm for converting RGB image to Grayscale in detail

1 回表示 (過去 30 日間)
Nehal  Jain
Nehal Jain 2012 年 8 月 25 日
コメント済み: Image Analyst 2014 年 2 月 23 日
Simple steps to convert RGB image to Grayscale

回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 8 月 25 日
Note: the formula in that Wikipedia section is for the sRGB color space. rgb2gray() uses a different formula, based upon the assumption that the values are in a linear colorspace instead of in sRGB.

Image Analyst
Image Analyst 2012 年 8 月 25 日
編集済み: Image Analyst 2012 年 8 月 25 日
One way:
grayImage = rgb2gray(rgbImage);
Another way is to take one color channel:
grayImage = rgbImage(:, :, 2); % Take green channel.
Some useful formulas are here:
  2 件のコメント
Rawan hamdi
Rawan hamdi 2014 年 2 月 23 日
This is my code and i get an error which says Index exceeds matrix dimensions.
Error in ==> graysc at 14 gray(i,j) = 0.29 * rgb(:,:,1) + 0.59 * rgb(:,:,2) + 0.11 * rgb(:,:,3);
%%code
function [gray] = graysc(input)
rgb = double(imread(input));
[h,w,k] = size(rgb);
gray = zeros(h,w,k);
for i = 1 : h
for j =1 : w
% Assume you have an RGB image of class double, or create a random one
gray(i,j) = 0.29 * rgb(:,:,1) + 0.59 * rgb(:,:,2) + 0.11 * rgb(:,:,3);
end
end
imshow(gray);
end
Image Analyst
Image Analyst 2014 年 2 月 23 日
What is the value of k? It should be 3. Then, don't use k when you allocate space for gray with zeros(). Just do
gray = zeros(h, w);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImport, Export, and Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by