From: Nehal on
I am using the following code to crop an object from an image:
The image is: http://www.mediafire.com/?znwngzjydn3

>> I = imread('filename.tif');
>> [rows columns numberOfColorBands] = size(I);
>> if numberOfColorBands > 1
grayImage = rgb2gray(I);
end
>> binaryImage = grayImage == 255;
>> horizontal = any(binaryImage, 1);
>> vertical = any(binaryImage, 2);
>> column1 = find(horizontal, 1, 'first');
>> column2 = find(horizontal, 1, 'last');
>> row1 = find(vertical, 1, 'first');
>> row2 = find(vertical, 1, 'last');
>> xBox = [column1 column2 column2 column1 column1];
>> yBox = [row1 row1 row2 row2 row1];
>> croppedImage = binaryImage(row1:row2, column1:column2);
>> imshow(croppedImage)


The above code works fine for a single image file to crop an object (in my case, the object is a hand). Now, I have almost 1000 images with the same object extracted from a video file for my project. So, I wanted to create a function that will read all the images from a folder called "Samples" and crop the hand (object) and save it to another folder called "Cropped". I have written the following function based on above code:

% function starts
function cropimage

tifFiles = dir('Samples\*.tif'); %takes all the images from the folder Sample

for k = 1:length(tifFiles) %the loop will continue for the number of images
filename = tifFiles(k).name;
data1 = imread(filename);

% converting the grb images to gray
[rows columns numberOfColorBands] = size(filename);
if numberOfColorBands > 1
grayImage = rgb2gray(filename);
end

% converts gray to binary
binaryImage = grayImage == 255; % line # 16, shows error while executing

horizontal = any(binaryImage, 1);
vertical = any(binaryImage, 2);

column1 = find(horizontal, 1, 'first');
column2 = find(horizontal, 1, 'last');
row1 = find(vertical, 1, 'first');
row2 = find(vertical, 1, 'last');

xBox = [column1 column2 column2 column1 column1];
yBox = [row1 row1 row2 row2 row1];

