How to get the pixel values of an image according to a given sequence.?

Let the image be a 3*3 matrix [12 45 44; 43 13 23; 33 98 99] and let there be a sequence [1 4 5 8 2 3 9 6 7]. I have to select the pixel values in the order of the given sequence and to output the x and y coordinates of each. ie., I have to select 1st element of image matrix and to get its coordinate values, then 4th element and so on.

 Accepted Answer

IM=[12 45 44; 43 13 23; 33 98 99]
SEQ=[1 4 5 8 2 3 9 6 7]
[x,y]=ind2sub(size(IM),SEQ)

2 Comments

"[I,J] = ind2sub(siz,IND) returns the matrices I and J containing the equivalent row and column subscripts"
So it should instead be
[y, x] = ind2sub(size(IM), SEQ)

Sign in to comment.

More Answers (1)

Try this:
IM=[12 45 44; 43 13 23; 33 98 99]
SEQ=[1 4 5 8 2 3 9 6 7]
% To get the x and y coordinates:
[y, x] = ind2sub(size(IM), SEQ) % [y, x] is [rows, columns] which ind2sub returns
% To extract the pixel values in column major order
pixelValues = IM(SEQ)

Asked:

on 1 Jul 2015

Commented:

on 1 Jul 2015

Community Treasure Hunt

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

Start Hunting!