# 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 );}