Opencv-point polygon Test

Source: Internet
Author: User
# Include "opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include <iostream> # include <stdio. h> # include <stdlib. h> using namespace CV; using namespace STD; int main (INT argc, char ** argv) {const int r = 100; MAT src = mat :: zeros (SIZE (4 * r, 4 * r), cv_8uc1); // create a sequence of points to make a contour: vector <point2f> Vert (6 ); vert [0] = point (1.5 * r, 1.34 * r); vert [1] = point (1 * r, 2 * R ); vert [2] = point (1.5 * r, 2.866 * r); vert [3] = point (2.5 * r, 2.866 * R ); vert [4] = point (3 * r, 2 * r); vert [5] = point (2.5 * r, 1.34 * R ); // draw it in SRC for (Int J = 0; j <6; j ++) {Line (SRC, vert [J], vert [(j + 1) % 6], scalar (255), 3, 8);} // get the second vector <point> contours; vector <vec4i> hierarchy; MAT src_copy = SRC. clone (); findcontours (src_copy, contours, hierarchy, retr_tree, chain_approx_simple); // calculate the distances to the contour mat raw_dist (SRC. size (), cv_32fc1); For (Int J = 0; j <SRC. rows; j ++) {for (INT I = 0; I <SRC. cols; I ++) {raw_dist.at <float> (J, I) = pointpolygontest (contours [0], point2f (I, j), true) ;}} double minval; double maxval; minmaxloc (raw_dist, & minval, & maxval, 0, 0, MAT (); minval = ABS (minval); maxval = ABS (maxval ); // depicting the distances graphically mat drawing = mat: zeros (SRC. size (), cv_8uc3); For (Int J = 0; j <SRC. rows; j ++) {for (INT I = 0; I <SRC. cols; I ++) {If (raw_dist.at <float> (J, I) <0) {drawing. at <vec3b> (J, I) [0] = 255-(INT) ABS (raw_dist.at <float> (J, I) * 255/minval ;} else if (raw_dist.at <float> (J, I)> 0) {drawing. at <vec3b> (J, I) [2] = 255-(INT) raw_dist.at <float> (J, I) * 255/maxval;} else {drawing. at <vec3b> (J, I) [0] = 255; drawing. at <vec3b> (J, I) [1] = 255; drawing. at <vec3b> (J, I) [2] = 255 ;}}// create window and show your results char * source_window = "Source"; namedwindow (source_window, cv_window_autosize); imshow (source_window, Src); namedwindow ("distance", cv_window_autosize); imshow ("distance", drawing); waitkey (0); Return (0 );}

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.