Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/superres/src/frame_source.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
#include "precomp.hpp"
43
44
using namespace cv;
45
using namespace cv::cuda;
46
using namespace cv::superres;
47
using namespace cv::superres::detail;
48
49
cv::superres::FrameSource::~FrameSource()
50
{
51
}
52
53
//////////////////////////////////////////////////////
54
// EmptyFrameSource
55
56
namespace
57
{
58
class EmptyFrameSource : public FrameSource
59
{
60
public:
61
void nextFrame(OutputArray frame) CV_OVERRIDE;
62
void reset() CV_OVERRIDE;
63
};
64
65
void EmptyFrameSource::nextFrame(OutputArray frame)
66
{
67
frame.release();
68
}
69
70
void EmptyFrameSource::reset()
71
{
72
}
73
}
74
75
Ptr<FrameSource> cv::superres::createFrameSource_Empty()
76
{
77
return makePtr<EmptyFrameSource>();
78
}
79
80
//////////////////////////////////////////////////////
81
// VideoFrameSource & CameraFrameSource
82
83
#ifndef HAVE_OPENCV_VIDEOIO
84
85
Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
86
{
87
CV_UNUSED(fileName);
88
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
89
return Ptr<FrameSource>();
90
}
91
92
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
93
{
94
CV_UNUSED(deviceId);
95
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
96
return Ptr<FrameSource>();
97
}
98
99
#else // HAVE_OPENCV_VIDEOIO
100
101
namespace
102
{
103
class CaptureFrameSource : public FrameSource
104
{
105
public:
106
void nextFrame(OutputArray frame) CV_OVERRIDE;
107
108
protected:
109
VideoCapture vc_;
110
111
private:
112
Mat frame_;
113
};
114
115
void CaptureFrameSource::nextFrame(OutputArray _frame)
116
{
117
if (_frame.kind() == _InputArray::MAT)
118
vc_ >> _frame.getMatRef();
119
else if(_frame.kind() == _InputArray::CUDA_GPU_MAT)
120
{
121
vc_ >> frame_;
122
arrCopy(frame_, _frame);
123
}
124
else if (_frame.isUMat())
125
vc_ >> *(UMat *)_frame.getObj();
126
else
127
{
128
// should never get here
129
CV_Error(Error::StsBadArg, "Failed to detect input frame kind" );
130
}
131
}
132
133
class VideoFrameSource : public CaptureFrameSource
134
{
135
public:
136
VideoFrameSource(const String& fileName);
137
138
void reset() CV_OVERRIDE;
139
140
private:
141
String fileName_;
142
};
143
144
VideoFrameSource::VideoFrameSource(const String& fileName) : fileName_(fileName)
145
{
146
reset();
147
}
148
149
void VideoFrameSource::reset()
150
{
151
vc_.release();
152
vc_.open(fileName_);
153
CV_Assert( vc_.isOpened() );
154
}
155
156
class CameraFrameSource : public CaptureFrameSource
157
{
158
public:
159
CameraFrameSource(int deviceId);
160
161
void reset() CV_OVERRIDE;
162
163
private:
164
int deviceId_;
165
};
166
167
CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
168
{
169
reset();
170
}
171
172
void CameraFrameSource::reset()
173
{
174
vc_.release();
175
vc_.open(deviceId_);
176
CV_Assert( vc_.isOpened() );
177
}
178
}
179
180
Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
181
{
182
return makePtr<VideoFrameSource>(fileName);
183
}
184
185
Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
186
{
187
return makePtr<CameraFrameSource>(deviceId);
188
}
189
190
#endif // HAVE_OPENCV_VIDEOIO
191
192
//////////////////////////////////////////////////////
193
// VideoFrameSource_CUDA
194
195
#ifndef HAVE_OPENCV_CUDACODEC
196
197
Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
198
{
199
CV_UNUSED(fileName);
200
CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
201
}
202
203
#else // HAVE_OPENCV_CUDACODEC
204
205
namespace
206
{
207
class VideoFrameSource_CUDA : public FrameSource
208
{
209
public:
210
VideoFrameSource_CUDA(const String& fileName);
211
212
void nextFrame(OutputArray frame);
213
void reset();
214
215
private:
216
String fileName_;
217
Ptr<cudacodec::VideoReader> reader_;
218
GpuMat frame_;
219
};
220
221
VideoFrameSource_CUDA::VideoFrameSource_CUDA(const String& fileName) : fileName_(fileName)
222
{
223
reset();
224
}
225
226
void VideoFrameSource_CUDA::nextFrame(OutputArray _frame)
227
{
228
if (_frame.kind() == _InputArray::CUDA_GPU_MAT)
229
{
230
bool res = reader_->nextFrame(_frame.getGpuMatRef());
231
if (!res)
232
_frame.release();
233
}
234
else
235
{
236
bool res = reader_->nextFrame(frame_);
237
if (!res)
238
_frame.release();
239
else
240
arrCopy(frame_, _frame);
241
}
242
}
243
244
void VideoFrameSource_CUDA::reset()
245
{
246
reader_ = cudacodec::createVideoReader(fileName_);
247
}
248
}
249
250
Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
251
{
252
return makePtr<VideoFrameSource_CUDA>(fileName);
253
}
254
255
#endif // HAVE_OPENCV_CUDACODEC
256
257