ImageMagick和OpenCV文件格式互转

图像处理领域两大开源库OpenCV和ImageMagick,前者偏重于智能识别算法,后者偏重于艺术加工

两个库使用的图像格式不同,相互转换的方法如下:

OpenCV -> ImageMagick

Image Ipl2Magick(IplImage* iplImage)
{
   Image mgk(iplImage->width, iplImage->height, "BGR", CharPixel, (char *)iplImage->imageData);
   return mgk;
}


ImageMagick -> OpenCV
void Magick2Ipl(Image magicImage, IplImage* cvImage)
{
   int width= magicImage.size().width();
   int height = magicImage.size().height();

   byte* blob= new byte[cvImage->imageSize];
   magicImage.write(0,0, width, height, "BGRA", MagickCore::CharPixel, blob);
   memcpy(cvImage->imageData, blob, cvImage->imageSize);
   delete [] blob;
}


需要注意OpenCV创建图像要创建成4通道的图像

IplImage* cvimg =  cvCreateImage(cvSize(...,...), IPL_DEPTH_8U, 4);



你可能感兴趣的:(图像处理)