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!