Converting Pixels to Line

Converting pixels to line could be a complicated job. A simple example is shown to serve as the basis of a complicated problem.

1. Reading image

clear all;clc;
I = imread('pic24.jpg');
I = im2bw(I);
imshow(I);

2. Finding all the pixels with the value of '1'

[y,x] = find(I==1);
imshow(I);
hold on;
plot(x,y);



3. Use the "polyfit" function to fit polynomial to data, in our case, since it is a straight line, we use 1st order polynomial or a linear equation Y = M*X + C

p = polyfit(x,y,1);
disp(p);

-0.7873 158.3164


4. Find the image size and create the X and Y based on the coefficients of "p".

sz = size(I);
X = 1:sz(2);
Y = p(1) * X + p(2);

5. Display the result

imshow(I);
hold on;
plot(X,Y);