将Magick :: Image转换为cv :: Mat

时间:2017-01-25 00:43:17

标签: c++ opencv imagemagick magick++

我正在尝试将通过Magick ++从GIF加载的图像转换为cv::Mat。我已经从cv::Mat转换为Magick::Image但似乎无法找到如何从Magick中的Image中提取数据以将其加载到Mat中。最好的方法是什么?

供参考,反过来:Convert cv::Mat to Magick::Image

2 个答案:

答案 0 :(得分:7)

更新了答案

我认为这是我能得到的最好的结果!

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

对于我自己将来的参考,括号中的其他Magick ++ StorageTypes和我假定的OpenCV等价物是:

  • Magick :: CharPixel(CV_8UC3)
  • Magick :: ShortPixel(CV_16UC3)
  • Magick :: IntegerPixel(CV_32SC3)
  • Magick :: FloatPixel(CV_32FC3)
  • Magick :: DoublePixel(CV_64FC3)

上一个答案

这是一项正在进行中的工作 - 它可行,但可能不是最佳的,因为我仍在学习自己。

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

}

实际上,Magick ++能够将像素缓冲区写入已经分配的内存中,如果我们更早地宣布Mat,我们就可以这样做。

看起来像这样:

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

目前,我们至少暂时使用双内存,因为我们将像素数据从Magick ++复制到缓冲区,从缓冲区复制到Mat,所以我们应该做这样的事情(不是但经过测试):

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

答案 1 :(得分:4)

补充标记了很棒的答案(应该被接受)

cv::Mat有一个字节数组的构造函数。

Mat(int rows,
    int cols,
    int type,
    void* data,
    size_t step=AUTO_STEP)

这需要你分配一个字节数组;而不是Magick::Image.write直接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;
}