ImageMagick提取图像原始数据(ImageData/RawData)
我用的是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)的更多相关文章
- Codrops 实验:使用 Vibrant.js 提取图像颜色
Codrops 分享了一个有趣的颜色提取实验.这个想法是创建图像的调色板,既有图像本身的潜移默化的影响,也有一些花哨的颜色延伸.通过使用 Vibrant.js 来提取图像中的颜色,并通过 CSS 过滤 ...
- MATLAB·提取图像中多个目标
基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...
- 原来CNN是这样提取图像特征的。。。
对于即将到来的人工智能时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的领域,会不会感觉马上就out了?作为机器学习的一个分支,深度学习同样需要计算机获得强大的学 ...
- VGG16提取图像特征 (torch7)
VGG16提取图像特征 (torch7) VGG16 loadcaffe torch7 下载pretrained model,保存到当前目录下 th> caffemodel_url = 'htt ...
- 从ROS bag文件中提取图像
从ROS bag文件中提取图像 创建launch文件,如下: export.launch <launch> <node pkg="rosbag" type=&qu ...
- (转)FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
雷霄骅分类专栏: FFMPEG FFmpeg 本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215391 FFMPEG中的sw ...
- matlab 提取图像轮廓(图像边缘提取)
利用edge()函数提取图像轮廓,绘制出对象的边界和提取边界坐标信息,matlab实现代码如下: close all;clear all;clc; % 提取图像轮廓,提取图像边缘 I = imread ...
- CNN基础二:使用预训练网络提取图像特征
上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...
- 利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
基于知乎上的一个答案.问题如下: 也就是在一张照片里,已知有个长方形的物体,但是经过了透视投影,已经不再是规则的长方形,那么如何提取这个图形里的内容呢?这是个很常见的场景,比如在博物馆里看到一幅很喜欢 ...
随机推荐
- Laravel5.2 下使用Form
laravel到了5.1.*以上版本,便没有了illuminate/html类库的支持, 我试着把illuminate/html类库加入了laravel5.2,依然没有用, 但是laravelcoll ...
- 关于XMLEncoder和XMLDecoder
我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...
- hand第四次考核
使用Spring与Mybatis技术实现下要求: (2分)1,Spring的配置文件名称为ApplicationContext.xml (2分)2,在ApplicationContext.xml中配置 ...
- github atom创建自己的语法高亮
使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...
- BLOG PLUGINS
文章分享按钮 (1)加网(JiaThis) (2)百度分享 文章关联推荐 每篇博文下面可以显示你博客中与该篇博文有些关联的几篇文章,也就是智能推荐,一方面可以增加你博文的曝光率和点击率,一方面也可以给 ...
- SecureCRT7.3和SecureFX7.3的MAC下破解
破解脚本:http://files.cnblogs.com/files/jieyuefeng/SecureCRTFX_mac_crack.zip 破解方法: sudo perl ~/Downloads ...
- canvas--画宇宙
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- ValidateBox( 验证框) 组件
本节课重点了解 EasyUI 中 ValidateBox(验证框)组件的使用方法,这个组件依赖于Tooltip(提示框)组件. 一. 加载方式//class 加载方式<input id=&quo ...
- JS简单实现图片切换
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- .net web api 的route理解
.NET web api 的特性是和MVC一样,通过Route 来控制action的访问方式.Route匹配规则是个奇特的方式,首先看一段Route的模板 Routes.MapHttpRoute( n ...