我用的是ImageMagickWand的接口,因为这接口比Core接口更上层,所以官方文档推荐用。
抽取整个图像文件字节数据:
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=20664
抽取图像像素的字节数据:
http://www.imagemagick.org/discourse-server/viewtopic.php?f=6&t=12135
ImageMagick附带的convert工具命令使用:
convert [-option] inputfile outputfile
以下时常用option:
-colorspace
-size
-depth
以下是提取原始数据的命令:
convert -colorspace gray -depth 16 inputfile gray:filename.raw
.raw文件会在convert工具的当前目录下生成,.raw文件就是没有文件头的原始图像像素的字节文件。
以下时Demo:
// ImageMagick_use_test.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "wand\MagickWand.h"
#include "magick\MagickCore.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "malloc.h" #define OUT void ThrowWandException(MagickWand* wand)
{
char *description; ExceptionType severity;
char err_msg[]; description=MagickGetException(wand,&severity);
sprintf(err_msg,"%s\nError_Type is %d\n",description,severity);
//description=(char *) MagickRelinquishMemory(description); printf(err_msg);
} char* __stdcall GetGrayPixelFormat_16bit( const char* fileName, int width, int height, OUT char* pDestImage ); bool __stdcall GetRGBPixelFormat_16bit( const char* fileName, int width, int height, OUT char* pRed, OUT char* pGreen, OUT char* pBlue); int _tmain(int argc, _TCHAR* argv[])
{ char *pDestImage = NULL;
char *pRed =NULL;
char *pGreen = NULL;
char *pBlue = NULL;
//Allocate 1024*5 Buffer in bytes
pDestImage = (char*)malloc(**);
pRed = (char*)malloc(**);
pGreen = (char*)malloc(**);
pBlue = (char*)malloc(**); memset(pDestImage,,**);
memset(pRed, , **);
memset(pGreen, , **);
memset(pBlue, , **); pDestImage = GetGrayPixelFormat_16bit( "C:\\Users\\Yajun Dou\\Desktop\\12.jpg", , , pDestImage); if(pDestImage == NULL)
{
printf("GetGrayPixelFormat_16bit Failed!\n");
} if(GetRGBPixelFormat_16bit("C:\\Users\\Yajun Dou\\Desktop\\12.jpg",,,pRed,pGreen,pBlue)==false)
{
printf("GetRGBPixelFormat_16bit Failed!\n");
} free(pDestImage);
free(pRed);
free(pGreen);
free(pBlue); return ;
} char* __stdcall GetGrayPixelFormat_16bit( const char* fileName, int width, int height, OUT char* pDestImage )
{
MagickBooleanType status; MagickWand *magick_wand; char* pReturnDestImage = NULL;
char* pReturnDestImageData = NULL; int IMageDepth; //Initialize Magick Enviroment MagickWandGenesis();
magick_wand=NewMagickWand(); //Read image
status=MagickReadImage(magick_wand,fileName);
if (status == MagickFalse)
ThrowWandException(magick_wand); //得到图像深度
IMageDepth = (int)MagickGetImageDepth(magick_wand); printf("The Current Image Depth is %d\n",IMageDepth); //convert image to grayscale
status = MagickSetImageColorspace(magick_wand,GRAYColorspace); if(status == MagickFalse)
ThrowWandException(magick_wand); status = MagickTransformImageColorspace(magick_wand,GRAYColorspace); if(status == MagickFalse)
ThrowWandException(magick_wand); IMageDepth = (int)MagickGetImageDepth(magick_wand); printf("The Current Image Depth is %d\n",IMageDepth); //得到图像宽度和高度
int Height; Height = (int)MagickGetImageHeight(magick_wand); printf("The Current Image Height is %d\n",Height); int Width; Width = (int)MagickGetImageWidth(magick_wand); printf("The Current Image Width is %d\n",Width); //得到像素的通道数
int ImageChannels;
Image* pImage = NULL; pImage = GetImageFromMagickWand(magick_wand); if(pImage == NULL)
{
return NULL;
} ImageChannels = pImage->channels; /*printf("RedChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,RedChannel));
printf("GreenChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,GreenChannel));
printf("BlueChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,BlueChannel));
printf("GrayChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,GrayChannel));*/ if(Width != width || Height != height || IMageDepth != || ImageChannels != )
{ if(MagickResizeImage(magick_wand,width,height,LanczosFilter,1.0) == MagickFalse)
{
ThrowWandException(magick_wand);
} //设置图像深度
if(MagickSetImageDepth(magick_wand,) == MagickFalse)
{
ThrowWandException(magick_wand);
}
//设置灰度通道深度
if(MagickSetImageChannelDepth(magick_wand,GrayChannel,) == MagickFalse)
{
ThrowWandException(magick_wand);
} } // MagickWriteImages(magick_wand,"C:\\Users\\Yajun Dou\\Desktop\\12_trans1.bmp",MagickFalse); IMageDepth = (int)MagickGetImageDepth(magick_wand); printf("The Current Image Depth is %d\n",IMageDepth); int ImageDataLength;
int ImageDataSize;
/* //MagickGetImageBlob函数得到的是整个图像文件的二进制字节流,并非图像数据
pReturnDestImage = (char*)MagickGetImageBlob(magick_wand,(size_t*)&ImageDataLength);*/
MagickGetImagePixels(magick_wand,,,width,height,"I",ShortPixel,pDestImage); magick_wand=DestroyMagickWand(magick_wand); MagickWand*(); return pDestImage;
} bool __stdcall GetRGBPixelFormat_16bit( const char* fileName, int width, int height, OUT char* pRed, OUT char* pGreen, OUT char* pBlue)
{
MagickBooleanType status; MagickWand *magick_wand; //Initialize Magick Enviroment MagickWandGenesis();
magick_wand=NewMagickWand(); //Read image
status=MagickReadImage(magick_wand,fileName);
if (status == MagickFalse)
{
ThrowWandException(magick_wand);
return false;
} //convert image to RGB three channels
status = MagickSetImageColorspace(magick_wand,RGBColorspace); if(status == MagickFalse)
ThrowWandException(magick_wand); status = MagickTransformImageColorspace(magick_wand,RGBColorspace); if(status == MagickFalse)
ThrowWandException(magick_wand); //得到图像深度
int IMageDepth; IMageDepth = (int)MagickGetImageDepth(magick_wand); printf("The Current Image Depth is %d\n",IMageDepth); //得到图像宽度和高度
int Height; Height = (int)MagickGetImageHeight(magick_wand); printf("The Current Image Height is %d\n",Height); int Width; Width = (int)MagickGetImageWidth(magick_wand); printf("The Current Image Width is %d\n",Width); //得到像素的通道数
int ImageChannels;
Image* pImage = NULL; pImage = GetImageFromMagickWand(magick_wand); if(pImage == NULL)
{
return false;
} ImageChannels = pImage->channels; /*printf("RedChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,RedChannel));
printf("GreenChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,GreenChannel));
printf("BlueChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,BlueChannel));
printf("GrayChannelDepth is %d\n",MagickGetImageChannelDepth(magick_wand,GrayChannel));*/ if(Width != width || Height != height)
{ if(MagickResizeImage(magick_wand,width,height,LanczosFilter,1.0) == MagickFalse)
{
ThrowWandException(magick_wand);
} } // MagickWriteImages(magick_wand,"C:\\Users\\Yajun Dou\\Desktop\\12_trans1.bmp",MagickFalse); IMageDepth = (int)MagickGetImageDepth(magick_wand); printf("The Current Image Depth is %d\n",IMageDepth); int ImageDataLength;
int ImageDataSize;
/* //MagickGetImageBlob函数得到的是整个图像文件的二进制字节流,并非图像数据
pReturnDestImage = (char*)MagickGetImageBlob(magick_wand,(size_t*)&ImageDataLength);*/
MagickGetImagePixels(magick_wand,,,width,height,"R",ShortPixel,pRed);
MagickGetImagePixels(magick_wand,,,width,height,"G",ShortPixel,pGreen);
MagickGetImagePixels(magick_wand,,,width,height,"B",ShortPixel,pBlue); magick_wand=DestroyMagickWand(magick_wand); MagickWand*(); return true; }
TIPS:MagickWandGenesis()函数是对于整个进程(全局的初始化),一般要在主线程中调用,如果每个函数都调用MagickWandGenesis(),MagickWand*();必然带来巨大性能开销。以上是Demo,所以没管那么多,如果糅合到项目中,必然不能这么写。ImageMagickGetImagePixels,后面的shortpixel是指提取16bit的图像数据,"R" "G"等是指示通道顺序,所以可以"RGB"连接起来写.最后一个参数是输出数据的字节数组。
另外还可以调用MagickExportImagePixels这个接口来得到图像数据。ImageMagickGetImagePixels这个接口貌似是过时的接口,虽然能用。这两个API的参数是一样的。
reference:
http://web.mit.edu/usmanm/MacData/afs/athena/contrib/graphics/share/ImageMagick/www/convert.html