MATLAB: How to save continuous position values in an image

image analysis

I have an image with black back ground and white rectangle. After the top white line indices are read and saved in an array, the vertical line indices will jump from left vertical line to the right one along the same y axis point. I want to save the indices values of the line such that we are tracing the line. Or to save the position indices in a continuous manner. How can i save the values of white pixels in a continous manner an seen in the image? Thanks in advance.

Best Answer

I = imread('testimage_paint.bmp');
I = rgb2gray(I) ;
[y,x] = find(I) ;
% Arrange the ppoints in order
P = [x y]; % coordinates / points
c = mean(P); % mean/ central point
d = P-c ; % vectors connecting the central point and the given points
th = atan2(d(:,2),d(:,1)); % angle above x axis
[th, idx] = sort(th); % sorting the angles
P = P(idx,:); % sorting the given points
P = [P ;P(1,:)]; % add the first at the end to close the polygon
plot( P(:,1), P(:,2), 'r');