Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Crop an Object from multiple Images (shows error in code)

Subject: Crop an Object from multiple Images (shows error in code)

From: Nehal

Date: 9 Jul, 2010 12:15:20

Message: 1 of 9

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.

Subject: Crop an Object from multiple Images (shows error in code)

From: Steven Lord

Date: 9 Jul, 2010 13:10:31

Message: 2 of 9


"Nehal " <arnab620@yahoo.com> wrote in message
news:i173so$jai$1@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@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

Subject: Crop an Object from multiple Images (shows error in code)

From: Nehal

Date: 9 Jul, 2010 13:26:04

Message: 3 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <i17747$aus$1@fred.mathworks.com>...
>
> "Nehal " <arnab620@yahoo.com> wrote in message
> news:i173so$jai$1@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@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..?

Subject: Crop an Object from multiple Images (shows error in code)

From: Steven Lord

Date: 9 Jul, 2010 13:57:19

Message: 4 of 9


"Nehal " <arnab620@yahoo.com> wrote in message
news:i1781c$7to$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <i17747$aus$1@fred.mathworks.com>...
>>
>> "Nehal " <arnab620@yahoo.com> wrote in message
>> news:i173so$jai$1@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@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

Subject: Crop an Object from multiple Images (shows error in code)

From: Image Analyst

Date: 9 Jul, 2010 14:14:04

Message: 5 of 9

"Nehal " <arnab620@yahoo.com> wrote in message <i1781c$7to$1@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);

Subject: Crop an Object from multiple Images (shows error in code)

From: Nehal

Date: 9 Jul, 2010 14:49:04

Message: 6 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <i179rv$2up$1@fred.mathworks.com>...
>
> 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@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
>

I am not using numberOfColorBands anymore... my function is:

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
grayImage = rgb2gray(filename);

% converts gray to binary
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);
imwrite(croppedImage,['Cropped\' filename]);
end
end

and it is showing error:
??? Error using ==> rgb2gray>parse_inputs at 82
MAP must be a m x 3 array.

Error in ==> rgb2gray at 35
X = parse_inputs(varargin{:});

Error in ==> cropimage at 10
grayImage = rgb2gray(filename);

Now, what is the problem...? can u plz help me..?

Subject: Crop an Object from multiple Images (shows error in code)

From: Mark Hayworth

Date: 9 Jul, 2010 15:51:04

Message: 7 of 9

SAME problem. Same solution.
Like I said before, filename is not your numerical image array, "data1" is. So, use the same fix:
grayImage = rgb2gray(data1);

Subject: Crop an Object from multiple Images (shows error in code)

From: Nehal

Date: 9 Jul, 2010 16:45:21

Message: 8 of 9

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i17arc$4a8$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <i1781c$7to$1@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);

thank you... I was so stressed that I could not notice this simple mistake... thank you once again... :)

Subject: Crop an Object from multiple Images (shows error in code)

From: Nehal

Date: 9 Jul, 2010 16:45:21

Message: 9 of 9

"Mark Hayworth" <hayworth@none.com> wrote in message <i17gh8$kj$1@fred.mathworks.com>...
> SAME problem. Same solution.
> Like I said before, filename is not your numerical image array, "data1" is. So, use the same fix:
> grayImage = rgb2gray(data1);

thank you... I was so stressed that I could not notice this simple mistake... thank you once again... :)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us