[learning opencv]第十章:精確檢測角點函數cvFindCornerSubPix的demo

cvFindCornerSubPix利用cvGoodFeaturesToTrack檢測出來的角點的基礎上,能夠將角點位置精確到亞像素級精度。

cvGoodFeaturesToTrack的使用可以參考這裏

//cvFindCornersSubPix_demo.cpp
//http://blog.csdn.net/moc062066
//2011.07.26

#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>


#pragma comment(lib, "opencv_core220d.lib")
#pragma comment(lib, "opencv_highgui220d.lib")
#pragma comment(lib, "opencv_imgproc220d.lib")



int main(int argc, char** argv)
{
	FILE* fp = fopen("d:\\result.txt","w+" ) ;
	const char* filename = "D:\\mochen_WIN32\\opencv\\CH10\\one_way_train_0001.jpg" ;
	//const char* windowname = "http://blog.csdn.net/moc062066" ;

	//img AND img_copy is the same,use to draw different result 
	IplImage* img = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR );
	IplImage* img_copy = cvCloneImage( img ) ;//a copy of img
	IplImage* img_gray = cvCreateImage(cvGetSize( img ),IPL_DEPTH_8U, 1) ;
	IplImage* eig_image = cvCreateImage(cvGetSize( img ),IPL_DEPTH_32F, 1);
	IplImage* temp_image = cvCloneImage( eig_image ) ;

	cvCvtColor(img,img_gray,CV_BGR2GRAY);

	const int MAX_CORNERS = 1000 ;
	CvPoint2D32f* corners = new CvPoint2D32f[ MAX_CORNERS ];
	int corner_count = MAX_CORNERS;
	double quality_level = 0.1; //OR 0.01
	double min_distance = 5;

	cvGoodFeaturesToTrack(
		img_gray,
		eig_image,         
		temp_image,
		corners,
		&corner_count,
		quality_level,
		min_distance
	);

	//draw corners from cvGoodFeaturesToTrack on "img"
	for (int i = 0 ; i < corner_count ; ++i){
		cvLine( 
			img,
			cvPoint(corners[i].x,corners[i].y) ,
			cvPoint(corners[i].x,corners[i].y),
			CV_RGB(255,0,0),
			5
		);
		fprintf(fp,"\t%f\t%f\n",corners[i].x,corners[i].y) ;
	}
	
	fprintf(fp,"\n\n\n\n\n") ;

	int half_win_size = 3;//the window size will be 3+1+3=7
	int iteration = 20;
	double epislon = 0.1;

	cvFindCornerSubPix(
		img_gray,
		corners,
		corner_count,
		cvSize(half_win_size,half_win_size),
		cvSize(-1,-1),//no ignoring the neighbours of the center corner
		cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,iteration,epislon)
		);

	//draw subpix corners on "img_copy"
	for (int i=0;i<corner_count;i++){
		cvLine( 
			img_copy,
			cvPoint(corners[i].x,corners[i].y) ,
			cvPoint(corners[i].x,corners[i].y),
			CV_RGB(255,0,0),
			5
		);
		fprintf(fp,"\t%f\t%f\n",corners[i].x,corners[i].y) ;
	}

	cvNamedWindow("cvFindCornerSubPix", CV_WINDOW_AUTOSIZE );
	cvShowImage( "cvFindCornerSubPix", img );
	cvNamedWindow("cvGoodFeaturesToTrack", CV_WINDOW_AUTOSIZE );
	cvShowImage( "cvGoodFeaturesToTrack", img_copy );


	cvWaitKey(0);

	cvReleaseImage(&img_gray);
	cvReleaseImage(&img);
	cvReleaseImage(&img_copy);
	cvDestroyWindow("cvGoodFeaturesToTrack");
	cvDestroyWindow("cvFindCornerSubPix");

	return 0;
}



結果:




還可以看看具體的位置差別:

