将Magick :: Image转换为cv :: Mat [英] Convert Magick::Image to cv::Mat

查看:164
本文介绍了将Magick :: Image转换为cv :: Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

作为参考,反过来:将cv :: Mat转换为Magick ::图片

推荐答案

更新后的答案

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

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

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

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)

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

上一个答案

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

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

}

实际上,Magick ++能够编写像素缓冲区你已经分配了一些内存,如果我们早点宣布 Mat ,我们可以做。

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.

看起来像这个:

   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 ,所以我们应该做这样的事情(尚未测试):

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

这篇关于将Magick :: Image转换为cv :: Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