Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将像素数据转换为C+中的图像+;_C++_Image_File Io - Fatal编程技术网

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

C++ 将像素数据转换为C+中的图像+;,c++,image,file-io,C++,Image,File Io,我有一个程序,可以生成具有以下结构的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 06

我有一个程序,可以生成具有以下结构的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
.....

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

我会使用OpenCV。可以使用宽度和高度作为尺寸将结构转换为cv::Mat。然后,您可以使用OpenCV函数来查看图像或相当轻松地写出bmp、png、tiff或jpg。假设您有无符号字符数据,我假设它已经是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个输入灰度(0000000 10101020202…ffffff)。如果您查找BMP头文件格式,这两种方法都非常简单。

另一种方法是使用,它非常容易设置,因为它没有依赖项,您只需将源文件放入项目中并使用它进行编译即可。它非常易于使用,并允许您将数据保存到一个无损PNG图像中。

您可以在命令行中使用ImageMagick转换图像。它安装在大多数Linux发行版上,并可用于OS X(理想情况下通过
自制
)和Windows

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

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

你的图像对比度很低,所以我添加了
-auto-level
来拉伸它。你的图像是灰色的


你也可以用IMAGE MigC+C++库实现相同的处理。

你没有给出一些关键信息:1)通道数量(每个像素,如RGB或B&W等);2) 每像素位数(例如24位、8位等);3) Endian(大端或小端)。顺便说一下,图像不是C++语言的一部分,而是特定于平台的。如果宽度是第一个整数,那么就是
06050606
,有趣的是,高度是
060605
,图像的第一行有与文件大小相同亮度的像素-我认为不是!给整个图像及其尺寸提供一个链接,我很可能会为您计算出来。@MarkSetchell我发布的数据只是图像数据(testimage.image),所以宽度和高度不包括在这张图中。高度为1040,宽度为1392。我上传了一个包含完整数据的.txt文件()请注意,数据与我上面发布的不同,因为这是一个不同的图像。哇。。这很简单。我试过了,效果很好。非常感谢。知道怎么做就容易;-)