YARP
Yet Another Robot Platform
opencv/main.cpp

An example of using OpenCV with YARP.

/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <yarp/sig/Image.h>
#include <yarp/cv/Cv.h>
int main(int argc, char *argv[]) {
std::cout<<"Show a circle for 3 seconds..."<<std::endl;
std::cout<<"Creating a YARP image of a nice circle"<<std::endl;
yarpImage.resize(300,200);
yarpImage.zero();
yarpImage.width()/2,yarpImage.height()/2,
yarpImage.height()/4);
yarpImage.width()/2,yarpImage.height()/2,
yarpImage.height()/5);
yarp::sig::file::write(yarpImage,"test-1.ppm");
std::cout<<"Saving YARP image to test-1.ppm"<<std::endl;
std::cout<<"Showing OpenCV image"<<std::endl;
cv::Mat cvImage1=yarp::cv::toCvMat(yarpImage);
cv::namedWindow("test",1);
cv::imshow("test",cvImage1);
std::cout<<"Taking image back into YARP..."<<std::endl;
cv::Mat cvImage2=cvImage1.clone();
auto yarpReturnImage=yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvImage2);
yarp::sig::file::write(yarpReturnImage,"test-2.ppm");
std::cout<<"Saving YARP image to test-2.ppm"<<std::endl;
cv::waitKey(3000);
cv::destroyWindow("test");
std::cout<<"...done"<<std::endl;
return 0;
}
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition: Image.cpp:452
void zero()
Set all pixels to 0.
Definition: Image.cpp:445
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
::cv::Mat toCvMat(yarp::sig::ImageOf< T > &yarpImage)
Convert a yarp::sig::ImageOf to a cv::Mat object.
void addCircle(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:40
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1099
Packed RGB pixel type.
Definition: Image.h:464
int main(int argc, char *argv[])
Definition: yarpros.cpp:264