【练习8.7】cvGoodFeaturesToTrack确定图像强角点、cvFindCornerSubPix亚像素级角点检测

 

页内索引
题目要求 程序代码 结果图片 要言妙道 借鉴参考

 

 

  

题目要求:

 黑色背景上创建一个白色拐角,使得这个角正好在整数值的坐标上,保存用OpenCV打开

a、找出并输出拐角的确切坐标

分别改变原始图像进行如下操作

b、用白边透明填充的圆将圆角遮住

c、用白边黑色填充的圆将圆角遮住

d、用黑色圆块将圆角遮住

 

程序代码:

 

 1 // OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。
 2 //
 3 //    string file_full_name = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第8章\\r20.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     string file_full_name = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第8章\\拐角.jpg";
27 
28     IplImage * image_source = cvLoadImage(file_full_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
29     CV_Assert(image_source);
30 
31     string file_full_name_2 = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第8章\\拐角2.jpg";
32 
33     IplImage * image_source_2 = cvLoadImage(file_full_name_2.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
34     CV_Assert(image_source_2);
35 
36     IplImage * images[2] = { image_source, image_source_2 };
37 
38     string window_name = "角度图像";
39     for (int i = 0; i < 2; ++i)
40     {
41         window_name = window_name + "";
42         IplImage * image_gray = cvCloneImage(images[i]);
43         IplImage * image_eig = cvCreateImage(cvGetSize(images[i]), IPL_DEPTH_32F, 1);
44         cvZero(image_eig);
45         IplImage* image_temp = cvCloneImage(image_eig);
46 
47         int corner_count = 5;
48         CvPoint2D32f *corners = new CvPoint2D32f[corner_count];
49 
50         cvGoodFeaturesToTrack(image_gray, image_eig, image_temp, corners, &corner_count, 0.1, 10);
51 
52         IplImage * image_corner = cvCloneImage(image_gray);
53         //cvZero(image_corner);
54         cout << "cvGoodFeaturesToTrack:" << endl;
55         for (int i = 0; i < corner_count; ++i)
56         {
57             cout << corners[i].x << "  " << corners[i].y << endl;
58             cvDrawCircle(image_corner, cvPoint(corners[i].x, corners[i].y), 5, cvScalar(255));
59         }
60 
61         cvNamedWindow(window_name.c_str(), CV_WINDOW_AUTOSIZE);
62         cvShowImage(window_name.c_str(), image_corner);
63 
64         int half_win_size = 3;//the window size will be 3+1+3=7  
65         int iteration = 20;
66         double epislon = 0.1;
67 
68         cvFindCornerSubPix(image_gray, corners, corner_count
69             , cvSize(half_win_size, half_win_size), cvSize(-1, -1)
70             , cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, iteration, epislon));
71 
72         IplImage * image_subPix = cvCloneImage(image_gray);
73 
74         cout << "cvFindCornerSubPix:" << endl;
75         for (int i = 0; i < corner_count; ++i)
76         {
77             cout << corners[i].x << "  " << corners[i].y << endl;
78             cvDrawCircle(image_subPix, cvPoint(corners[i].x, corners[i].y), 5, cvScalar(255));
79         }
80 
81         window_name = window_name + "";
82         cvShowImage(window_name.c_str(), image_subPix);
83 
84         cvReleaseImage(&image_gray);
85         cvReleaseImage(&image_eig);
86         cvReleaseImage(&image_temp);
87         cvReleaseImage(&image_corner);
88         cvReleaseImage(&image_subPix);
89     }
90 
91     cvWaitKey(0);
92 
93     cvReleaseImage(&image_source);
94     cvReleaseImage(&image_source_2);
95     cvDestroyAllWindows();
96 
97     return 0;
98 }

 

 

结果图片:

 

 

要言妙道:

  

 

借鉴参考:

 

posted on 2015-05-20 21:18  毋忆典藏  阅读(1046)  评论(0编辑  收藏  举报