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:
Multiple Image to Matrix Conversion

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 6 Aug, 2010 19:38:04

Message: 1 of 20

I have 1500 binary images. I have been trying to create a matrix that will have 1500 number of columns. Each column will contain all the image elements of an image.

Here's the code I have been trying:


function test

tifFiles = dir('*.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; % takes the an image and it's file name
        
        I = imread(filename); % reads the image to I

        F(k) = reshape(I,[],1); % reshapes the matrix into 1 column

    end

G = [F(1):F(k)]; % creates the final matrix

end


but it's showing error:
??? Subscripted assignment dimension mismatch.
Error in ==> test at 9
F(k) = reshape(I,[],1);


What's wrong..? can anyone plz help me..?

Subject: Multiple Image to Matrix Conversion

From: Walter Roberson

Date: 6 Aug, 2010 19:47:05

Message: 2 of 20

Nehal wrote:
> I have 1500 binary images. I have been trying to create a matrix that
> will have 1500 number of columns. Each column will contain all the image
> elements of an image.

You do not, however, promise that all of the images are exactly the same size.


> Here's the code I have been trying:
>
>
> function test
>
> tifFiles = dir('*.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; % takes the an image and it's
> file name
> I = imread(filename); % reads the image to I
>
> F(k) = reshape(I,[],1); % reshapes the matrix into 1 column

F(k) is a single location, and you are trying to store an entire column in
that location. *If* you are sure that each image is exactly the same size,
instead use F(:,k) as the destination.

> end
>
> G = [F(1):F(k)]; % creates the final matrix

and in that case you would not use G

>
> end
>
>
> but it's showing error:
> ??? Subscripted assignment dimension mismatch.
> Error in ==> test at 9
> F(k) = reshape(I,[],1);
>
>
> What's wrong..? can anyone plz help me..?


If the matrices are not necessarily all exactly the same file, then use cell
arrays, F{k} = reshape... However in that case you would have to figure out
what it means to put everything into the same matrix.

Subject: Multiple Image to Matrix Conversion

From: Matt J

Date: 6 Aug, 2010 19:48:04

Message: 3 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3hoas$6f$1@fred.mathworks.com>...

> but it's showing error:
> ??? Subscripted assignment dimension mismatch.
> Error in ==> test at 9
> F(k) = reshape(I,[],1);
======

F(k) is a 1x1 location in an array and you're trying to shove a non-scalar quantity into it.
The line G = [F(1):F(k)] is also not what you want.

Probably the best thing to do, assuming you've pre-allocated G (recommended) is the following:

function test

tifFiles = dir('*.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; % takes the an image and it's file name
        
        I = imread(filename); % reads the image to I

        G(:,k)= I(:); % reshapes the matrix into 1 column

    end



end

Subject: Multiple Image to Matrix Conversion

From: us

Date: 6 Aug, 2010 19:52:04

Message: 4 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3hoas$6f$1@fred.mathworks.com>...
> I have 1500 binary images. I have been trying to create a matrix that will have 1500 number of columns. Each column will contain all the image elements of an image.
>
> Here's the code I have been trying:
>
>
> function test
>
> tifFiles = dir('*.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; % takes the an image and it's file name
>
> I = imread(filename); % reads the image to I
>
> F(k) = reshape(I,[],1); % reshapes the matrix into 1 column
>
> end
>
> G = [F(1):F(k)]; % creates the final matrix
>
> end
>
>
> but it's showing error:
> ??? Subscripted assignment dimension mismatch.
> Error in ==> test at 9
> F(k) = reshape(I,[],1);
>
>
> What's wrong..? can anyone plz help me..?

one of the solutions

     flst=dir('*.tif');
     nf=numel(flst);
     dat=cell(flst,1);
for i=1:nf
     img=imread(flst(i).name);
     dat{i,1}=img(:);
end
     dat=cat(2,dat{:});

us

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 6 Aug, 2010 20:01:23

Message: 5 of 20

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3hotk$7k9$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <i3hoas$6f$1@fred.mathworks.com>...
>
> > but it's showing error:
> > ??? Subscripted assignment dimension mismatch.
> > Error in ==> test at 9
> > F(k) = reshape(I,[],1);
> ======
>
> F(k) is a 1x1 location in an array and you're trying to shove a non-scalar quantity into it.
> The line G = [F(1):F(k)] is also not what you want.
>
> Probably the best thing to do, assuming you've pre-allocated G (recommended) is the following:
>
> function test
>
> tifFiles = dir('*.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; % takes the an image and it's file name
>
> I = imread(filename); % reads the image to I
>
> G(:,k)= I(:); % reshapes the matrix into 1 column
>
> end
>
>
>
> end


actually I was trying....

G = [image1 image2 image3 ..... image1500]

where
image1 = [ 1st element of 1st image
                2nd element of 1st image
                3rd element of 1st image
                       .
                       .
                last element of 1st image ]

image1 has only 1 column and G has 1500 columns.

Subject: Multiple Image to Matrix Conversion

From: Matt J

Date: 6 Aug, 2010 20:10:25

Message: 6 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3hpmj$r6r$1@fred.mathworks.com>...

> actually I was trying....
>
> G = [image1 image2 image3 ..... image1500]
>
> where
> image1 = [ 1st element of 1st image
> 2nd element of 1st image
> 3rd element of 1st image
> .
> .
> last element of 1st image ]
>
> image1 has only 1 column and G has 1500 columns.
===============

Yes. That's what the code I proposed produces.

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 6 Aug, 2010 20:18:05

Message: 7 of 20

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i3hq7h$212$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <i3hpmj$r6r$1@fred.mathworks.com>...
>
> > actually I was trying....
> >
> > G = [image1 image2 image3 ..... image1500]
> >
> > where
> > image1 = [ 1st element of 1st image
> > 2nd element of 1st image
> > 3rd element of 1st image
> > .
> > .
> > last element of 1st image ]
> >
> > image1 has only 1 column and G has 1500 columns.
> ===============
>
> Yes. That's what the code I proposed produces.

oh.. yes.. but some how.. variable "G" is not showing in my workspace...
But i need it there because of my further process...
what's the problem..? why is it not showing there..?

Subject: Multiple Image to Matrix Conversion

From: James Tursa

Date: 6 Aug, 2010 21:03:05

Message: 8 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3hqlt$1el$1@fred.mathworks.com>...
>
> oh.. yes.. but some how.. variable "G" is not showing in my workspace...
> But i need it there because of my further process...
> what's the problem..? why is it not showing there..?

Did you bother to return it? e.g., change

function test

to

function G = test


James Tursa

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 7 Aug, 2010 03:55:07

Message: 9 of 20

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i3hta9$jbi$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <i3hqlt$1el$1@fred.mathworks.com>...
> >
> > oh.. yes.. but some how.. variable "G" is not showing in my workspace...
> > But i need it there because of my further process...
> > what's the problem..? why is it not showing there..?
>
> Did you bother to return it? e.g., change
>
> function test
>
> to
>
> function G = test
>
>
> James Tursa

a little progress... now there shows a variable... but it's not "G".. the variable it shows: "ans".
I was expecting "G".
can you help me..?

Subject: Multiple Image to Matrix Conversion

From: ImageAnalyst

Date: 7 Aug, 2010 04:08:26

Message: 10 of 20

On Aug 6, 11:55 pm, "Nehal " <arnab...@yahoo.com> wrote:
> a little progress... now there shows a variable... but it's not "G".. the variable it shows: "ans".
> I was expecting "G".
> can you help me..?
-----------------------------------------------------
If you just call test, that's what it will say since you didn't stuff
the return value into any array. If you had said
g1 = test() then it would have said g1 = that number instead of ans.
ans is short for "answer" and is the default name given to return
values when you don't accept them into any variable, but simply just
call the function. Make sense?

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 7 Aug, 2010 08:45:10

Message: 11 of 20

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <fd68a9fa-ab00-438a-b483-320ed825190a@d8g2000yqf.googlegroups.com>...
> On Aug 6, 11:55 pm, "Nehal " <arnab...@yahoo.com> wrote:
> > a little progress... now there shows a variable... but it's not "G".. the variable it shows: "ans".
> > I was expecting "G".
> > can you help me..?
> -----------------------------------------------------
> If you just call test, that's what it will say since you didn't stuff
> the return value into any array. If you had said
> g1 = test() then it would have said g1 = that number instead of ans.
> ans is short for "answer" and is the default name given to return
> values when you don't accept them into any variable, but simply just
> call the function. Make sense?

yes.. I understood. But I created an array of return values. Still, it doesn't work.

function G = test

tifFiles = dir('*.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; % takes the an image and it's file name
        
        I = imread(filename); % reads the image to I

        G(:,k)= I(:); % reshapes the matrix into 1 column
        
    end

end

I am suppose to get a variable "G" in my workspace. But it is still not showing. Why..?

Subject: Multiple Image to Matrix Conversion

From: ImageAnalyst

Date: 7 Aug, 2010 13:10:47

Message: 12 of 20

On Aug 7, 4:45 am, "Nehal " <arnab...@yahoo.com> wrote:
> yes.. I understood. But I created an array of return values. Still, it doesn't work.
> I am suppose to get a variable "G" in my workspace. But it is still not showing. Why..?
-----------------------------------------------------------------------
You think you understand but you don't. First of all, your code won't
work unless all images are the same size, but let's assume they are.
Now let's say you have two images and do

>> test
you'll see:
ans =

  167 167
  167 167
  167 167 etc.

Now let's say you do
>> g1=test
You'll see
g1 =

  167 167
  167 167
  167 167 etc.

Note there's no G in the base workspace because I didn't accpet the
output into any variable called G. Also observe that when I didn't
accept it into ANY variable, it called it "ans."

Now let's do
>> G = test

G =

  167 167
  167 167
  167 167 etc.

NOW, we DO have a variable called G in the base workspace. By
coincidence I called it G, same name as the variable internal to the
function but they're different G's in different workspaces/scopes.
Even when I was not accepting the output into any variable or a
variable called g1, there was a variable G created BUT it existed (had
scope in) ONLY the function called "test." If you put a breakpoint
INSIDE that function near the very end, you will in fact see a G
variable.

However, once test existed, that G variable went out of scope and was
probably destroyed along with all the other variables internal to it
(i.e., I, k, filename, & tiffFiles). The G that lived in that
function never makes it out of that function, it basically just
transfers it's values into the variable that you've set up to accept
all the values, in other words it transfers the values into the
variable on the left hand side of your call statement, such as g1, G,
or whatever name you choose to use. If you don't use a variable, then
it just calls it "ans." NOW, does it make sense?

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 7 Aug, 2010 16:53:03

Message: 13 of 20

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e270b806-50a7-4c7e-98e2-c4f7b8595568@s9g2000yqd.googlegroups.com>...
> On Aug 7, 4:45 am, "Nehal " <arnab...@yahoo.com> wrote:
> > yes.. I understood. But I created an array of return values. Still, it doesn't work.
> > I am suppose to get a variable "G" in my workspace. But it is still not showing. Why..?
> -----------------------------------------------------------------------
> You think you understand but you don't. First of all, your code won't
> work unless all images are the same size, but let's assume they are.
> Now let's say you have two images and do
>
>
> However, once test existed, that G variable went out of scope and was
> probably destroyed along with all the other variables internal to it
> (i.e., I, k, filename, & tiffFiles). The G that lived in that
> function never makes it out of that function, it basically just
> transfers it's values into the variable that you've set up to accept
> all the values, in other words it transfers the values into the
> variable on the left hand side of your call statement, such as g1, G,
> or whatever name you choose to use. If you don't use a variable, then
> it just calls it "ans." NOW, does it make sense?

1st of all...
all the images are the same size...

2nd...
I have the idea that some how "G" (G = test) in my code is getting destroyed.. I tried to come up with a solution.. but wasn't able to.

Subject: Multiple Image to Matrix Conversion

From: James Tursa

Date: 7 Aug, 2010 16:57:03

Message: 14 of 20

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e270b806-50a7-4c7e-98e2-c4f7b8595568@s9g2000yqd.googlegroups.com>...
>
> However, once test existed, that G variable went out of scope and was
> probably destroyed along with all the other variables internal to it
> (i.e., I, k, filename, & tiffFiles). The G that lived in that
> function never makes it out of that function, it basically just
> transfers it's values into the variable that you've set up to accept
> all the values, in other words it transfers the values into the
> variable on the left hand side of your call statement, such as g1, G,
> or whatever name you choose to use.

To clarify a bit, a statement such as:

X = test(stuff)

will cause the the data area of the G in the test workspace to be directly attached to the data area of X in the calling workspace. There will be no data copy involved so you don't have to worry about inefficiencies in that step of the process.


James Tursa

Subject: Multiple Image to Matrix Conversion

From: ImageAnalyst

Date: 7 Aug, 2010 17:01:13

Message: 15 of 20

On Aug 7, 12:53 pm, "Nehal " <arnab...@yahoo.com> wrote:
> I have the idea that some how "G" (G = test) in my code is getting destroyed.. I tried to come up with a solution.. but wasn't able to.
----------------------------------------------------------------

Well then it's very difficult, or impossible, to help you further.
Can you search through all your code and print here every single place
where the word "test" appears? Then we should be able to see if
"test" is even being called, and it if is, what variable (if any) is
taking the returned results.

Subject: Multiple Image to Matrix Conversion

From: Walter Roberson

Date: 7 Aug, 2010 17:08:02

Message: 16 of 20

Nehal wrote:
> function G = test
>
> tifFiles = dir('*.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; % takes the an image and it's
> file name
> I = imread(filename); % reads the image to I
>
> G(:,k)= I(:); % reshapes the matrix into 1 column
> end
>
> end
> I am suppose to get a variable "G" in my workspace. But it is still not
> showing. Why..?

As you are resistant to the correct and accurate solutions you have been
shown, you need to add the following hack to your program:

assignin('caller', 'G', G);

Subject: Multiple Image to Matrix Conversion

From: Matt J

Date: 7 Aug, 2010 17:11:02

Message: 17 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3k31f$6j$1@fred.mathworks.com>...
 all the images are the same size...
>
> 2nd...
> I have the idea that some how "G" (G = test) in my code is getting destroyed.. I tried to come up with a solution.. but wasn't able to.
===================

But now you understand that it does not get destroyed, correct? When you run the function

function G=test
 ...
 G=....; %This doesn't
end


the result G is just a temporary variable that will be assigned to the variable "ans" in the workspace where you issued a call to test(). That will always automatically happen unless you specifically assign the result to something else.

Subject: Multiple Image to Matrix Conversion

From: Nehal

Date: 7 Aug, 2010 17:37:03

Message: 18 of 20

Walter Roberson <roberson@hushmail.com> wrote in message <Spg7o.40648$Bh2.12963@newsfe04.iad>...
> Nehal wrote:
> > function G = test
> >
> > tifFiles = dir('*.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; % takes the an image and it's
> > file name
> > I = imread(filename); % reads the image to I
> >
> > G(:,k)= I(:); % reshapes the matrix into 1 column
> > end
> >
> > end
> > I am suppose to get a variable "G" in my workspace. But it is still not
> > showing. Why..?
>
> As you are resistant to the correct and accurate solutions you have been
> shown, you need to add the following hack to your program:
>
> assignin('caller', 'G', G);

Thank You a lot. It worked.

And Thank You EVERYONE for helping me out.. :)

Subject: Multiple Image to Matrix Conversion

From: Matt J

Date: 7 Aug, 2010 21:16:04

Message: 19 of 20

"Nehal " <arnab620@yahoo.com> wrote in message <i3k5jv$9ji$1@fred.mathworks.com>...

> >
> > assignin('caller', 'G', G);
>
> Thank You a lot. It worked.


It's a dangerous solution. If you don't understand why it's unnecessary, you're bound for trouble.

Subject: Multiple Image to Matrix Conversion

From: Walter Roberson

Date: 8 Aug, 2010 03:21:27

Message: 20 of 20

Matt J wrote:
> "Nehal " <arnab620@yahoo.com> wrote in message
> <i3k5jv$9ji$1@fred.mathworks.com>...
>
>> > > assignin('caller', 'G', G);
>>
>> Thank You a lot. It worked.
>
>
> It's a dangerous solution. If you don't understand why it's unnecessary,
> you're bound for trouble.

Some people just don't learn until the light matches burn their fingers. :(

Tags for 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