How to get xy coordinates from a binary image matrix
Afficher commentaires plus anciens
How do I get the xy coordinates of all the 1's in a binary matrix?
1 commentaire
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:
- Loop through the rows and columns.
- 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]
Réponse acceptée
Plus de réponses (1)
Juan Alberto Antonio Velazquez
le 3 Mai 2018
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
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=1N∑xi,yc=1N∑yix_c = \frac{1}{N}\sum x_i , \quad y_c = \frac{1}{N}\sum y_ixc=N1∑xi,yc=N1∑yi
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:
- Get coordinates of all 1 pixels (find).
- Compute the mean x and y.
- Plot the result.
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.
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!