I am trying to convert an image loaded in from a GIF via Magick++ into a cv::Mat
. I have already converted from cv::Mat
to Magick::Image
but cannot seem to find how to pull the data out of an Image in Magick in order to load it into a Mat. What's the best way to do this?
For reference, in reverse: Convert cv::Mat to Magick::Image
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);
Complementing Marks fantastic answer (which should be accepted).
cv::Mat
has a constructer for byte arrays.
Mat(int rows,
int cols,
int type,
void* data,
size_t step=AUTO_STEP)
This would require you to allocate a byte array; as opposed to Magick::Image.write
directly to cv::Mat
.
#include <Magick++.h>
#include <opencv2/opencv.hpp>
bool copyImageToMat(Magick::Image & im_image, cv::Mat & cv_image)
{
// Get size of image.
size_t
w = im_image.columns(),
h = im_image.rows();
// Allocate enough bytes for image data.
unsigned char blob[w * h * 3];
// Write image data to blob.
im_image.write(0, 0, w, h, "BGR", Magick::CharPixel, &blob);
// Construct new Mat image.
cv::Mat cv_temp((int)h, (int)w, CV_8UC3, blob);
// Was any work done?
bool dataWasCopied = !cv_temp.empty();
if (dataWasCopied) {
// Copy data to destination.
cv_image = cv_temp.clone();
}
return dataWasCopied;
}
int main(int argc, const char * argv[]) {
cv::Mat destination;
Magick::Image source("rose:");
if(copyImageToMat(source, destination)) {
cv::imwrite("/tmp/rose.png", destination);
}
return 0;
}
来源:https://stackoverflow.com/questions/41841553/convert-magickimage-to-cvmat