This is a more difficult problem, please see:
Here are seven points, I want to sort these seven points, and then connect to achieve the following effect:
How does this come true? First write the steps and then the code:
1, first identify the centroid of multiple points
2. Connect and draw points via centroid
Off-topic, about the connection and PIP operation is how to do, this is through the original image of the data modified to do
Directly to the code, as well as the picture saved in the code of the path under the operation of Matlab can make
I=imread (' d:\\ bike test 1.jpg ');% Read Original image%figure (1); Imshow (I);% Show Original imageI_gray=rgb2gray (I);% original image becomes grayscale imageLevel=graythresh (I_gray);% calculates the global threshold for image I_gray, level is a normalized grayscale value with a range of [0 1][Height,width]=size(I_gray);% calculate the length and width of the grayscale imageI_BW=IM2BW (I_gray,level);the%IM2BW uses the threshold level to convert a grayscale image to a two-value image. %figure (2); Imshow (I_BW);% display binary image (background is white) for I=1: Heightinverse color In a percent cycle for J=1: WidthifI_BW (I,J)==1I_BW (I,J)=0;ElseI_BW (I,J)=1;End EndEnd%figure (3); Imshow (I_BW);% shows inverted two value image (black background)[L,num]=bwlabel (I_BW,8);%bwlabel Labels The target object in the binary image I_BW, returns the number of target objects in the identity matrix L and I_BW num,8 represents the number of connections. plot_x=Zeros(1, num);%%zeros (m,n) produces a full 0 matrix of MXN. The horizontal axis used to record the centroid positionplot_y=Zeros(1, num);%zeros (m,n) produces a full 0 matrix of MXN. Ordinate for recording the centroid positionplot_z=Zeros(1, num);%zeros (m,n) produces a full 0 matrix of MXN. Ordinate for recording the centroid positionpoly_x=Zeros(1, num);%%zeros (m,n) produces a full 0 matrix of MXN. The horizontal axis used to record the centroid positionpoly_y=Zeros(1, num);%zeros (m,n) produces a full 0 matrix of MXN. Ordinate for recording the centroid position for I=1: Num plot_z (I) =1;End fork=1: num%%num the centroid position in the order of the Regionssum_x=0; sum_y=0; Area=0;% Initialization for I=1: Height for J=1: WidthifLI,J) ==k sum_x=sum_x+I;% calculates the sum of the horizontal axis of zone Ksum_y=sum_y+J;% calculates the sum of the ordinate of the zone Karea=area+1;% calculates how many coordinate points are represented by the area K End End EndPlot_x (k) =Fix(Sum_x/area);% computed centroid axis of area KPlot_y (k) =Fix(Sum_y/area);% computed centroid ordinate for area KEnd% First stepmax_x =0;p OS =0; for I=1: numifPlot_y (I) >max_x pos =I; max_x = Plot_y (I);EndEndPoly_x (1) = Plot_x (POS); Poly_y (1) = Plot_y (POS);p lot_z (pos) =0;% Second StepMin_y =9999999;p OS =0; for I=1: numifPlot_x (I) <min_y pos =I; min_y = plot_x (I);EndEndPoly_x (3) = Plot_x (POS);p oly_y (3) = Plot_y (POS);p lot_z (pos) =0;% Fourth StepMin_y =9999999999;p OS =0; for I=1: numifPlot_z (I)==1 ifPlot_x (I) <min_y pos =I; min_y = plot_x (I);End EndEndPoly_x (2) = Plot_x (POS);p oly_y (2) = Plot_y (POS);p lot_z (pos) =0;% Fifth StepMin_y =9999999999;p OS =0; for I=1: numifPlot_z (I)==1 ifPlot_x (I) <min_y pos =I; min_y = plot_x (I);End EndEndPoly_x (5) = Plot_x (POS);p oly_y (5) = Plot_y (POS);p lot_z (pos) =0;% Find a third pointMin_y =9999999999;p OS =0; for I=1: numifPlot_z (I)==1 ifPlot_x (I) <min_y pos =I; min_y = plot_x (I);End EndEndPoly_x (4) = Plot_x (POS);p oly_y (4) = Plot_y (POS);p lot_z (pos) =0;% Third Point and fourth point local information resolvedifPoly_y (4) > Poly_y (5) temp_x = poly_x (4); Temp_y = Poly_y (4); Poly_x (4) = Poly_x (5); Poly_y (4) = Poly_y (5); Poly_x (5) = temp_x; Poly_y (5) = Temp_y;End% Sixth Stepmax_x =0;p OS =0; for I=1: numifPlot_z (I)==1 ifPlot_y (I) >max_x pos =I; max_x = Plot_y (I);End EndEndPoly_x (8) = Plot_x (POS);p oly_y (8) = Plot_y (POS);p lot_z (pos) =0;% Seventh StepMin_y =9999999999;p OS =0; for I=1: numifPlot_z (I)==1 ifPlot_x (I) <min_y pos =I; min_y = plot_x (I);End EndEndPoly_x (6) = Plot_x (POS);p oly_y (6) = Plot_y (POS);p lot_z (pos) =0;% Eighth Step for I=1: numifPlot_z (I)==1Poly_x (7) = Plot_x (I); Poly_y (7) = Plot_y (I); Break;EndEnd%figure (4); imshow (I_BW);% shows inverted two-value image (background is black), and the centroid point position is marked on the graph for I=1: Num I ((plot_x (I)- -):(plot_x (I)+ -), (Plot_y (I)- -):(plot_y (I)+ -),1) =Panax Notoginseng;% GreenI ((Plot_x (I)- -):(plot_x (I)+ -), (Plot_y (I)- -):(plot_y (I)+ -),2) = the; I ((Plot_x (I)- -):(plot_x (I)+ -), (Plot_y (I)- -):(plot_y (I)+ -),3) =191;End for I=1: num-1X1 = poly_x (I); Y1 = poly_y (I); x2 = poly_x (I+1); y2 = poly_y (I+1); K = (y2-y1)/(X2-X1);ifx1>x2 t = x1; x1 = x2; x2 = t; temp = y1; y1 = y2; Y2 =temp;End forx = x1:x2 y = k* (x-x1) +y1; y =round(y); for J=1:6*ABS(Fix(k) +1) T =y-5+J; I (X,t,1) =214;%I (x,y,1) = 214;I (X,t,2) = the;%I (x,y,2) =;I (X,t,3) = -;%I (x,y,3) = n; End EndEnd
"matlab": Matlab on the original revision and drawing of related issues