[Opencv learning] convex hull Rendering

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

 

Two-dimensional convex hull Problem description:
Finding two-dimensional convex hull is one of the classic issues in computational geometry.
Given some points on the plane, find a minimum point set and connect them to a convex polygon.
Points are within the polygon or on the polygon. This polygon is a two-dimensional convex hull for a fixed point.
The trigger algorithm (the three-Coins algorithm ). The trigger algorithm was proposed by Sklansky in 1972. We can use three coins to simulate this algorithm.
To solve the problem of convex hull, you need to understand the order of points and left turn decision.
Point sorting steps:
1. Find a point that must be on the convex hull. This is obviously easy. Generally, the point with the smallest horizontal or vertical coordinates is extremely P0.
2. link P0 with other points to calculate the angle between these line segments and the vertical downward direction (that is, the three quadrant split line, the other end of each line segment (P0 at one end) is marked as P1, P2, P3 ......
Sorted
Left turn judgment:
This is a classic computational geometric problem. Determine whether the vector p1 = (x1, Y1) to p2 = (X2, Y2) is left.
Turn, only need to judge X1 * y2-x2 * Y1 positive and negative, if the result is positive, from P1 to P2 do left. If you are interested in the proof of this conclusion, please refer to the relevant books.
Now let's take a look at the three-coin algorithm of skaransk:
1. Preprocessing: sorts each point.
2. Place a coin on P0, P1, and P2 respectively;
Name the three coins as "Rear", "Middle", and "Front" respectively ".
3. Repetition
If three coins are left in the "back-middle-Front" order"
Pick up the "back" and put it in front of the "front;
Rename the original "Post" as "Front ";
Rename the original "Front" to "Middle ";
Rename the original "Middle" to "Rear ";
Otherwise
Pick up "medium" and put it behind "Rear;
Remove the point where "medium" is located;
Rename the original "Middle" to "Rear ";
Rename the original "Post" to "medium ";
Until "Front" is placed on P0, and three coins "Turn left"
4. Follow the remaining points of the link according to the number size (the point with the largest number is connected back to P0 ),
The polygon is the convex hull of the fixed point set.
Draw a few points on the paper and use three coins to simulate the execution process of the algorithm. It seems that the result is always correct. However, please do not try to prove its correctness, because, in fact, it is not correct *_*. Although this algorithm is not correct, there are still many examples for reference, and many convex packets later. Algorithms penetrate into some of the meanings of the skuransk algorithm. Therefore, it is very beneficial to understand this algorithm. In 1978, bykat determined that this algorithm was incorrect. Therefore, if you are interested in this algorithm, refer to bykat,. convex hull of a finite set of points in two dimensions 296th-298.
The Sklansky algorithm is used in the legend of the convex hull function created in opencv. However, its correctness remains to be investigated... The following is an example:

# Include "CV. H "# include" highgui. H "# include <stdlib. h> # define array 1/* switch between array/Sequence Method by replacing 0 <=> 1 */INT main (INT argc, char ** argv) {iplimage * IMG = cvcreateimage (cvsize (500,500), 8, 3); cvnamedwindow ("hull", 1); # If! Array cvmemstorage * storage = cvcreatememstorage (); # endif for (;) {int I, Count = rand () % 100 + 1, hullcount; cvpoint pt0; # If! Array cvseq * ptseq = cvcreateseq (cv_seq_kind_generic | cv_32sc2, sizeof (cvcontour), sizeof (cvpoint), storage); cvseq * hull; // obtain random vertex for (I = 0; I <count; I ++) {pt0.x = rand () % (IMG-> width/2) + IMG-> height/4; pt0.y = rand () % (IMG-> height/2) + IMG-> width/4; cvseqpush (ptseq, & pt0);} hull = cvconvexhull2 (ptseq, 0, cv_clockwise, 0 ); // clockwise hullcount = hull-> total; # else cvpoint * points = (cvpoint *) malloc (count * sizeo F (points [0]); int * hull = (int *) malloc (count * sizeof (Hull [0]); cvmat point_mat = cvmat (1, count, cv_32sc2, points); cvmat hull_mat = cvmat (1, Count, cv_32sc1, hull); // random dot for (I = 0; I <count; I ++) {pt0.x = rand () % (IMG-> width/2) + IMG-> height/4; pt0.y = rand () % (IMG-> height/2) + IMG-> width/4; points [I] = pt0;} cvconvexhull2 (& point_mat, & hull_mat, cv_clockwise, 0); hullcount = hull_mat.cols; // note that these regions are different from the sequential processing methods. Party # endif cvzero (IMG); // clear the IMG and prepare to draw a new image. // The image point for (I = 0; I <count; I ++) {# If! Array pt0 = * cv_get_seq_elem (cvpoint, ptseq, I); # else pt0 = points [I]; # endif cvcircle (IMG, pt0, 2, cv_rgb (255, 0, 0 ), cv_filled);} // determine an endpoint # If! Array pt0 = ** cv_get_seq_elem (cvpoint *, Hull, hullcount-1); # else pt0 = points [Hull [hullcount-1]; # endif for (I = 0; I 

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

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.