Permalink
Browse files

add CImage::setFromMatNoCopy() method

  • Loading branch information...
jlblancoc committed Oct 30, 2018
1 parent 6140e02 commit 639d6fdc52b142715df75399d8badea531391278
@@ -228,12 +228,7 @@ bool CImageGrabber_OpenCV::getObservation(
{
// Fill the output class:
out_observation.timestamp = mrpt::system::now();
// don't free the img memory, since we are transfering ownership
// to mrpt's CImage:
capImg.addref();
// Create a new IplImage structure:
auto ipl = new IplImage(capImg);
out_observation.image.setFromIplImageReadOnly(ipl);
out_observation.image.setFromMatNoCopy(capImg);
return true;
}
cerr << "[CImageGrabber_OpenCV] WARNING: Ignoring error #" << nTries + 1
@@ -128,10 +128,11 @@ class CExceptionExternalImageNotFound : public std::runtime_error
* \code
* CImage::Ptr myImg::Ptr = CImage::Ptr( new CImage(...) );
* \endcode
* - To set a CImage from an OpenCV "IPLImage*", use the methods:
* - To set a CImage from an OpenCV `IplImage*` or `cv::Mat`, use:
* - CImage::loadFromIplImage
* - CImage::setFromIplImage
* - CImage::CImage(void *IPL)
* - CImage::setFromMatNoCopy(cv::Mat &i)
*
* Some functions are implemented in MRPT with highly optimized SSE2/SSE3
*routines, in suitable platforms and compilers. To
@@ -231,6 +232,7 @@ class CImage : public mrpt::serialization::CSerializable, public CCanvas
CImage(const IplImage* iplImage);

/** Constructor from an cv::Mat, making a copy of the image.
* \sa setFromMatNoCopy()
*/
CImage(const cv::Mat& mat);
#endif
@@ -905,9 +907,14 @@ class CImage : public mrpt::serialization::CSerializable, public CCanvas
* IPLImage will NOT be released/freed at this object destructor.
* This method provides a fast method to grab images from a camera
* without making a copy of every frame.
* \sa setFromImageReadOnly
* \sa setFromImageReadOnly, setFromMatNoCopy
*/
void setFromIplImageReadOnly(IplImage* iplImage);

/** Set the image from a cv::Mat image, transfering owneership to this
* CImage object, i.e. it calls the cv::Mat::addref() method.
* \sa setFromIplImageReadOnly */
void setFromMatNoCopy(cv::Mat& img);
#endif

/** Sets the internal IplImage pointer to that of another given image,
@@ -56,6 +56,7 @@
#endif
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgcodecs.hpp>
#endif

#if defined(__cplusplus)
@@ -369,20 +369,14 @@ bool CImage::saveToFile(const std::string& fileName, int jpeg_quality) const
makeSureImageIsLoaded(); // For delayed loaded images stored externally
ASSERT_(img != nullptr);

int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = jpeg_quality;
p[2] = 0;
return (0 != cvSaveImage(fileName.c_str(), img, p));
const std::vector<int> params={CV_IMWRITE_JPEG_QUALITY, jpeg_quality};
return cv::imwrite(fileName, cv::cvarrToMat(img), params);
#else
THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
#endif
MRPT_END
}

/*---------------------------------------------------------------
loadFromIplImage
---------------------------------------------------------------*/
#if MRPT_HAS_OPENCV
void CImage::loadFromIplImage(const IplImage* iplImage)
{
@@ -395,18 +389,28 @@ void CImage::loadFromIplImage(const IplImage* iplImage)
}
MRPT_END
}
#endif

/*---------------------------------------------------------------
setFromIplImageReadOnly
---------------------------------------------------------------*/
#if MRPT_HAS_OPENCV
void CImage::setFromIplImageReadOnly(IplImage* iplImage)
void CImage::setFromMatNoCopy(cv::Mat& newimg)
{
MRPT_START
releaseIpl();
ASSERT_(iplImage != nullptr);

// don't free the img memory, since we are transfering ownership:
newimg.addref();
// Create a new IplImage structure:
img = new IplImage(newimg);

m_imgIsReadOnly = true;
m_imgIsExternalStorage = false;
MRPT_END
}

void CImage::setFromIplImageReadOnly(IplImage* iplImage)
{
MRPT_START
ASSERTMSG_(iplImage != this->img, "Trying to assign read-only to itself.");
ASSERT_(iplImage != nullptr);
releaseIpl();

img = iplImage;
m_imgIsReadOnly = true;

0 comments on commit 639d6fd

Please sign in to comment.