Function to convert RGB to grayscale
Mostra commenti meno recenti
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 Commenti
Thorsten
il 7 Apr 2015
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
il 7 Apr 2015
Modificato: Geoff Hayes
il 7 Apr 2015
Geoff Hayes
il 7 Apr 2015
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
il 7 Apr 2015
Image Analyst
il 7 Apr 2015
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/
Risposta accettata
Più risposte (1)
Geoff Hayes
il 7 Apr 2015
Modificato: Geoff Hayes
il 7 Apr 2015
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.
1 Commento
Silvia Caruso
il 7 Apr 2015
Categorie
Scopri di più su Import, Export, and Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!