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
26#include <opencv2/imgproc/imgproc.hpp>
27
31
33
34#include <Calibration/Calibration.h>
35
36namespace visionx
37{
41
42 std::string
44 {
45 return "WebCamImageProvider";
46 }
47
48 std::vector<ImageDimension>
50 {
51 std::vector<ImageDimension> supportedVideoResolutions;
52 const ImageDimension CommonResolutions[] = {ImageDimension(320, 240),
53 ImageDimension(640, 480),
54 ImageDimension(800, 600),
55 ImageDimension(768, 576),
56 ImageDimension(1024, 768),
57 ImageDimension(1280, 720),
58 ImageDimension(1280, 960),
59 ImageDimension(1280, 1024),
60 ImageDimension(1600, 1200),
61 ImageDimension(1920, 1080),
62 ImageDimension(1920, 1200),
63 ImageDimension(2560, 1440),
64 ImageDimension(3849, 2160)};
65 int nbTests = sizeof(CommonResolutions) / sizeof(CommonResolutions[0]);
66
67 for (int i = 0; i < nbTests; i++)
68 {
69 ImageDimension test = CommonResolutions[i];
70
71 // try to set resolution
72 camera.set(cv::CAP_PROP_FRAME_WIDTH, test.width);
73 camera.set(cv::CAP_PROP_FRAME_HEIGHT, test.height);
74 camera.set(cv::CAP_PROP_AUTOFOCUS, 0);
75
76 double width = camera.get(cv::CAP_PROP_FRAME_WIDTH);
77 double height = camera.get(cv::CAP_PROP_FRAME_HEIGHT);
78 if (test.width == width && test.height == height)
79 {
80 supportedVideoResolutions.push_back(test);
81 }
82 }
83
84 return supportedVideoResolutions;
85 }
86
87 void
89 {
90 ARMARX_INFO << "Opening camera";
91 capturer.open(getProperty<int>("DeviceNumber").getValue());
93 // CCalibration::LoadCameraParameters();
95 auto dim = getProperty<ImageDimension>("ImageResolution").getValue();
96 if (dim.width == 0)
97 {
98 auto supportedResolutions = getSupportedResolutions(capturer);
99 ARMARX_INFO << "Supported resolutions: " << supportedResolutions;
100 ARMARX_CHECK_POSITIVE(supportedResolutions.size());
101 capturer.set(cv::CAP_PROP_FRAME_WIDTH, supportedResolutions.rbegin()->width);
102 capturer.set(cv::CAP_PROP_FRAME_HEIGHT, supportedResolutions.rbegin()->height);
103 ARMARX_INFO << "Using max resolution found for camera: "
104 << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
105 << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
106 }
107 else
108 {
109 capturer.set(cv::CAP_PROP_FRAME_WIDTH, dim.width);
110 capturer.set(cv::CAP_PROP_FRAME_HEIGHT, dim.height);
111 ARMARX_INFO << "Using resolution " << capturer.get(cv::CAP_PROP_FRAME_WIDTH) << "x"
112 << capturer.get(cv::CAP_PROP_FRAME_HEIGHT);
113 }
114 // setImageFormat(ImageDimension(dim.width, dim.height), visionx::eRgb, visionx::eBayerPatternGr);
115 setImageFormat(ImageDimension(capturer.get(cv::CAP_PROP_FRAME_WIDTH),
116 capturer.get(cv::CAP_PROP_FRAME_HEIGHT)),
117 visionx::eRgb,
118 visionx::eBayerPatternGr);
119 if (getProperty<float>("FPS").isSet())
120 {
121 setImageSyncMode(visionx::eFpsSynchronization);
122 frameRate = getProperty<float>("FPS").getValue();
123 ARMARX_INFO << "Using " << frameRate << " FPS";
124 }
125 else
126 {
127 setImageSyncMode(visionx::eCaptureSynchronization);
128 }
129 }
130
131 void
136
137 bool
139 {
140 // TIMING_START(capture);
141 auto result = capturer.read(image);
142 // TIMING_END(capture);
143 auto tmpSharedMemoryProvider = sharedMemoryProvider;
144 if (result && tmpSharedMemoryProvider)
145 {
146 Ice::Byte* pixels = static_cast<Ice::Byte*>(image.data);
147 // TIMING_START(conversion);
148 updateTimestamp(armarx::TimeUtil::GetTime().toMicroSeconds());
149 cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
150 // TIMING_END(conversion);
151 // TIMING_START(mutex);
153 tmpSharedMemoryProvider->getScopedWriteLock());
154 // TIMING_END(mutex);
155 // TIMING_START(copy);
156 memcpy(ppImageBuffers[0], pixels, image.cols * image.rows * image.channels());
157 // TIMING_END(copy);
158 }
159 // TIMING_END(capture);
160 return result;
161 }
162
163 void
165 {
166 ARMARX_INFO << __FUNCTION__;
167 // capturer.set(CV_CAP_PROP_FPS, framesPerSecond);
168 }
169
170 void
174
181
182 visionx::MonocularCalibration
187
188 std::vector<imrec::ChannelPreferences>
190 {
192
193 imrec::ChannelPreferences cp;
194 cp.requiresLossless = false;
195 cp.name = "rgb";
196 return {cp};
197 }
198} // namespace visionx
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