Convert Magick::Image to cv::Mat

为君一笑 提交于 2020-01-02 08:21:28

问题


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


回答1:


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);



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!