YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
opencv/main.cpp

An example of using OpenCV with YARP.

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();
yarp::sig::draw::addCircle(yarpImage,yarp::sig::PixelRgb(255,0,0),
yarpImage.width()/2,yarpImage.height()/2,
yarpImage.height()/4);
yarp::sig::draw::addCircle(yarpImage,yarp::sig::PixelRgb(255,50,50),
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;
}
Typed image class.
Definition Image.h:616
size_t width() const
Gets width of image in pixels.
Definition Image.h:163
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:453
void zero()
Set all pixels to 0.
Definition Image.cpp:446
size_t height() const
Gets height of image in pixels.
Definition Image.h:169
::cv::Mat toCvMat(yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
Definition Cv-inl.h:84
void addCircle(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition ImageDraw.h:32
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)