29 #include <opencv2/highgui/highgui.hpp>
30 #include <opencv2/imgproc/imgproc.hpp>
31 #include <opencv2/videoio/videoio.hpp>
65 std::string file = config.
check(
"movie",
Value(
""),
66 "if present, read from specified file rather than camera").asString();
67 fromFile = (file!=
"");
71 m_cap.open(file.c_str());
72 if (!m_cap.isOpened()) {
73 yCError(OPENCVGRABBER,
"Unable to open file '%s' for capture!", file.c_str());
78 m_loop = config.
check(
"loop",
"if present, loop movie");
84 config.
check(
"camera",
85 Value(cv::VideoCaptureAPIs::CAP_ANY),
86 "if present, read from camera identified by this index").asInt32();
88 m_cap.open(camera_idx);
89 if (!m_cap.isOpened()) {
90 yCError(OPENCVGRABBER,
"Unable to open camera for capture!");
95 yCInfo(OPENCVGRABBER,
"Capturing from camera: %d",camera_idx);
98 if ( config.
check(
"framerate",
"if present, specifies desired camera device framerate") ) {
99 double m_fps = config.
check(
"framerate",
Value(-1)).asFloat64();
100 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FPS, m_fps);
104 if (config.
check(
"flip_x",
"if present, flip the image along the x-axis")) {
108 if (config.
check(
"flip_y",
"if present, flip the image along the y-axis")) {
112 if (config.
check(
"transpose",
"if present, rotate the image along of 90 degrees")) {
118 if (config.
check(
"width",
"if present, specifies desired image width")) {
119 m_w = config.
check(
"width",
Value(0)).asInt32();
120 if (!fromFile && m_w>0) {
121 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH, m_w);
124 m_w = m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
127 if (config.
check(
"height",
"if present, specifies desired image height")) {
128 m_h = config.
check(
"height",
Value(0)).asInt32();
129 if (!fromFile && m_h>0) {
130 m_cap.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT, m_h);
133 m_h = m_cap.get(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
138 yCInfo(OPENCVGRABBER,
"OpenCVGrabber opened");
142 m_config.fromString(config.
toString());
177 if (!m_cap.isOpened()) {
183 if (
static_cast<int>(image.
width()) != m_w ||
static_cast<int>(image.
height()) != m_h) {
191 if (bgr.empty() && m_loop) {
192 bool ok = open(m_config);
208 cv::cvtColor(bgr, frame, cv::COLOR_BGR2RGB);
212 cv::transpose(frame, transposed);
213 image.
resize(transposed.cols, transposed.rows);
215 transposed.copyTo(frame);
218 if (m_flip_x && m_flip_y) {
219 cv::flip(frame, frame, -1);
220 }
else if (m_flip_x) {
221 cv::flip(frame, frame, 0);
222 }
else if (m_flip_y) {
223 cv::flip(frame, frame, 1);
227 yCDebug(OPENCVGRABBER,
"Received image of size %dx%d", frame.cols, frame.rows);
232 m_laststamp.update();
242 if (fromFile && (frame.cols != (!m_transpose ? m_w : m_h) || frame.rows != (!m_transpose ? m_h : m_w))) {
244 yCDebug(OPENCVGRABBER,
"Software scaling from %dx%d to %dx%d", frame.cols, frame.rows, m_w, m_h);
249 cv::resize(frame, resized, {m_w, m_h});
250 image.
resize(resized.cols, resized.rows);
252 resized.copyTo(frame);
255 yCTrace(OPENCVGRABBER,
"%d by %d image", frame.cols, frame.rows);
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Read an image from the grabber.
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.
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.
size_t getRowSize() const
Size of the underlying image buffer rows.
unsigned char * getRawImage() const
Access to the internal image buffer.
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.
IFrameGrabberOf< yarp::sig::ImageOf< yarp::sig::PixelRgb > > IFrameGrabberImage
An interface to the operating system, including Port based communication.