bajwa77
bajwa77

Reputation: 53

Storing pixel coordinates in a matrix

I have to store coordinates of pixels of an image in matrix in form of(x,y). The code I am trying is:

[x,y]=size(diff_im);
count=0;
for i=1:x
    for j=1:y
        if a(i,j)==0
            count=count+1;
            new_x(count)=j;
            new_y(count)=i;
        end
    end
end

Currently I am storing x and y in separate arrays. But I would like to know how to store both x,y values in single matrix.

Upvotes: 1

Views: 731

Answers (2)

xanz
xanz

Reputation: 221

I'm not exactly sure of what you want. If all you want is to store couples of new coordinates in a single matrix you can do something very basic like:

[x,y]=size(diff_im);
count=0;
for i=1:x
    for j=1:y
        if a(i,j)==0 %what is this line??
            count=count+1;
            A(count,1)=j;
            A(count,2)=i;
        end
    end
end

Each new pair of coords can then be retreived by A(i,:)

Upvotes: 0

Jonas
Jonas

Reputation: 74940

If all you need is an array where every row is the x/y coordinate of the pixels that are 0 in a, you can use find, followed by a catenation.

[new_y, new_x]=find(a==0); %// x/y are now correct for plotting onto an image
new_xy = [new_x,new_y];

Upvotes: 1

Related Questions