我用的是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. Windows服务器Pyton辅助运维--02.远程重启IIS服务器

    Windows服务器Pyton辅助运维 02.远程重启IIS服务器 开发环境: u  Web服务器: Windows Server 2008 R2 SP1 IIS 7.5 u  运维服务器: Pyth ...

  2. HDU-1241Oil Deposits

    Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否 ...

  3. APP纯黑盒测试---某些可以试试的操作

    一.多次快速点击一处功能入口: 该测试方法可以在某些应用中打开俩次目标界面,举一些具体一点的例子: 1.比如现在很多APP需要登陆,如果打开了俩次登录页面,就容易造成登录成功后应用跳转界面又是登录界面 ...

  4. socket实例2

    第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能 MainWindow.java package com.jikexue ...

  5. Mac OS X下HomeBrew安装卸载

    1.卸载 cd `brew --prefix` rm -rf Cellar brew prune rm `git ls-files` rm -r Library/Homebrew Library/Al ...

  6. Object-C 类定义 -- 笔记

    OC类分为两个文件,一个是.h文件,一个是.m文件 .h文件 存放类,函数的申明 .文件 存放类的具体实现 类申明使用关键字 @interface @end来申明 类实现使用关键字@implement ...

  7. JavaScript 自动分页插件 datatables

    DataTables Table plug-in for jQuery https://www.datatables.net/

  8. Github 开源编辑器 ATOM 已开放下载

    Update:2014-09-08 Atom 于5月6日正式开放下载(遗憾的是只有 MAC 版本),并且在 MIT 协义下开源,从界面可以看出,这款软件很像 Sublime Text,不过由于她的开源 ...

  9. 创建Chromium WebUI接口

    转载自:http://www.chromium.org/developers/webui Chrome的WebUI是那种,在Chrome中输入 "chrome://xxxx"就能打 ...

  10. ASIHTTPRequest使用指南---<<翻译稿>>

    ASIHTTPRequest使用指南---<<翻译稿>> 当第一次使用ASIHTTPRequest进行http请求时,会出现非常多的bug提示.查了一些资料,发现在少倒入了几个 ...