croppedImage = binaryImage(row1:row2, column1:column2);
imwrite(croppedImage,['Cropped\' filename]);
end
end
% function ends

Now, the problem is, when I try to execute by putting "cropimage" command in command window, it shows the following error:

??? Undefined function or variable "grayImage".

Error in ==> cropimage at 16
binaryImage = grayImage == 255;

Can anyone plz help me what am I doing wrong...? It would be a great help... plz.
From: Steven Lord on

"Nehal " <arnab620(a)yahoo.com> wrote in message
news:i173so$jai$1(a)fred.mathworks.com...
>I am using the following code to crop an object from an image:
> The image is: http://www.mediafire.com/?znwngzjydn3

*snip*

> The above code works fine for a single image file to crop an object (in my
> case, the object is a hand). Now, I have almost 1000 images with the same
> object extracted from a video file for my project. So, I wanted to create
> a function that will read all the images from a folder called "Samples"
> and crop the hand (object) and save it to another folder called "Cropped".
> I have written the following function based on above code:
> % function starts
> function cropimage
>
> tifFiles = dir('Samples\*.tif'); %takes all the images from the folder
> Sample
>
> for k = 1:length(tifFiles) %the loop will continue for the number of
> images
> filename = tifFiles(k).name;
> data1 = imread(filename);
>
> % converting the grb images to gray
> [rows columns numberOfColorBands] = size(filename);

You're using the wrong variable here ... you want the size of the _image_
not the size of its _filename_. Unless you have a 3D string as the filename
(unlikely at best), numberOfColorBands will always be 1 which means ...

> if numberOfColorBands > 1
> grayImage = rgb2gray(filename);
> end

the body of this IF statement will never execute, and so grayImage is never
defined. [You're using the wrong variable here as well.] Therefore ...

> % converts gray to binary
> binaryImage = grayImage == 255; % line # 16, shows error while executing

when you try to use grayImage on this line, MATLAB (correctly) errors
indicating that there's no variable or function of that name in scope.

Changing the variable you use in your SIZE call will only solve half the
problem -- you need to define an ELSE block for that IF statement to handle
the case where numberOfColorBands equals 1. Otherwise either that line of
code will error (if your first image is M-by-N) or use the previous image's
information (if a later image is M-by-N.)

I would also take a reread through your function to make sure you're using
the correct variable (image filename or image data) in each location where
you use either of those variables. Given that you used the wrong variable
in two separate locations, it's a good idea to check that you didn't do so
in a third.

*snip*

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Nehal on
"Steven Lord" <slord(a)mathworks.com> wrote in message <i17747$aus$1(a)fred.mathworks.com>...
>
> "Nehal " <arnab620(a)yahoo.com> wrote in message
> news:i173so$jai$1(a)fred.mathworks.com...
> >I am using the following code to crop an object from an image:
> > The image is: http://www.mediafire.com/?znwngzjydn3
>
> *snip*
>
> > The above code works fine for a single image file to crop an object (in my
> > case, the object is a hand). Now, I have almost 1000 images with the same
> > object extracted from a video file for my project. So, I wanted to create
> > a function that will read all the images from a folder called "Samples"
> > and crop the hand (object) and save it to another folder called "Cropped".
> > I have written the following function based on above code:
> > % function starts
> > function cropimage
> >
> > tifFiles = dir('Samples\*.tif'); %takes all the images from the folder
> > Sample
> >
> > for k = 1:length(tifFiles) %the loop will continue for the number of
> > images
> > filename = tifFiles(k).name;
> > data1 = imread(filename);
> >
> > % converting the grb images to gray
> > [rows columns numberOfColorBands] = size(filename);
>
> You're using the wrong variable here ... you want the size of the _image_
> not the size of its _filename_. Unless you have a 3D string as the filename
> (unlikely at best), numberOfColorBands will always be 1 which means ...
>
> > if numberOfColorBands > 1
> > grayImage = rgb2gray(filename);
> > end
>
> the body of this IF statement will never execute, and so grayImage is never
> defined. [You're using the wrong variable here as well.] Therefore ...
>
> > % converts gray to binary
> > binaryImage = grayImage == 255; % line # 16, shows error while executing
>
> when you try to use grayImage on this line, MATLAB (correctly) errors
> indicating that there's no variable or function of that name in scope.
>
> Changing the variable you use in your SIZE call will only solve half the
> problem -- you need to define an ELSE block for that IF statement to handle
> the case where numberOfColorBands equals 1. Otherwise either that line of
> code will error (if your first image is M-by-N) or use the previous image's
> information (if a later image is M-by-N.)
>
> I would also take a reread through your function to make sure you're using
> the correct variable (image filename or image data) in each location where
> you use either of those variables. Given that you used the wrong variable
> in two separate locations, it's a good idea to check that you didn't do so
> in a third.
>
> *snip*
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

how can i put the size of the image instead of the size of the filename in that place of the code...?
and what can i do to handle numberOfColorBands equals 1 ?

could u plz help me..?
From: Steven Lord on

"Nehal " <arnab620(a)yahoo.com> wrote in message
news:i1781c$7to$1(a)fred.mathworks.com...
> "Steven Lord" <slord(a)mathworks.com> wrote in message
> <i17747$aus$1(a)fred.mathworks.com>...
>>
>> "Nehal " <arnab620(a)yahoo.com> wrote in message
>> news:i173so$jai$1(a)fred.mathworks.com...
>> >I am using the following code to crop an object from an image:
>> > The image is: http://www.mediafire.com/?znwngzjydn3

*snip*

> how can i put the size of the image instead of the size of the filename in
> that place of the code...?

Reread the code that you wrote. What variable contains the image data? USE
THAT VARIABLE in place of the variable containing the filename.

> and what can i do to handle numberOfColorBands equals 1 ?

You have to determine that on your own; I don't know the specific details of
the procedure you're using.

I've given you as much help as I can -- you will need to take the debugging
information I gave you and determine how to correct those problems in your
code.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Image Analyst on
"Nehal " <arnab620(a)yahoo.com> wrote in message <i1781c$7to$1(a)fred.mathworks.com>...
> how can i put the size of the image instead of the size of the filename in that place of the code...?
> and what can i do to handle numberOfColorBands equals 1 ?
>
> could u plz help me..?
------------------------------------------------------------------------------------------
You called your image "data1" so the correct line would be
[rows columns numberOfColorBands] = size(data1);