obtaining greyscale image as nxm matrix

i have a gray scale image and wish to obtain values in matrix form.(as in an n x m matrix).This is what i have attempted :
I = imread('cameraman.tiff');
[xmax,ymax]=size(I)
for x=0:1:xmax
for y=0:1:ymax
I(x,y)
end
end
but am getting an error:
??? Subscript indices must either be real positive integers or logicals.

 Accepted Answer

I is already a matrix, so what do you mean by obtain values in matrix form?
Matrix indexing in matlab starts at 1, so your loops should be
for x = 1:xmax
for y = 1:ymax
%whatever you want to do
end
end
Note that if indexing started at 0, then your loop would have to terminate at xmax-1, not xmax.
Also note that indexing is in the form (row, column), so if you use the standard convention that x is horizontal and y vertical, then your indexing should be:
I(y, x)

7 Comments

this is what i had already tried dude.
I = imread('cameraman.tiff'); [xmax,ymax]=size(I)
for x=0:xmax-1
for y=0:ymax-1
I(x,y)
end
end
but im still gettin the same error
??? Subscript indices must either be real positive integers or logicals.
Error in ==> codetest5 at 8 I(x,y)
and by matrix form i mean this: [ a b c d; e f g h; i j k l]
Did you actually read my answer?
matlab indexing starts at 1, so for y = 1:ymax
matlab uses row, column indexing, so I(y, x)
I still have no idea what you mean by matrix form. I is already a matrix. Ca you show an example of the output you want as valid matlab syntax?
k sorry got it.
What i meant was in a row and column form,looking like this:
double click on I in the workspace browser and you'll set its value in the variable editor
Or just type I in the command window
italic what i really need to do is implement a 2d gaussian fitting.I am modifying the code given in:
where i need to put in the values of data obtained from my image. But I am not able to modify the code as stated in the comment by Nathan neff-mallon .Im a stuck in this part:
change line 38 to [X,Y] = meshgrid(x,y); where x and y a vectors with your x an y axis points change line 47 to Z = zdata; where _ _ zdata is a length(x) by length(y) matrix with the data you wish to fit such that zdata(i,j)=z(x_i,y_j). _ _
It's a completely different question to your original one, so you should start a new question.
thanks Guillaume

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!