function created for transform grayscale to binary doesn't work

Hi,
I've created this function m file to convert grayscale images to binary images (as gray2bin matlab function):
function conv_to_BW(im)
[m,n]=size(im);
BW=zeros(m,n);
for i=1:m
for j=1:n
if im(i,j)<10
BW(i,j)=0;
else BW(i,j)=1;
end
end
end
But, when I apply this function to a file in grayscale as:
conv_to_BW(grayscalefile);
it doesn't generate BW variable-image-matrix and doesn't generate m,n variables.

 채택된 답변

Matt Fig
Matt Fig 2012년 8월 6일
You didn't specify a return value for your function. If you want your function to return something, you have to tell it to do so.
function BW = conv_to_BW(im)

댓글 수: 5

Ok...I've forgotten it. Thanks
...and to put returned ans-variable in a variable BW ? (it currently displays BW as "ans")
You should read the help for FUNCTION.
help function
Welcome back Matt!
Thanks, Sean de. (MathWorker!)

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

추가 답변 (1개)

John Petersen
John Petersen 2012년 8월 6일
If you're interested, this should be faster code if your image is very large or you're doing many of them.
function BW = conv_to_BW(im)
[m,n]=size(im);
BW = ones(m,n);
BW(im<10)) = 0;

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2012년 8월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by