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

조회 수: 8 (최근 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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by