43 #include <opencv2/highgui/highgui.hpp>
44 #include <opencv2/imgproc/imgproc.hpp>
46 #if CV_MAJOR_VERSION >= 3
47 #include <opencv2/videoio/videoio.hpp>
82 std::string file = config.
check(
"movie",
Value(
""),
83 "if present, read from specified file rather than camera").asString();
84 fromFile = (file!=
"");
88 m_cap.open(file.c_str());
89 if (!m_cap.isOpened()) {
90 yCError(OPENCVGRABBER,
"Unable to open file '%s' for capture!", file.c_str());
95 m_loop = config.
check(
"loop",
"if present, loop movie");
100 #if CV_MAJOR_VERSION >= 3
102 config.
check(
"camera",
103 Value(cv::VideoCaptureAPIs::CAP_ANY),
104 "if present, read from camera identified by this index").asInt32();
107 config.
check(
"camera",
109 "if present, read from camera identified by this index").asInt32();
112 m_cap.open(camera_idx);
113 if (!m_cap.isOpened()) {
114 yCError(OPENCVGRABBER,
"Unable to open camera for capture!");
119 yCInfo(OPENCVGRABBER,
"Capturing from camera: %d",camera_idx);
122 if ( config.
check(
"framerate",
"if present, specifies desired camera device framerate") ) {
123 double m_fps = config.
check(
"framerate",
Value(-1)).asFloat64();
124 #if CV_MAJOR_VERSION >= 3
125 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FPS, m_fps);
127 m_cap.set(CV_CAP_PROP_FPS, m_fps);
131 if (config.
check(
"flip_x",
"if present, flip the image along the x-axis")) m_flip_x =
true;
132 if (config.
check(
"flip_y",
"if present, flip the image along the y-axis")) m_flip_y =
true;
133 if (config.
check(
"transpose",
"if present, rotate the image along of 90 degrees")) m_transpose =
true;
139 if (config.
check(
"width",
"if present, specifies desired image width")) {
140 m_w = config.
check(
"width",
Value(0)).asInt32();
141 if (!fromFile && m_w>0) {
142 #if CV_MAJOR_VERSION >= 3
143 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH, m_w);
145 m_cap.set(CV_CAP_PROP_FRAME_WIDTH, m_w);
149 #if CV_MAJOR_VERSION >= 3
150 m_w = (size_t)m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
152 m_w = (size_t)m_cap.get(CV_CAP_PROP_FRAME_WIDTH);
156 if (config.
check(
"height",
"if present, specifies desired image height")) {
157 m_h = config.
check(
"height",
Value(0)).asInt32();
158 if (!fromFile && m_h>0) {
159 #if CV_MAJOR_VERSION >= 3
160 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT, m_h);
162 m_cap.set(CV_CAP_PROP_FRAME_HEIGHT, m_h);
166 #if CV_MAJOR_VERSION >= 3
167 m_h = (size_t)m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
169 m_h = (size_t)m_cap.get(CV_CAP_PROP_FRAME_HEIGHT);
175 yCInfo(OPENCVGRABBER,
"OpenCVGrabber opened");
179 m_config.fromString(config.
toString());
216 image.
resize(frame.cols, frame.rows);
219 yCDebug(OPENCVGRABBER,
"Received image of size %zux%zu", image.
width(), image.
height());
224 m_laststamp.update();
228 cv::cvtColor(frame, frame_rgb, cv::COLOR_BGR2RGB);
231 memcpy(image.
getRawImage(), frame_rgb.data,
sizeof(
unsigned char) * frame_rgb.rows * frame_rgb.cols * frame_rgb.channels());
240 if (m_w>0 && m_h>0) {
241 if (image.
width() != m_w || image.
height() != m_h) {
243 yCDebug(OPENCVGRABBER,
"Software scaling from %zux%zu to %zux%zu", image.
width(), image.
height(), m_w, m_h);
246 image.
copy(image, m_w, m_h);
260 if (!m_cap.isOpened()) {
269 if (frame.empty() && m_loop) {
270 bool ok = open(m_config);
271 if (!ok)
return false;
282 cv::transpose(frame, frame);
285 if (m_flip_x && m_flip_y)
287 cv::flip(frame, frame, -1);
291 cv::flip(frame, frame, 0);
295 cv::flip(frame, frame, 1);
298 return sendImage(frame, image);
define common interfaces to discover remote camera capabilities
virtual bool sendImage(const cv::Mat &frame, yarp::sig::ImageOf< yarp::sig::PixelRgb > &image)
Read an image from the grabber.
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
bool close() override
Close a grabber.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Interface implemented by all device drivers.
A factory for creating driver objects of a particular type.
Global factory for devices.
Read a YARP-format image from a device.
A container for a device driver.
A class for storing options and configuration information.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
size_t width() const
Gets width of image in pixels.
unsigned char * getRawImage() const
Access to the internal image buffer.
bool copy(const Image &alt)
Copy operator.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
void zero()
Set all pixels to 0.
size_t height() const
Gets height of image in pixels.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
An interface to the operating system, including Port based communication.