simplAR: Augmented reality for OpenCV beginners
来源:互联网 发布:用最小二乘法处理数据 编辑:程序博客网 时间:2024/04/30 23:16
simplAR: Augmented reality for OpenCV beginners
Here is a simple AR demo for beginners (-includes me). The program augments only in 2D with a picture or a clip.The video plays rather slow, it would be great if you could use multiple threads (-which i have no clue what so ever!).
*Update - Download links fixed!
Download the Project folder and the source code [6.62 Mb].
Links:
http://www.bigbuckbunny.org/
If you want to download only the (modified) video used in this demo, try the link below-
BigBuckBunny_Trailer [6.43 Mb]
640x480, Xvid/LameMP3
Pattern (Size A4,JPEG):
Videos:
http://www.youtube.com/watch?v=rakZ3b0P5rU&feature=player_embedded
Source Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//______________________________________________________________________________________
// OpenCV Simple Augmented Reality Program
// Author: Bharath Prabhuswamy
//______________________________________________________________________________________
//______________________________________________________________________________________
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"
int
main()
{
CvCapture *capture = 0;
IplImage *image = 0;
IplImage *frame = 0;
IplImage *disp,*neg_img,*cpy_img;
int
key = 0;
int
fcount = 0;
int
option = 0;
capture = cvCaptureFromCAM( 0 );
if
( !capture )
return
-1;
//Use a video with aspect ratio 4:3
CvCapture* vid = cvCreateFileCapture(
"trailer.avi"
);
if
( !vid )
return
-1;
IplImage *pic = cvLoadImage(
"pic.jpg"
);
cvFlip(pic,pic,1);
int
b_width = 5;
int
b_height = 4;
int
b_squares = 20;
CvSize b_size = cvSize( b_width, b_height );
//The pattern actually has 6 x 5 squares, but has 5 x 4 = 20 'ENCLOSED' corners
CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
CvPoint2D32f* corners =
new
CvPoint2D32f[ b_squares ];
int
corner_count;
printf
(
"Select an option to run the program\n\n"
);
printf
(
"1. Show an Image over the pattern.\n"
);
printf
(
"2. Play a Clip over the pattern.\n"
);
printf
(
"3. Mark the pattern.\n\n"
);
scanf
(
"%d"
,&option);
//Quit on invalid entry
if
(!(option>=1 && option<=3))
{
printf
(
"Invalid selection."
);
return
-1;
}
cvNamedWindow(
"Video"
,CV_WINDOW_AUTOSIZE);
while
(key!=
'q'
)
{
image = cvQueryFrame( capture );
if
( !image )
break
;
cvFlip(image,image,1);
disp = cvCreateImage( cvGetSize(image), 8, 3 );
cpy_img = cvCreateImage( cvGetSize(image), 8, 3 );
neg_img = cvCreateImage( cvGetSize(image), 8, 3 );
IplImage* gray = cvCreateImage( cvGetSize(image), image->depth, 1);
int
found = cvFindChessboardCorners(image, b_size, corners, &corner_count,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvCvtColor(image, gray, CV_BGR2GRAY);
//This function identifies the pattern from the gray image, saves the valid group of corners
cvFindCornerSubPix(gray, corners, corner_count, cvSize(11,11),cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
if
( corner_count == b_squares )
{
if
(option == 1)
{
CvPoint2D32f p[4];
CvPoint2D32f q[4];
IplImage* blank = cvCreateImage( cvGetSize(pic), 8, 3);
cvZero(blank);
cvNot(blank,blank);
//Set of source points to calculate Perspective matrix
q[0].x= (
float
) pic->width * 0;
q[0].y= (
float
) pic->height * 0;
q[1].x= (
float
) pic->width;
q[1].y= (
float
) pic->height * 0;
q[2].x= (
float
) pic->width;
q[2].y= (
float
) pic->height;
q[3].x= (
float
) pic->width * 0;
q[3].y= (
float
) pic->height;
//Set of destination points to calculate Perspective matrix
p[0].x= corners[0].x;
p[0].y= corners[0].y;
p[1].x= corners[4].x;
p[1].y= corners[4].y;
p[2].x= corners[19].x;
p[2].y= corners[19].y;
p[3].x= corners[15].x;
p[3].y= corners[15].y;
//Calculate Perspective matrix
cvGetPerspectiveTransform(q,p,warp_matrix);
//Boolean juggle to obtain 2D-Augmentation
cvZero(neg_img);
cvZero(cpy_img);
cvWarpPerspective( pic, neg_img, warp_matrix);
cvWarpPerspective( blank, cpy_img, warp_matrix);
cvNot(cpy_img,cpy_img);
cvAnd(cpy_img,image,cpy_img);
cvOr(cpy_img,neg_img,image);
cvShowImage(
"Video"
, image);
}
else
if
(option == 2)
{
CvPoint2D32f p[4];
CvPoint2D32f q[4];
frame = cvQueryFrame(vid);
if
(!frame)
printf
(
"error frame"
);
IplImage* blank = cvCreateImage( cvGetSize(frame), 8, 3);
cvZero(blank);
cvNot(blank,blank);
q[0].x= (
float
) frame->width * 0;
q[0].y= (
float
) frame->height * 0;
q[1].x= (
float
) frame->width;
q[1].y= (
float
) frame->height * 0;
q[2].x= (
float
) frame->width;
q[2].y= (
float
) frame->height;
q[3].x= (
float
) frame->width * 0;
q[3].y= (
float
) frame->height;
p[0].x= corners[0].x;
p[0].y= corners[0].y;
p[1].x= corners[4].x;
p[1].y= corners[4].y;
p[2].x= corners[19].x;
p[2].y= corners[19].y;
p[3].x= corners[15].x;
p[3].y= corners[15].y;
cvGetPerspectiveTransform(q,p,warp_matrix);
//Boolean juggle to obtain 2D-Augmentation
cvZero(neg_img);
cvZero(cpy_img);
cvWarpPerspective( frame, neg_img, warp_matrix);
cvWarpPerspective( blank, cpy_img, warp_matrix);
cvNot(cpy_img,cpy_img);
cvAnd(cpy_img,image,cpy_img);
cvOr(cpy_img,neg_img,image);
cvShowImage(
"Video"
, image);
}
else
{
CvPoint p[4];
p[0].x=(
int
)corners[0].x;
p[0].y=(
int
)corners[0].y;
p[1].x=(
int
)corners[4].x;
p[1].y=(
int
)corners[4].y;
p[2].x=(
int
)corners[19].x;
p[2].y=(
int
)corners[19].y;
p[3].x=(
int
)corners[15].x;
p[3].y=(
int
)corners[15].y;
cvLine( image, p[0], p[1], CV_RGB(255,0,0),2);
cvLine( image, p[1], p[2], CV_RGB(0,255,0),2);
cvLine( image, p[2], p[3], CV_RGB(0,0,255),2);
cvLine( image, p[3], p[0], CV_RGB(255,255,0),2);
//or simply
//cvDrawChessboardCorners(image, b_size, corners, corner_count, found);
cvShowImage(
"Video"
, image);
}
}
else
{
//Show gray image when pattern is not detected
cvFlip(gray,gray);
cvShowImage(
"Video"
, gray );
}
key = cvWaitKey(1);
}
cvDestroyWindow(
"Video"
);
cvReleaseCapture( &vid );
cvReleaseMat(&warp_matrix);
cvReleaseCapture( &capture );
return
0;
}
转载自:http://dsynflo.blogspot.com/2010/06/simplar-augmented-reality-for-opencv.html
- simplAR: Augmented reality for OpenCV beginners
- Augmented Reality Using C# and OpenCV
- Books for Augmented Reality on Android
- Augmented Reality
- Augmented reality
- Top 5 Tools for Augmented Reality in Mobile Apps
- Top 5 Tools for Augmented Reality in Mobile Apps
- Holographic Near-Eye Displays for Virtual and Augmented Reality
- Six Sense, augmented reality
- 增强现实Augmented Reality简介
- Augmented reality with PCL+KinectFusion
- What is Augmented Reality(AR)
- History of Mobile Augmented Reality
- 增强现实(Augmented Reality)浅谈
- AR增强现实 Augmented Reality
- 增强现实(Augmented Reality)浅谈
- augmented reality(AR)入门实例
- AR增强现实 Augmented Reality
- SQL SERVER 2005如何建立自动备份的维护计划
- SSH框架 - Hibernate基础讲解
- 添加第三方地址:
- 智能机销售达饱和 谁是幕后黑手?
- Finagle 一个支持多协议的RPC系统
- simplAR: Augmented reality for OpenCV beginners
- 河水与皮革
- Android 所有Dialog大合集
- SOCKET 封包和拆包
- 游戏开发利器 TexturePacker
- javabean与java类的关系
- HTML5移动Web应用程序的JavaScript 框架
- ubuntu安装无线网卡---tenda311mi
- 4 款类似 Facebook/Path 切换效果的 iOS 组件