RGB to Gray function with Matlab code

hi all , we all know the Matlab belt in rgb2gray ,
i am tring to do it myself ,, here's my function
function out=gray(x)
[p,q,n]=size(x);
%out=uint8(ones(m,n,p));
R=x(:,:,1);
G=x(:,:,2);
B=x(:,:,3);
out=((R+G+B)/3);
and i have this error whn calling gray()
??? Subscript indices must either be real positive integers or logicals.
i don't know what's wrong , if any one know the solution .. Thanx in advance ..

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 1일

0 개 추천

gray() is a MATLAB function too. Do the following to see if there are conflicts.
which gray -all

댓글 수: 3

jenny mat
jenny mat 2011년 11월 1일
i changed the function name and it goes well but i have this error instead when viewing the resulted image..
g= rgbtogray2(x);
imshow(g)
//Error
??? Index exceeds matrix dimensions.
Error in ==> gray at 6
G=x(:,:,2);
Error in ==> imageDisplayParseInputs at 207
common_args.Map = gray(256);
Error in ==> imshow at 173
[common_args,specific_args] = ...
thanx in advance
Fangjun Jiang
Fangjun Jiang 2011년 11월 1일
You mean you change the name of your own gray() function? Why did the error message still say "Error in ==> gray at 6, G=x(:,:,2);"?
jenny mat
jenny mat 2011년 11월 1일
yes ,, i deleted the old one and it worked very well ..
thanx very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 1일

0 개 추천

Chances are you are trying to call gray() from within a routine in which you have a variable named "gray".

카테고리

도움말 센터 및 File Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by