Identify the individual sides (or boundary) of the binary image.
7 views (last 30 days)
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.
More Answers (3)
yanqi liu on 16 Dec 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));
[r1,c1] = find(be1); plot(c1,r1,'r.')
[r2,c2] = find(be2); plot(c2,r2,'y.')
Image Analyst on 16 Dec 2021
Edited: Image Analyst on 16 Dec 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'));
[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');
leftEdge(row) = col;
rightEdge(row) = find(binaryImage(row, :), 1, 'last');
% Plot edges over original image.
r = 1 : rows;
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.