Identify the individual sides (or boundary) of the binary image.

10 vues (au cours des 30 derniers jours)
Sam Ade
Sam Ade le 16 Déc 2021
Commenté : Image Analyst le 17 Déc 2021
I have a binary image that looks like the attached picture. I am trying to identify the XY coordinates of the inner curve (in red)), as well as the outer curve (in yellow) without using a method that first find the central axis of the entire white pixel. I will appreciate any hint or pointers on doing this.
  1 commentaire
Sam Ade
Sam Ade le 17 Déc 2021
Thank you all for input.
I found @Matt J solution most efficient and works well for other similar test images.
Thank you @Matt J

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 16 Déc 2021
I=(1:size(bw,1))';
[~,Jred]=max(bw,[],2);
[~,Jyellow]=max( cumsum(bw,2),[],2);
imshow(bw);hold on
k=Jred>1;
plot(Jred(k),I(k),'xr');
k=Jyellow>1;
plot( Jyellow(k),I(k),'xy');hold off
  1 commentaire
yanqi liu
yanqi liu le 16 Déc 2021
yes,sir,it is a great method
for every row,use start index to left,use cumsum index to right

Connectez-vous pour commenter.

Plus de réponses (3)

KSSV
KSSV le 16 Déc 2021
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/835530/image.jpeg') ;
I1 = rgb2gray(I) ;
I2 = imbinarize(I1) ;
[y,x] = find(I2) ;
idx = boundary(x,y) ;
xb = x(idx) ;
yb = y(idx) ;
imshow(I)
hold on
plot(xb,yb,'-*g')

yanqi liu
yanqi liu le 16 Déc 2021
clc; clear all; close all;
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/835530/image.jpeg');
bw = im2bw(img);
bw = imfill(bwareafilt(imopen(bw, strel('disk', 3)), 1), 'holes');
be = bwperim(bw);
be2 = bwareafilt(imopen(be, strel('line', 2, 0)), 2);
be2 = imdilate(be2, strel('disk', 50));
be(be2) = 0;
% left and right
c = find(bw(round(size(bw,1)/2), :));
be1 = bwselect(be, c(1), round(size(bw,1)/2));
be2 = bwselect(be, c(end), round(size(bw,1)/2));
figure; imshow(bw);
hold on;
[r1,c1] = find(be1); plot(c1,r1,'r.')
[r2,c2] = find(be2); plot(c2,r2,'y.')

Image Analyst
Image Analyst le 16 Déc 2021
Modifié(e) : Image Analyst le 16 Déc 2021
[EDIT] I created an image and am attaching it. And added comments to the bottom of the post.
You forgot to attach the original binary image. What I'd do to find the left and right edge is scan down line by line using find():
% Read in image.
binaryImage = logical(imread('image copy.png'));
imshow(binaryImage);
axis('on', 'image');
impixelinfo;
[rows, columns] = size(binaryImage)
% Find left and right edges.
leftEdge = nan(rows, 1);
rightEdge = nan(rows, 1);
for row = 1 : rows
col = find(binaryImage(row, :), 1, 'first');
if ~isempty(col)
leftEdge(row) = col;
rightEdge(row) = find(binaryImage(row, :), 1, 'last');
end
end
% Plot edges over original image.
r = 1 : rows;
hold on;
lineWidth = 5; % Whatever you want.
plot(leftEdge, r, 'r-', 'LineWidth', lineWidth);
plot(rightEdge, r, 'y-', 'LineWidth', lineWidth);
% Maximize figure
g = gcf;
g.WindowState = 'maximized'
I'm not exactly sure what you mean by "inside" and "outside". You actually show lines along the left edge and right edge. Since they overlay the blob it's hard to tell if they are inside the blob (covering white pixels) or outside the blob (covering black pixels). Actually it looks like you hand drew the colored lines and so they wander from outside to inside and back outside again.
But, for each line in the image, each edge has one pixel that is outside (and it will be black) and one pixel that will be inside (and it will be white). My left and right edges are inside -- meaning they lie on the white pixels. If you want the black pixels, which are outside the blob, you need to subtract 1 from leftEdge and add one to rightEdge.
  2 commentaires
Sam Ade
Sam Ade le 17 Déc 2021
Thank you for your input and time. Also, my apologies for not inclduing the original image but an edited copy for the image showing the edges (or boundary sections) I am interested in. The shape is a planar curve. As such, I refered to the left and right edges as the inside and outside of the curve respectively.
Your solution worked for the initial image and I understand the process. However, when I tried it for a diffrent but similar image, I got an output that was not a perfect trace of the edges as attached
Image Analyst
Image Analyst le 17 Déc 2021
Looks like it's because you are using a JPG image with dark compression artifacts. The others solutions also show these bad artifacts since their edges are fuzzy. This is why one should never ever use JPG images for image analysis. Use only uncompressed format images, like PNG. If you had used a PNG image, it would have been perfect. If you want to try to salvage the bad image, try to threshold it.
binaryImage = imread('lousy image.jpg'); % Read in JPG image.
if max(binaryImage(:)) > 1
% Actually it's a gray scale image. Try to avoid dark, noisy artifacts.
binaryImage = binaryImage > 128; % or some threshold that works.
end

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by