How to get xy coordinates from a binary image matrix

How do I get the xy coordinates of all the 1's in a binary matrix?

1 commentaire

Jaime
Jaime le 7 Mar 2026 à 1:05
To get the (x, y) coordinates of all the 1s in a binary matrix, the basic idea is:
  1. Loop through the rows and columns.
  2. Whenever you find a 1, record its coordinates.
Below are common ways depending on the language or tool.Python (Pure Python)
matrix = [
[0,1,0],
[1,1,0],
[0,0,1]

Connectez-vous pour commenter.

 Réponse acceptée

If M is your matrix and it looks like this:
M = logical(randi(2,5)-1)
M =
0 0 1 0 1
0 1 0 1 1
1 1 0 1 0
1 0 0 1 0
0 0 1 1 1
find the rows and colums of each 1 in M by
[rows,cols] = find(M)
Then x and y are whatever x and y values you have that correspond to the rows and columns of M.

2 commentaires

And Ian, be careful to not make the very common beginner mistake of thinking (x,y) = (rows, columns). So if you want the variables to be names x and y, or xy, then do this:
[y, x] = find(M); % x and y are column vectors.
xy = [x, y]; % Two columns. Column 1=x, column 2=y;
DO NOT do what so many beginners do:
[x, y] = find(M); % WRONG!
Hi Sir,
Attached is the signal whose x y coordinates we want to extract. The one in yellow color making a wave-like pattern.
On x-axis: time
On y-axis: velocity
So that after getting the data points, if we plot these values in excel, it should draw/ exhibit the same wave pattern.
Any help would be appreciated.

Connectez-vous pour commenter.

Plus de réponses (1)

Juan Alberto Antonio Velazquez
Modifié(e) : Walter Roberson le 7 Mar 2026 à 1:49
if you want to find the center of mass of a binary image
%Codigo que sirve para encontrar el centro de masa de una objeto en una
%imagen
clc;
clear all;
close all;
dir='/Users/jalberto/Documents/PROYECTO DETECCION DE PERSONAS/ACTIVESHAPEMODEL/ASMNEW/ModeloFondo/Testing/fotomov1/sinfondo138';
I = imread([dir,'.png']);
Im=im2bw(I);
%figure, imshow(Im);
[y,x]=find(Im==1);
xy=[x,y];
media=mean(xy);
figure, imshow(Im);
hold on
plot(media(:,1),media(:,2),'*b');
by Juan Alberto Antonio Velazquez from Toluca, México

2 commentaires

Jaime
Jaime le 7 Mar 2026 à 1:06
Modifié(e) : Walter Roberson le 7 Mar 2026 à 1:50
Yes — that code computes the center of mass (centroid) of a binary image. Let’s break down what it does and show a slightly cleaner version.Key Idea
For a binary image, the centroid is simply the average of the coordinates of all pixels equal to 1.
Mathematically:xc=1Nxi,yc=1Nyix_c = \frac{1}{N}\sum x_i , \quad y_c = \frac{1}{N}\sum y_ixc=N1xi,yc=N1yi
where NNN is the number of pixels with value 1.
Explanation of the MATLAB Code
clc; clear all; close all;
dir='/Users/.../sinfondo138';
I = imread([dir,'.png']);
Im = im2bw(I); % convert image to binary
[y,x] = find(Im==1); % find coordinates of pixels that are 1
xy = [x,y]; % combine coordinates
media = mean(xy); % compute mean (centroid)
figure, imshow(Im);
hold on
plot(media(:,1), media(:,2), '*b'); % plot centroid
What each step does
StepPurposeim2bw(I)Converts image to binary (0/1)find(Im==1)Gets indices of all white pixels[y,x]MATLAB returns row (y) then column (x)mean(xy)Average position → center of massplot()Shows centroid on the image
Simpler Version
You don't need to build xy.
I = imread('image.png');
Im = imbinarize(I);
[y,x] = find(Im);
cx = mean(x);
cy = mean(y);
imshow(Im); hold on
plot(cx, cy, '*r')
Even Better (Built-in MATLAB Function)
MATLAB already has a function for this:
stats = regionprops(Im,'Centroid');
centroid = stats.Centroid;
imshow(Im); hold on
plot(centroid(1), centroid(2), '*r')
Using regionprops is usually preferred.
Summary
To find the centroid of a binary image:
  1. Get coordinates of all 1 pixels (find).
  2. Compute the mean x and y.
  3. Plot the result.
Image Analyst
Image Analyst le 7 Mar 2026 à 13:00
If your binary image has multiple blobs, using regionprops will get you the centroid of each blob individually. The first code chunk gives you the centroid of the entire image ignoring if there might be multiple blobs. They're different. It just depends on what you want.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Particle & Nuclear Physics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by