/
Camera.cpp
132 lines (114 loc) · 3.2 KB
/
Camera.cpp
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
/*
* Camera.cpp
*
* Created on: Jul 12, 2009
* Author: josh
*/
#include "Camera.h"
Camera::Camera(int index,
const char* homographyFile,
const char* inverseFile)
{
if((capture = cvCreateCameraCapture(index)) == NULL)
{
printf("Error create camera capture for camera %d\n", index);
}
loadHomography(homographyFile, inverseFile);
getCameraProperties();
}
Camera::Camera(const char* filename,
const char* homographyFile,
const char* inverseFile)
{
if((capture = cvCreateFileCapture(filename)) == NULL)
{
printf("Error creating file capture for file %s\n", filename);
}
loadHomography(homographyFile, inverseFile);
getCameraProperties();
}
Camera::~Camera()
{
cvReleaseCapture(&capture);
cvReleaseMat(&homography);
}
int Camera::grabFrame()
{
return cvGrabFrame(capture);
}
QImage* Camera::retrieveFrame()
{
IplImage* image = cvRetrieveFrame(capture);
int width = image->width;
int height = image->height;
int pixels = width * height;
uchar* imageData = new unsigned char[4 * pixels];
for(int i = 0; i < pixels; i++)
{
imageData[i * 4 + 3] = 0xFF; // alpha channel
}
uchar* src = (uchar*)(image->imageData);
uchar* srcEnd = src + (3 * pixels);
uchar* dest = imageData;
do
{
memcpy(dest, src, 3);
dest += 4;
src += 3;
}
while(src < srcEnd);
QImage* result = new QImage(imageData, width, height, QImage::Format_RGB32);
return result;
}
Pixel Camera::inViewLatLon(LatLon latlon)
{
Pixel pixel = translateLatLon(latlon);
if(pixel.getX() < 0 || pixel.getX() > cameraWidth
|| pixel.getY() < 0 || pixel.getY() > cameraHeight)
{
pixel.setValid(false);
}
return pixel;
}
QPolygon Camera::translateAreaOfInterest(AreaOfInterest aoi)
{
QPolygon polygon(aoi.numberOfPoints());
int index = 0;
for(LatLon latlon = aoi.begin(); !aoi.end(); latlon = aoi.next(), ++index)
{
Pixel pixel = translateLatLon(latlon);
polygon.setPoint(index, pixel.getX(), pixel.getY());
}
return polygon;
}
void Camera::loadHomography(const char* homographyFile,
const char* inverseFile)
{
if((homography = (CvMat*) cvLoad(homographyFile)) == NULL)
{
printf("Error loading %s\n", homographyFile);
}
if((inverse = (CvMat*) cvLoad(inverseFile)) == NULL)
{
printf("Error loading %s\n", inverseFile);
}
}
void Camera::getCameraProperties()
{
cameraWidth = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
cameraHeight = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
cameraFPS = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
}
Pixel Camera::translateLatLon(LatLon latlon)
{
CvMat* latlonHomogeneous = cvCreateMat(3, 1, CV_32FC1);
cvmSet(latlonHomogeneous, 0, 0, latlon.getLat());
cvmSet(latlonHomogeneous, 1, 0, latlon.getLon());
cvmSet(latlonHomogeneous, 2, 0, 1.0);
CvMat* pixelHomogeneous = cvCreateMat(3, 1, CV_32FC1);
cvMatMul(inverse, latlonHomogeneous, pixelHomogeneous);
int x = (int)(cvmGet(pixelHomogeneous, 0, 0) / cvmGet(pixelHomogeneous, 2, 0));
int y = (int)(cvmGet(pixelHomogeneous, 1, 0) / cvmGet(pixelHomogeneous, 2, 0));
Pixel pixel(x, y);
return pixel;
}