【练习8.2】使用指定标志创建序列cvCreateSeq、在序列中插入元素
题目要求 | 程序代码 | 结果图片 | 要言妙道 | 借鉴参考 |
用CvSeq的函数创建一个圆,这个圆用点序列来表示
1 // OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。 2 // 3 //D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg 4 5 6 #include "stdafx.h" 7 #include<string> 8 #include <cv.h> 9 #include <highgui.h> 10 #include <iostream> 11 #include<math.h> 12 13 #include <opencv2/legacy/legacy.hpp> 14 //#pragma comment(lib, "opencv_legacy2411.lib") 15 16 using namespace cv; 17 using namespace std; 18 19 //函数声明-->--->-->--->-->--->-->--->// 20 21 22 //<--<--<--<--<--<--<--<--<--函数声明// 23 24 int _tmain(int argc, _TCHAR* argv[]) 25 { 26 27 CvMemStorage *sotrage = cvCreateMemStorage(); 28 int flags = CV_SEQ_ELTYPE_POINT | CV_SEQ_FLAG_CLOSED; 29 CvSeq * seq_circle = cvCreateSeq(flags, sizeof(CvSeq), sizeof(CvPoint), sotrage); 30 31 IplImage * image_circle = cvCreateImage(cvSize(201, 201), IPL_DEPTH_8U, 1); 32 cvZero(image_circle); 33 34 int center_x = 100; 35 int center_y = 100; 36 int r = 100; 37 38 int x_temp, y_temp; 39 for (int x = 0; x <= 200; ++x) 40 { 41 for (int y = 0; y <= 200; ++y) 42 { 43 if (x < center_x) 44 { 45 x_temp = center_x - x; 46 } 47 else 48 { 49 x_temp = x - center_x; 50 } 51 52 if (y < center_y) 53 { 54 y_temp = center_y - y; 55 } 56 else 57 { 58 y_temp = y - center_y; 59 } 60 61 if ((pow(x_temp, 2) + pow(y_temp, 2)) == pow(r, 2)) 62 { 63 CvPoint pt; 64 pt.x = x; 65 pt.y = y; 66 cvSeqPush(seq_circle, &pt); 67 //cvSetReal2D(image_circle, pt.x, pt.y, 255); 68 } 69 } 70 } 71 72 for (int i = 0; i < seq_circle->total; ++i) 73 { 74 CvPoint * pt = (CvPoint*)cvGetSeqElem(seq_circle, i); 75 cvSetReal2D(image_circle, pt->x, pt->y, 255); 76 } 77 78 cvNamedWindow("图像", CV_WINDOW_AUTOSIZE); 79 cvShowImage("图像",image_circle); 80 81 cvWaitKey(0); 82 83 cvReleaseImage(&image_circle); 84 cvDestroyAllWindows(); 85 86 return 0; 87 }
‖==========钟于原创 乐于分享 宁静致远 毋忆典藏==========‖