WebCamImageProvider.cpp
Go to the documentation of this file.
1#/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "WebCamImageProvider.h"
25
27
28#include <opencv2/imgproc/imgproc.hpp>
29
33
35
36#include <Calibration/Calibration.h>
37
38namespace visionx
39{
43
44 std::string
46 {
47 return "WebCamImageProvider";
48 }
49
50 std::string
55
56 std::vector<ImageDimension>
58 {
59 std::vector<ImageDimension> supportedVideoResolutions;
60 const ImageDimension CommonResolutions[] = {ImageDimension(320, 240),
61 ImageDimension(640, 480),
62 ImageDimension(800, 600),
63 ImageDimension(768, 576),
64 ImageDimension(1024, 768),
65 ImageDimension(1280, 720),
66 ImageDimension(1280, 960),
67 ImageDimension(1280, 1024),
68 ImageDimension(1600, 1200),
69 ImageDimension(1920, 1080),
70 ImageDimension(1920, 1200),
71 ImageDimension(2560, 1440),
72 ImageDimension(3849, 2160)};
73 int nbTests = sizeof(CommonResolutions) / sizeof(CommonResolutions[0]);
74
75 for (int i = 0; i < nbTests; i++)
76 {
77 ImageDimension test = CommonResolutions[i];
78
79 // try to set resolution
80 camera.set(cv::CAP_PROP_FRAME_WIDTH, test.width);
81 camera.set(cv::CAP_PROP_FRAME_HEIGHT, test.height);
82 camera.set(cv::CAP_PROP_AUTOFOCUS, 0);
83
84 double width = camera.get(cv::CAP_PROP_FRAME_WIDTH);
85 double height = camera.get(cv::CAP_PROP_FRAME_HEIGHT);
86 if (test.width == width && test.height == height)
87 {
88 supportedVideoResolutions.push_back(test);
89 }
90 }
91
92 return supportedVideoResolutions;
93 }
94
95 void
97 {
98 ARMARX_INFO << "Opening camera";
99 capturer.open(getProperty<int>("DeviceNumber").getValue());
101 // CCalibration::LoadCameraParameters();
103 auto dim = getProperty<ImageDimension>("ImageResolution").getValue();
104 if (dim.width == 0)
105 {
106 auto supportedResolutions = getSupportedResolutions(capturer);
107 ARMARX_INFO << "Supported resolutions: " << supportedResolutions;
108 ARMARX_CHECK_POSITIVE(supportedResolutions.size());
109 capturer.set(cv::CAP_PROP_FRAME_WIDTH, supportedResolutions.rbegin()->width);
110 capturer.set(cv::CAP_PROP_FRAME_HEIGHT, supportedResolutions.rbegin()->height);
111 ARMARX_INFO << "Using max resolution found for camera: "
112 << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
113 << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
114 }
115 else
116 {
117 capturer.set(cv::CAP_PROP_FRAME_WIDTH, dim.width);
118 capturer.set(cv::CAP_PROP_FRAME_HEIGHT, dim.height);
119 ARMARX_INFO << "Using resolution " << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
120 << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
121 }
122 // setImageFormat(ImageDimension(dim.width, dim.height), visionx::eRgb, visionx::eBayerPatternGr);
123 setImageFormat(ImageDimension(capturer.get(cv::CAP_PROP_FRAME_WIDTH),
124 capturer.get(cv::CAP_PROP_FRAME_HEIGHT)),
125 visionx::eRgb,
126 visionx::eBayerPatternGr);
127 if (getProperty<float>("FPS").isSet())
128 {
129 setImageSyncMode(visionx::eFpsSynchronization);
130 frameRate = getProperty<float>("FPS").getValue();
131 ARMARX_INFO << "Using " << frameRate << " FPS";
132 }
133 else
134 {
135 setImageSyncMode(visionx::eCaptureSynchronization);
136 }
137 }
138
139 void
144
145 bool
147 {
148 // TIMING_START(capture);
149 auto result = capturer.read(image);
150 // TIMING_END(capture);
151 auto tmpSharedMemoryProvider = sharedMemoryProvider;
152 if (result && tmpSharedMemoryProvider)
153 {
154 Ice::Byte* pixels = static_cast<Ice::Byte*>(image.data);
155 // TIMING_START(conversion);
156 updateTimestamp(armarx::TimeUtil::GetTime().toMicroSeconds());
157 cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
158 // TIMING_END(conversion);
159 // TIMING_START(mutex);
161 tmpSharedMemoryProvider->getScopedWriteLock());
162 // TIMING_END(mutex);
163 // TIMING_START(copy);
164 memcpy(ppImageBuffers[0], pixels, image.cols * image.rows * image.channels());
165 // TIMING_END(copy);
166 }
167 // TIMING_END(capture);
168 return result;
169 }
170
171 void
173 {
174 ARMARX_INFO << __FUNCTION__;
175 // capturer.set(CV_CAP_PROP_FPS, framesPerSecond);
176 }
177
178 void
182
189
190 visionx::MonocularCalibration
195
196 std::vector<imrec::ChannelPreferences>
198 {
200
201 imrec::ChannelPreferences cp;
202 cp.requiresLossless = false;
203 cp.name = "rgb";
204 return {cp};
205 }
206
208} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
Throw this exception to indicate missing functionality.
void setImageSyncMode(ImageSyncMode imageSyncMode)
Sets the image synchronization mode.
armarx::IceSharedMemoryProvider< unsignedchar >::pointer_type sharedMemoryProvider
shared memory provider
void updateTimestamp(Ice::Long timestamp, bool threadSafe=true)
Updates the timestamp of the currently captured image.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
std::vector< ImageDimension > getSupportedResolutions(cv::VideoCapture &camera)
MonocularCalibration getCalibration(const Ice::Current &)
void onStartCapture(float framesPerSecond) override
This is called when the image provider capturing has been started.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onStopCapture() override
This is called when the image provider capturing has been stopped.
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
std::vector< imrec::ChannelPreferences > getImageRecordingChannelPreferences(const Ice::Current &) override
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
std::string getDefaultName() const override
Retrieve default name of component.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_POSITIVE(number)
This macro evaluates whether number is positive (> 0) and if it turns out to be false it will throw a...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
ArmarX headers.
#define ARMARX_TRACE
Definition trace.h:77