我用的是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); MagickWandTerminus(); 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); MagickWandTerminus(); return true; }

TIPS:MagickWandGenesis()函数是对于整个进程(全局的初始化),一般要在主线程中调用,如果每个函数都调用MagickWandGenesis(),MagickWandTerminus();必然带来巨大性能开销。以上是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

ImageMagick提取图像原始数据(ImageData/RawData)的更多相关文章

  1. Codrops 实验:使用 Vibrant.js 提取图像颜色

    Codrops 分享了一个有趣的颜色提取实验.这个想法是创建图像的调色板,既有图像本身的潜移默化的影响,也有一些花哨的颜色延伸.通过使用 Vibrant.js 来提取图像中的颜色,并通过 CSS 过滤 ...

  2. MATLAB·提取图像中多个目标

    基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...

  3. 原来CNN是这样提取图像特征的。。。

    对于即将到来的人工智能时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的领域,会不会感觉马上就out了?作为机器学习的一个分支,深度学习同样需要计算机获得强大的学 ...

  4. VGG16提取图像特征 (torch7)

    VGG16提取图像特征 (torch7) VGG16 loadcaffe torch7 下载pretrained model,保存到当前目录下 th> caffemodel_url = 'htt ...

  5. 从ROS bag文件中提取图像

    从ROS bag文件中提取图像 创建launch文件,如下: export.launch <launch> <node pkg="rosbag" type=&qu ...

  6. (转)FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)

    雷霄骅分类专栏: FFMPEG FFmpeg 本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215391 FFMPEG中的sw ...

  7. matlab 提取图像轮廓(图像边缘提取)

    利用edge()函数提取图像轮廓,绘制出对象的边界和提取边界坐标信息,matlab实现代码如下: close all;clear all;clc; % 提取图像轮廓,提取图像边缘 I = imread ...

  8. CNN基础二:使用预训练网络提取图像特征

    上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...

  9. 利用OpenCV检测图像中的长方形画布或纸张并提取图像内容

    基于知乎上的一个答案.问题如下: 也就是在一张照片里,已知有个长方形的物体,但是经过了透视投影,已经不再是规则的长方形,那么如何提取这个图形里的内容呢?这是个很常见的场景,比如在博物馆里看到一幅很喜欢 ...

随机推荐

  1. js 数组引用 发现的问题

    最近做项目时,要对返回的数据[保存在json数组中]做一次修改,但原数据要保留一次做备用.首先想到,原数据不动,用一个临时的变量来修改,大致模型就是这样: // 原始: a=[1,2,3,4,5,.. ...

  2. 关于解决Oracle登录:ora-12154:tns:无法解析指定的连接标识符

    (注:此文摘自http://www.linuxidc.com/Linux/2012-04/59322.htm) 开始学习Oracle,所以今天就打算把Oracle 10g安装下.安装完后就来进行测试是 ...

  3. zoj3658 Simple Function (函数值域)

    Simple Function Time Limit: 2 Seconds       Memory Limit: 32768 KB Knowing that x can be any real nu ...

  4. linux 切换用户之后变成-bash-x.x$的解决方法

    我们平时在linux下切换用户后命令行为什么会变成-bash-3.2$呢,我们来分析一下,这就是跟linux的机制有关联了,因为在linux下每次通过useradd创建新的用户时,都会将所有的配置文件 ...

  5. iOS 后台定位被拒注意事项

    iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...

  6. windows使用命令行杀进程

    在windows有时使用任务管理器杀进程,一直杀不掉: 这个时候,可以使用命令行: 先使用tasklist 命令查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令 如要杀nginx ...

  7. Oracle 11g新特性invisible index(不可见的索引)

    假设一张表上有十几个索引,你有什么感受?显然会拖慢增.删.改的速度.不要指望开发者能建好索引.我的处理方法是先监控非常长的一段时间.看哪些索引没实用到,然后删除. 但删除以后,假设发现某一天实用,那又 ...

  8. 菜鸟玩云计算之十六:Ubuntu14.04上创建的虚拟机迁移到RHEL6.4

    菜鸟玩云计算之十六:Ubuntu14.04上创建的RHEL6.4虚拟机迁移到RHEL6.4主机上 RHEL6.4 Server作为虚拟机的HOST,执行以下的命令检查配置和安装相关软件: # egre ...

  9. C#基础枚举的设计

    枚举分为:简单枚举和标记枚举(为了枚举值位操作) 标记枚举的用法:

  10. 在C#、Java中,为什么不能用[返回值]区别重载方法?

    为什么方法签名只包含方法名和参数列表,而没有把返回值考虑进去? 如下有两个方法: void Func(){} string Func() { return string.Empty; } 编辑器可以根 ...