Function to convert RGB to grayscale

How can I convert an RGB or indexed images into gray scale without using B=ind2gray(A,map) in matlab? I should create a new function to do it.

5 件のコメント

Thorsten
Thorsten 2015 年 4 月 7 日
Why don't you use ind2gray? Is it homework? Then please show what you've done and where you got stuck. And are you sure that it has to work for both RGB and indexed images? Because that's completely different to implement.
Silvia Caruso
Silvia Caruso 2015 年 4 月 7 日
編集済み: Geoff Hayes 2015 年 4 月 7 日
Yes, it is homework.I tried to write this function (to convert rgb to grayscale),but it doesn't work and I don't know how to define newmap into the function.
function [ I,newmap ] = convRGB2gray(RGB, map)
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
[n,m]=size(RGB);
for i=1:n
for j=1:m
I(i,j)=0.2989*R(i,j)+0.5870*G(i,j)+0.1140*B(i,j);
end
end
end
Geoff Hayes
Geoff Hayes 2015 年 4 月 7 日
Silvia - in your above function you are setting the R, G, and B variables to that from I which is doesn't exist at this point (and is an output parameter). I think that you mean to do
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
and define I to be as
[n,m,c] = size(RGB);
I = cast(zeros(n,m),class(RGB));
Note that since RGB is presumably a 3-dimensional array you must specify a third output to size else m will be the product of the number of columns of RGB with three.
When you encounter problems like this, try stepping through the code (using the debugger) to see what the problem is.
Silvia Caruso
Silvia Caruso 2015 年 4 月 7 日
thank you
Image Analyst
Image Analyst 2015 年 4 月 7 日
Good catch by Geoff about your misuse of the size() function. Steve Eddins has a blog about this common issue with size(): http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/

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

 採用された回答

James Garritano
James Garritano 2015 年 4 月 7 日

0 投票

Your code confuses "I" with "RGB" Your code uses "RGB" as one of its inputs, but it refers to "I" as if it were the input instead. Either rename your input to "I" or replace "I" with "RGB."
For example:
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
size is being used improperly. RGB is a three-dimensional variable, but you are only trying to save two of the dimensions' sizes. In the code you've posted, the variable that you designated as m will have a value equal to product of the number of columns and the number of colors in your image.
If you want to use size, replace your call with this:
[rows,cols,colors]=size(RGB);
You are reversing the convention for rows and columns. Normally, we designate rows as "m" and columns as "n." (e.g., "an m by n matrix") This resulted in an error in your code, but I'd advise either not using those variable names or switching to the m = rows, n = columns convention.
Do not use a for loop for matrix mathematics Your code should be rewritten so you don't use any loops.
Whenever possible, in MATLAB, attempt to remove loops from your code if you can accomplish the same thing with matrix mathematics. If you're dealing a large number of large images, your code will function poorly.
In your example, you can rewrite your loop as an arithmetic statement.
I = 0.2989 *R + 0.5870 * G + 0.1140 * B;
This avoids using the loop. This will make the code perform several thousand times faster. (on my machine, it took around 8.4 seconds to run a loop, but 0.004 seconds to to perform the a matrix addition)

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2015 年 4 月 7 日
編集済み: Geoff Hayes 2015 年 4 月 7 日

0 投票

Silvia - look at the algorithm used by rgb2gray which creates the grayscale value by calculating the weighted sum of the red (R), green (G), and blue (B) channels
0.2989 * R + 0.5870 * G + 0.1140 * B
You could try doing this for your RGB images. If the image is indexed, then see how ind2gray does the same.

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by