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

10 ビュー (過去 30 日間)
Sam Ade
Sam Ade 2021 年 12 月 16 日
コメント済み: Image Analyst 2021 年 12 月 17 日
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 件のコメント
Sam Ade
Sam Ade 2021 年 12 月 17 日
Thank you all for input.
I found @Matt J solution most efficient and works well for other similar test images.
Thank you @Matt J

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

採用された回答

Matt J
Matt J 2021 年 12 月 16 日
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 件のコメント
yanqi liu
yanqi liu 2021 年 12 月 16 日
yes,sir,it is a great method
for every row,use start index to left,use cumsum index to right

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

その他の回答 (3 件)

KSSV
KSSV 2021 年 12 月 16 日
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 2021 年 12 月 16 日
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 2021 年 12 月 16 日
編集済み: Image Analyst 2021 年 12 月 16 日
[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 件のコメント
Sam Ade
Sam Ade 2021 年 12 月 17 日
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 2021 年 12 月 17 日
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

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

Community Treasure Hunt

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

Start Hunting!

Translated by