MATLAB exercise program (finding convex hull, Graham scanning)

Source: Internet
Author: User

I am not sure how this convex bag will be used in image processing, because it seems to be more about computing ry or graphics. However, as an algorithm, I feel it is necessary to study it. My main reference material is 33.3 of Introduction to algorithms and this blog.

Here, I only write the main process, and the judgment of excessive details is omitted. Here is the search counter-clockwise:

Main. m

Clear all; close all; clc; IMG = ones (256,256 );
Imshow (IMG); [x y] = ginput (); X = round (x); y = round (y); n = length (x); P = []; for I = 1: n img (Y (I)-1: Y (I) + 1, x (I)-1: X (I) + 1) = 0; P = [P; X (I) y (I)]; % endimshow (IMG ); % The following convex hull [t Index] = max (P (:, 2); % locate the index of the largest vertex of y, when there are multiple such points, tmp_p = P (index, :); % finds the largest point of Y tmp_heng = [tmp_p (1) + 30, tmp_p (2)]; % set a point parallel to Y's largest point for I = 1: N %. The angle is not determined here. When the angle is the same, you can determine the distance between the current vertex and P0 vertex. Jiao (I) = multi_jiao (tmp_heng, P (I, :), tmp_p); % calculate the angle between each vertex and Y's largest vertex, and the angle between yourself and yourself is nanendjiao = Jiao '; P = [p Jiao]; P = sortrows (p, 3); % is sorted by the third column, and the third column is the angle degree re {1} = P (n ); % RE is equivalent to stack re {2} = P (1, 1: 2); Re {3} = P (2, 1: 2); Top = 3; % pointer to stack top for I = 3: n-1 while multi (P (I,), Re {top-1}, re {top})> = 0% if it is positive Top = Top-1; end Top = Top + 1; re {top} = P (I ); end % below is the dot link found on the convex bag for I = 2: Top IMG = drawline (IMG, re {I-1} (1), Re {I-1} (2 ), re {I} (1), Re {I} (2); endimg = drawline (IMG, re {1} (1), Re {1} (2 ), re {top} (1), Re {top} (2); figure; imshow (IMG)

The angle of the multi_jiao.m vector, 0-degrees.

Function Re = multi_jiao (P1, P2, P0) % judge the <P10, P20> angle, prepare for sorting x = 1; y = 2; vec1 = p1-p0; vec2 = p2-p0; re = ACOs (dot (vec1, vec2)/(norm (vec1) * norm (vec2) * 180/PI; End

Multi. m cross product to determine the symbol of the returned value

Function Re = multi (P1, P2, P0) % P10, P20 cross product, get positive and negative, the value at the top of the regular stack is not a vertex on the convex bag, if it is negative, x = 1; y = 2; Re = (p1 (x)-P0 (x) * (P2 (y) -P0 (y)-(p1 (y)-P0 (y) * (P2 (x)-P0 (x); End

Drawline. m draw line function, Matlab does not seem to have, do it yourself, full of clothes

Function IMG = drawline (IMG, X1, Y1, X2, Y2) % because the orientation of the image coordinate is opposite to that of the Y axis of the mathematical function, the inverse Number of all Y variables is Y1 =-Y1; y2 =-Y2; k = (y2-y1)/(x2-x1); B = y1-k * x1; MI = min (x1, x2); MA = max (x1, x2 ); for I = mi: Ma IMG (-round (I * k + B), I) = 0; end Mi = min (Y1, Y2); MA = max (Y1, y2); for I = mi: Ma IMG (-I, round (I-B)/K) = 0; endend

The following is a result. The biggest benefit of MATLAB is that the algorithm result is displayed intuitively:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.