繁体   English   中英

在C ++中将像素数据转换为图像

[英]Convert pixel data to image in C++

我有一个程序可以生成具有以下结构的8位图像:

struct int8image
{
  int width;
  int height;
  unsigned char* image;
};

将像素数据(int8image.image)转储到文本文件后,得到以下输出:

0605 0606 0606 0605 0606 0506 0606 0606
0606 0605 0606 0506 0606 0606 0606 0606
0606 0606 0606 0606 0606 0505 0606 0706
0606 0606 0606 0606 0606 0606 0706 0706
.....

如何将其转换为可查看的图像(格式无关紧要)?

您可以在命令行使用ImageMagick转换图像。 它已安装在大多数Linux发行版中,并且可用于OS X(最好通过homebrew )以及Windows。

如果要将其转换为PNG,可以运行以下命令:

convert -size 1392x1040 -depth 8 image.gray -auto-level image.png

在此处输入图片说明

您的图像对比度很低,因此我添加了-auto-level进行拉伸。 您的图像是灰度的。

您还可以在此处使用ImageMagick C ++库实现相同的处理。

我会使用OpenCV。 您可以使用宽度和高度作为尺寸将结构转换为cv :: Mat。 然后,您可以使用OpenCV函数查看图像或相当轻松地写出bmp,png,tiff或jpg。 假设您有未签名的char数据,我假设它已经是8位灰度,因此它看起来像这样。

int8image testData;

// Do stuff in your program so that testData contains an image.

// Define a cv::Mat object with a pointer to the data and the data type set
// as "CV_8U" which means 8-bit unsigned data
cv::Mat image( testData.height, testData.width, CV_8U, testData.image );

// Write the image to a bitmap in the current working directory named 
// "test.bmp". This is just an example of one way you could write it out.
cv::imwrite( "test.bmp", image );

库是最灵活的,但是如果您希望快速又脏乱,则可以为RGB24写出一个简单的54字节BMP标头,并假设您的数据是灰度的,只需为每个3个分量重复八位字节像素。 或者,如果您希望能够更直接地写入图像数据,则为托盘图像写入BMP标头,您的托盘本质上就是256项灰度(000000,010101,020202 ... ffffff)。 如果您查找BMP标头格式,则两种方法都非常简单。

另一种选择是使用LodePNG库 ,它很容易安装,因为它没有依赖关系,您只需将源文件放入项目中并进行编译即可。 它非常易于使用,并允许您将数据保存到无损PNG图像中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM