Image processing/Matrix manipulation question

Hello,
I have a 256x256 matrix (matrix 1) where the numerical value in each cell corresponds to an intensity value in an (x, y)-coordinate system (x=rows, y=columns). The intensity may take on a value of 0 or 255. First, I would like to extract the locations of the pixels in the (x, y)-coordinate system with intensity values of 255. Exactly how this is done likely depends on what I am trying to do next, but I assume the extracted coordinate locations will be stored in another matrix (matrix 2).
I also have a matrix (matrix 3) with four columns and thousands of rows. The middle two columns correspond to (x, y)-coordinates, e.g., {a, 1,1, b}, {c, 1,2, d}, etc. What I would like to do is form a fourth matrix (matrix 4) containing only the rows with the specific coordinate locations in matrix 2.
I would appreciate all help, thanks!
John

 Accepted Answer

M1=randi([0 255],256,256) % Your matrix 1
[x,y]=ind2sub([256 256],1:256*256);
M2=[x' y' M1(:)] %
%--------------------------------
M3=[1 2 3 4;5 6 7 8 ;11 22 33 44;55 11 44 77];
M4=M3(:,2:3)

3 Comments

Thank you very much for your answer. For matrix 4, I had a little something different in mind. Please see my comment above.
matrix1=[0 0 0 255 0;255 0 0 0 255;0 0 0 255 0;0 0 0 0 255;255 0 0 0 0]
[ii,jj]=find(matrix1==255)
matrix2=sortrows([ii jj],[1 2])
matrix3= [10 1 4 20;11 1 5 12;14 2 1 25;44 6 1 1;4 4 5 8;1 7 8 1]
matrix4=matrix3(ismember(matrix3(:,2:3),matrix2,'rows'),:)
Thanks, this is spot on.
John

Sign in to comment.

More Answers (1)

The first question is easy:
matrix2 = matrix1 == 255;
For the second question, I'm not sure what you want. matrix2 does not have to have pixels in a rectangle but matrix4 must be rectangular, so I don't know how to build it unless you want matrix4 to just be a long list of pixel values pulled from matrix2 (or matrix1 - I can't tell). Please give a small example with like a 5 by 5 image.

3 Comments

Thanks for your answer. 5x5 example:
matrix 1= {0 0 0 255 0},{255 0 0 0 255},{0 0 0 255 0},{0 0 0 0 255},{255 0 0 0 0}
matrix 2= {1 4},{2 1},{2 5},{3 4},{4 5},{5 1}
matrix 3 has thousands of rows, but I only want to keep the rows which have the (x, y)-coordinate values from matrix 2: {a 1 4 b},{c 2 1 d},{e 2 5 f}, {g 3 4 h}, {i 4 5 j}, {k 5 1 l}
This should result in matrix 4.
*In the real matrix 3, there are 670 rows for each set of (x, y)-coordinates - the difference between them all are the values for the first and last columns (a, b, c...).
Let me know if you need any further clarification.
Why are these cell arrays instead of just regular old integer or double matrices? That complicates things. And I still don't understand the relationship between matrix4 and matrix2 that you mentioned in your original post. Can we assume that the middle columns of matrix3 are always identical to matrix2? Please give the final result for matrix4 because I'm still not sure what matrix4 looks like. Try Azzi's code - does that work?
It seems that Azzi's code works. Probably my explanation could have been better. Thanks again.
John

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!