Distance between pixels and axes in a image

2 ビュー (過去 30 日間)
Francesco Pignatelli
Francesco Pignatelli 2023 年 1 月 11 日
Hi all,
I have a small issue related to image processing. I have the following binarized image:
and I would like to compute the distance between the bottom row and the fist pixel=1 for each column of the image. In other words, I would like to compute the length of the following red lines:
Any clue?
Best

採用された回答

DGM
DGM 2023 年 1 月 11 日
編集済み: DGM 2023 年 1 月 11 日
Consider the example:
% a binarized image
inpict = imread('monojagblob.png');
mask = imbinarize(inpict);
imshow(mask)
% pad the array to guarantee no object pixels are on the boundary
mask = padarray(mask,[1 1],0,'both');
% distance to west edge
[~,Wdist] = max(mask,[],2);
Wdist = Wdist-1; % correct for padding
Wdist(Wdist==0) = NaN; % zero distances are invalid (no object here)
plot(Wdist)
% distance to east edge
[~,Edist] = max(fliplr(mask),[],2);
Edist = Edist-1; % correct for padding
Edist(Edist==0) = NaN; % zero distances are invalid (no object here)
plot(Edist)
% distance to north edge
[~,Ndist] = max(mask,[],1);
Ndist = Ndist-1; % correct for padding
Ndist(Ndist==0) = NaN; % zero distances are invalid (no object here)
plot(Ndist)
% distance to south edge
[~,Sdist] = max(flipud(mask),[],1);
Sdist = Sdist-1; % correct for padding
Sdist(Sdist==0) = NaN; % zero distances are invalid (no object here)
plot(Sdist)
Note that these are the locations of the first nonzero pixel, not the number of zero pixels prior to it. If you want the latter, subtract 1.
  1 件のコメント
Francesco Pignatelli
Francesco Pignatelli 2023 年 1 月 11 日
Amazing! Thanks ;)

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by