Curve Extraction from Binary Image

4 次查看(过去 30 天)
Hi Mathworks community. I have a black and white image and I am trying to extract the polynomial equation of the third line from the left in the image shown below. I am having trouble extracting the locations of the non-zero pixels and then determining the polynomial coefficients (fitting the line). My code is below.
% Label image with 1,2,3,4 for each connected component
L = bwlabeln(BW3);
% return the row and column locations of label #3 within the image
[row,col]=find(L(:,:) == 3);
% Extract the polynomial coeffecients of each line in the image
n=2;
p = polyfit(col,row,n)
% Show the original black and white image and hold on axis
imshow(BW3)
hold on
% Plot the determined polynomial line as a red line on the same figure
x=0:.1:500;
plot(polyval(p,x),x,'r')

回答(1 个)

Amrtanshu Raj
Amrtanshu Raj 2021-2-4
Hi,
In the image there are only 2 labels generated by bwlabeln,
Assuming that you wish to generate a polynomial and plot it for the curve in middle (which is label 2 in L) of the image, I have made edits to your code so that it generates the required plot.
The major error was in the line
p = polyfit(col,row,n)
Code -
% Label image with 1,2,3,4 for each connected component
L = bwlabeln(BW3);
% return the row and column locations of label #3 within the image
[row,col]=find(L == 2)
% Extract the polynomial coeffecients of each line in the image
n=2;
p = polyfit(row,col,n);
% Show the original black and white image and hold on axis
imshow(BW3)
hold on
% Plot the determined polynomial line as a red line on the same figure
x=0:.1:500;
plot(polyval(p,x),x,'r')
Hope this help !!

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by