226	177		223.979004	177.954727
246	457		246.342575	458.441925
206	217		205.988937	217.017212
63	233		61.92979	231.779572
59	239		57.358231	237.760956
476	127		476.999207	126.51577
244	230		244.378113	231.000763
329	261		328.071289	261.544617
41	253		39.679108	251.516586
449	206		450.561768	205.385559
298	116		297.48111	115.146233
57	249		58.309151	250.528427
65	259		63.784649	257.482605
56	233		57.358143	231.902939
480	265		481.90155	264.144104
40	266		39.356575	265.407654
282	151		280.037598	152.058228
37	247		37.105995	245.344711
391	103		393.433319	101.992729
242	141		242.607422	141.100433
186	258		187.336563	257.453247
374	279		373.053436	277.157898
55	259		53.271538	257.877075
69	242		69.2658	244.048813
260	67		259.071808	67.505707
50	252		49.351063	250.887909
356	323		355.215271	323.20282
37	260		37.986225	258.311005
336	127		337.292725	126.324081
320	163		320.452179	163.841644
305	203		303.238556	204.069641
51	267		49.322056	265.451752
46	246		44.387882	244.464386
423	150		421.249329	150.734589
64	245		62.19445	243.746231
50	239		48.513313	238.295792
365	60		366.504028	59.652817
189	209		187.248123	209.612976
60	266		59.270794	265.092224
262	191		262.622223	190.768219
288	5		287.563843	4.65015
70	252		69.444695	249.886948
421	293		419.476837	293.755798
389	233		389.720612	233.096863
510	178		508.495789	178.653564
513	327		513.642883	326.605164
285	246		285.041473	245.707077
42	243		44.218742	244.554565
74	271		74.592758	271.931274
265	290		266.595428	289.260986
45	259		43.965206	258.221985
445	81		446.234833	80.587486
262	105		260.089722	105.417213
38	238		39.463673	239.081985
179	477		178.42366	478.361267
294	349		292.825348	349.096405
291	43		291.821869	42.637604
277	72		277.381744	72.414444
466	309		466.518738	310.26712
310	306		309.965088	305.939087
361	178		362.592712	177.057404
69	265		68.396118	264.394745
65	271		64.174408	272.556793
226	274		225.409454	272.912567
346	219		345.795349	218.329971
464	163		464.552094	164.572495
70	237		70.615662	236.817734
74	298		74.548515	296.672241
402	340		402.264862	340.515778
435	247		435.498627	248.511307
68	306		68.562714	305.116577
528	279		528.224304	279.99704
60	256		58.378197	257.673218
451	357		449.710205	357.786896
226	134		224.146622	134.863815
406	190		406.038696	190.735214
312	82		313.823181	81.432472
337	366		337.767242	366.91449
432	402		432.503906	403.237396
34	235		35.783943	233.398193
72	333		72	333
63	227		62.944881	225.204498
72	303		72.011215	304.445038
497	373		497.65799	374.472382
210	313		208.663559	313.423889
387	385		385.418671	385.524963
39	272		39	272
68	300		68	300
573	294		573.499817	295.692657
68	319		69.112984	318.079987
72	312		72.112968	310.587891
170	295		170.060715	296.296753
378	137		378.369995	137.953888
61	335		60.104778	334.017151
40	228		40.901752	226.912415
66	311		66.144821	311.928802
434	112		434.703094	113.567688
353	92		352.966858	91.11277
53	327		53.927685	328.156189
56	227		57.43726	225.764465
72	323		74.141632	323.789276
277	424		277	424
58	307		57.944839	307.864197
58	329		58.423988	327.466278
294	458		295.407806	459.466583
50	259		49.397194	258.418549
60	302		60	302
47	234		47.988354	232.771988
330	52		329.634888	50.872742
48	227		50.580132	226.629684
293	429		291.427155	427.723633
61	319		60.054024	320.291595
279	6		279.202332	4.682604
55	264		54.053097	265.40564
55	243		53.70647	243.994568
152	288		151.368958	288.24527
70	338		68.620132	339.708984
561	154		562.112	153.836777
298	467		296.016693	465.889954
129	144		130.049789	143.166763
542	237		541.159363	235.351898
248	329		248.618698	330.610992
559	343		559.828735	342.710571
169	252		168.375534	250.124008
494	221		495.947693	220.213699
528	105		528.884766	104.912086
73	318		73.327011	317.454468
145	477		145	477
48	272		49.243771	273.165466
58	343		59.737438	341.331635
552	192		552.329285	193.175949
587	250		585.283508	251.023361
521	139		519.62085	139.533493
188	376		188	376
161	477		160.223663	478.555725
286	438		287.129913	439.437408
528	435		528.64801	436.565552
53	304		53	304
64	327		64.307709	326.131226
206	173		205.530167	172.02626
53	321		52.927059	322.150146
290	453		289.464203	451.641998
283	425		284.591217	425.801331
59	272		59.303421	272.722107
2	195		0.55171	194.967331
594	209		595.181274	208.285736
50	309		50.056606	310.088776
594	301		594.44751	302.387329
110	465		112.678375	464.216278
105	301		105.904839	299.43158
58	313		59.005356	313.973969
580	351		581.15625	350.483337
65	347		64.425552	348.0896
284	368		285.773041	367.813232
242	102		241.573944	100.385773
543	391		544.625549	390.523132
54	272		53.65321	272.630188
120	477		120	477
65	339		63.423409	340.927551
489	94		488.384949	92.754433
65	332		63.918072	333.602203
26	266		25.558767	264.518463
291	401		289.508911	400.552765
141	433		141	433
161	434		161.566635	432.378479
143	427		144.209717	424.16922
144	453		146.34552	450.987305
50	315		49.960327	316.33371
65	207		66.250748	207.832001
158	407		158	407
282	461		283.567688	460.905243
53	337		54.0564	334.985413
638	291		638.365051	291.364532
34	477		32.855476	478.484039
162	413		162	413
274	40		274.129211	38.520088
41	277		41	277
407	71		406.617126	70.229546
95	477		95.162102	478.559387
133	477		133	477
225	390		226.661194	389.679596
603	169		603.631226	168.46019
146	467		144.850479	464.238312
174	383		174.595169	382.531189
112	325		111.867439	326.299652
194	388		194	388
58	348		58.941166	349.243073
156	453		157.918381	454.864227
615	213		615.423035	214.50383
178	398		178.002335	397.914215
116	473		117.430855	470.287781
47	329		46.515869	329.850739
44	307		44.659878	305.491821
160	457		157.926819	459.669403
148	415		150.416962	417.023529
145	290		143.436157	290.277985
109	474		109	474
118	447		117.039696	446.002502
139	469		139	469
138	445		140.743973	444.947571
160	310		159.202484	309.343964
153	467		152.075958	468.852844
137	455		134.443466	455.724915
484	420		482.420044	421.416656
143	448		140.511902	445.754517
622	176		622.9776	174.712143
154	448		154	448
150	306		151.808548	305.681183
30	277		30.737091	279.031219
136	439		136	439
165	449		162.291138	450.282227
191	395		191.669235	392.0336
2	229		0.359053	229.453705
95	306		93.769638	306.755463
303	477		303.275635	478.525665
176	389		176	389
140	477		139.243103	478.506836
149	455		149	455
97	438		95.075996	438.308624
638	285		638.268433	284.668304
292	477		292	477
188	401		185.619461	400.684967
70	227		70	227
126	419		126	419
143	438		143	438
610	133		611.076111	132.721558
172	440		172	440
24	247		24.90378	247.919006
158	419		158	419
566	397		566.452209	398.134277
121	410		119.065735	410.2771
33	252		33.511356	251.844849
180	381		178.197098	379.613708
126	452		126	452
183	393		182.582108	392.361359
131	437		131	437
151	420		150.32756	417.153473
128	477		126.419952	478.534943
138	429		138	429
166	394		166.871658	395.072937
217	367		217	367
172	394		170.262985	391.322693
133	451		134.923233	449.34375
135	298		136.869858	298.207581
131	446		131	446
113	454		111.401466	453.746399
142	399		143.115082	397.244324
144	462		145.246246	464.354553
132	462		130.169952	459.457245
182	420		182.27916	419.063446
170	388		170	388
8	255		8.042757	256.647827
605	259		605.310486	257.383911
102	468		100.057426	467.894165
76	155		74.387268	155.617752
2	174		0.527431	173.877594
185	427		185	427
161	463		161	463
280	28		280	28
118	461		116.281197	461.59375
198	393		198	393
140	362		140.18956	361.06958
77	345		75.254028	346.954712
45	323		46.802341	323.090698
156	414		154.80864	411.225403
107	461		105.771194	460.097656
115	467		115	467
107	434		107	434
572	121		570.835999	119.155617
153	478		151.811005	478.576996
122	466		124.632881	466.831177
173	445		171.83699	442.292023
131	406		132.519928	403.895569
207	394		208.327072	392.170074
124	440		122.428673	439.951691
176	327		177.730301	326.282715
176	403		174.62149	400.221405
83	453		82.17395	452.288971
194	407		192.251038	405.181671
144	303		144.42923	301.771515
173	399		173	399
88	458		88	458
128	459		130.425415	458.998322
78	460		76.168289	459.321838
182	401		184.579971	400.85202
5	242		3.816916	243.952225
169	411		169	411
302	442		302	442
82	464		82	464
128	402		127.028763	401.395264
109	423		108.082031	422.247162
115	417		113.579552	416.254883
42	331		42	331
167	400		167	400
201	397		201	397
90	446		88.533951	445.079132
30	304		28.948282	305.937805
121	451		123.440361	448.574249
104	429		102.435143	428.326569
118	421		118	421
36	277		36	277
52	344		54.155262	342.375519
119	221		119	221
155	473		155	473
22	254		22	254
70	469		68.035934	469.233368
172	477		169.329987	478.596527
151	460		153.50676	461.944519
48	337		50.156414	335.978241
215	384		215.199768	385.493744
181	387		181	387
106	443		106	443
278	419		276.92392	419.238892
42	319		42	319
149	447		150.125107	449.818298
190	421		190	421
228	360		230.534348	361.441376
6	247		4.255842	245.215683
185	406		185	406
95	331		95.585266	329.267426
124	471		124	471
46	344		46.258667	343.305542
139	460		139	460
93	450		93	450
184	440		184	440
153	409		154.891968	411.116425
20	235		20	235
139	420		138.211136	420.432587
82	477		80.268921	478.45166
102	448		102	448
24	275		24	275
276	457		275.921661	457.222351
192	477		192	477
634	135		634	135
212	390		212	390
314	477		314.638733	478.578766
179	437		180.079559	436.389618
307	405		308.8909	405.719025
75	340		74.390884	338.975739
150	363		148.123001	363.714539
324	448		325.662994	448.72998
113	427		113	427
133	467		131.354553	464.375366
195	415		195.793442	414.296265
555	449		554.767761	450.06488
53	288		52.229633	289.30661
126	243		124.381767	242.674225
306	446		306	446
302	464		303.366913	465.364105
50	349		50.666424	350.814087
301	437		299.912231	435.25946
21	229		18.846128	227.009109
593	335		592.243591	335.971527
41	314		41	314
157	400		154.883865	400.263031
303	470		305.244904	471.695343
22	269		22	269
22	240		22	240
132	396		130.897324	395.369934
81	243		78.946297	241.783127
297	477		297	477
288	445		288.517487	444.895172
299	432		299	432
168	320		169.037354	318.091766
284	374		284	374
122	457		121.275467	454.710907
88	298		88	298
171	316		169.500443	317.163361
130	415		130	415
225	340		225	340
84	254		83.076866	256.995026
103	145		103	145
221	361		221	361


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章