Updated Answer
This is the best I can get it, I think!
#include <opencv2/opencv.hpp>
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
using namespace cv;
int main(int argc,char **argv)
{
// Initialise ImageMagick library
InitializeMagick(*argv);
// Create Magick++ Image object and read image file
Image image("image.gif");
// Get dimensions of Magick++ Image
int w=image.columns();
int h=image.rows();
// Make OpenCV Mat of same size with 8-bit and 3 channels
Mat opencvImage(h,w,CV_8UC3);
// Unpack Magick++ pixels into OpenCV Mat structure
image.write(0,0,w,h,"BGR",Magick::CharPixel,opencvImage.data);
// Save opencvImage
imwrite("result.png",opencvImage);
}
For my own future reference, the other Magick++ StorageTypes and my assumed OpenCV equivalents in brackets are:
- Magick::CharPixel (CV_8UC3)
- Magick::ShortPixel (CV_16UC3)
- Magick::IntegerPixel (CV_32SC3)
- Magick::FloatPixel (CV_32FC3)
- Magick::DoublePixel (CV_64FC3)
Previous Answer
This is a work in progress - it works but may not be optimal as I am still learning myself.
#include <opencv2/opencv.hpp>
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
using namespace cv;
int main(int argc,char **argv)
{
// Initialise ImageMagick library
InitializeMagick(*argv);
// Create Magick++ Image object and read image file
Image image("image.gif");
// Get pointer to the Magick++ pixel data in OpenCV "BGR" format
Magick::PixelData pData(image,"BGR",Magick::CharPixel);
// Get dimensions of the Magick++ image
int w=image.columns();
int h=image.rows();
// Make OpenCV Mat of same size with 8-bit and 3 channels
Mat opencvImage(h,w,CV_8UC3);
// Copy Magick++ data into OpenCV Mat
std::memcpy(opencvImage.data,pData.data(),w*h*3);
// Save opencvImage
imwrite("result.png",opencvImage);
}
Actually, Magick++ has the ability to write a buffer of pixels to some memory you have already allocated, which we could do if we declared the Mat
sooner.
It looks like this:
image.write(const ssize_t x_,
const ssize_t y_,
const size_t columns_,
const size_t rows_,
const std::string &map_,
const StorageType type_, void *pixels_)
At the moment, we are temporarily at least, using double memory because we copy the pixel data out of Magick++ into a buffer and from the buffer into the Mat
, so we should maybe do something like this (not yet tested):
// Create Magick++ Image object and read image file
Image image("image.gif");
// Get dimensions of the Magick++ image
int w=image.columns();
int h=image.rows();
// Make OpenCV Mat of same size with 8-bit and 3 channels
Mat opencvImage(h,w,CV_8UC3);
// Write the Magick++ image data into the Mat structure
image.write(const ssize_t x_, # testing this param
const ssize_t y_, # testing this param
const size_t columns_, # testing this param
const size_t rows_, # testing this param
const std::string &map_, # testing this param
Magick::CharPixel, opencvImage.data);