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
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
2015-4-7
编辑:Geoff Hayes
2015-4-7
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
2015-4-7
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/
采纳的回答
更多回答(1 个)
Geoff Hayes
2015-4-7
编辑:Geoff Hayes
2015-4-7
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Import, Export, and Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!