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检测图像中的长方形画布或纸张并提取图像内容
基于知乎上的一个答案.问题如下: 也就是在一张照片里,已知有个长方形的物体,但是经过了透视投影,已经不再是规则的长方形,那么如何提取这个图形里的内容呢?这是个很常见的场景,比如在博物馆里看到一幅很喜欢 ...
随机推荐
- HDU5196--DZY Loves Inversions 树状数组 逆序数
题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...
- Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)
题目列表: 2146 Problem A [手速]阔绰的Dim 2147 Problem B [手速]颓废的Dim 2148 Problem C [手速]我的滑板鞋 2149 Problem D [手 ...
- libvirtVirsh
virsh基于不同协议远程连接libvirt drivers(Hypervisors) 基于OS系统账号ssh登陆
- 行内人解读开发一个App需要多少钱?
对于很多互联网的创业者来说,评估前期的创业成本是很重要的.在这几年的创业大潮中,伴随着“互联网+”和“互联网思维”的普及,很多创业项目选择了开发app作为创业项目的载体.在我接触到的很多创业者,找Ap ...
- vue + vuex 表单处理
使用场景:在一个表单中,各项数据提交后需要重置表单中各<input>元素的值为空. 组件中关联: <template> <el-form ref="form&q ...
- Linux下Weblogic域的创建过程
环境介绍:操作系统 :Redhat 5.5Weblogic :英文版 8.1.6 Weblogic安装目录 :/weblogic 一.域的建立执行下面语句进入weblogic的bin目录: cd /w ...
- [Redux] Implementing combineReducers() from Scratch
The combineReducers function we used in previous post: const todoApp = combineReducers({ todos, visi ...
- C++中数组初始化
#include<iostream>using std::cout;using std::endl;int arr1[5];int arr2[5] = {1,3,5};int main() ...
- 对return 语句的正确性和效率进行检查
注意事项如下: 1. return 语句不可返回指向"堆栈内存“的”指针“或者”引用“,因为该内存单元在函数体结束时被自动释放. //错误 char* Func(void) { char s ...
- C#winform检测电脑安装的.netframework版本和是否安装了某软件
代码如下: //C#获取已安装 .NET Framework 版本 private static string[] GetDotNetVersions() { DirectoryInfo[] dire ...