简体   繁体   English

如何将.png文件转换为二维矩阵?

[英]How do you turn a .png file into a 2-d matrix?

I am working on a project for a Bio-medical Imaging course (Note: it is a non-programming course, so asking for programming help is not cheating. Asking for conceptual help with planning would be cheating.) where I need to manipulate an image using different mathematical transforms. 我正在研究一个生物医学成像课程的项目(注意:这是一个非编程课程,所以要求编程帮助不是作弊。要求规划的概念性帮助会作弊。)我需要操纵一个图像使用不同的数学变换。 I am writing in C so it can be as fast as possible. 我用C语写,所以它可以尽可能快。 I have finished the code for the mathematical transforms, but I have realized that I do not know how to turn a grayscale .png file into a 2-d matrix/array to compute with, and I do not know how to display a .png file in C. Can anyone help me? 我已完成数学变换的代码,但我已经意识到我不知道如何将灰度.png文件转换为二维矩阵/数组来计算,我不知道如何显示.png用C语言文件。有人可以帮帮我吗?

I'm trying to turn the "image.png" image into a 2d array where each entry in the array has a value between 0 - 255 and corresponds with each pixel in "image.png". 我正在尝试将“image.png”图像转换为2d数组,其中数组中的每个条目的值都在0到255之间,并与“image.png”中的每个像素相对应。 I also want to turn a 2d array where each entry corresponds to a pixel in the image and has a value between 0 - 255 into a new "image_two.png" file. 我还想转换一个2d数组,其中每个条目对应于图像中的一个像素,并且在0到255之间的值为新的“image_two.png”文件。

I'm a somewhat new programmer. 我是一个有点新手的程序员。 I have a solid base in python programming, but C is new for me. 我在python编程方面有扎实的基础,但C对我来说是新的。 I have done a lot of research and I have found a lot of people talking about using "this library" or "that library" , or also "this library" , but how do I use a downloaded library in C? 我已经做了很多研究,我发现很多人都在谈论使用“这个库”“那个库” ,或者“这个库” ,但是如何在C中使用下载的库? It's unfamiliar territory for me as a python programmer :( 作为一个python程序员,这对我来说是陌生的领域:(

I'm using Ubuntu 12.04 我正在使用Ubuntu 12.04

To reiterate: 重申:

  • How do you read a grayscale .png image as a 2-d array/matrix in C? 你如何在C中读取灰度.png图像作为二维数组/矩阵?

  • How do you display a 2-d array/matrix as a grayscale image in C? 如何在C中将二维阵列/矩阵显示为灰度图像?

  • How do you use a downloaded library in C code (specifically for the two questions above)? 如何在C代码中使用下载的库(特别是上面的两个问题)? I found out how to use these libraries. 我发现了如何使用这些库。

EDIT: I am still having trouble figuring out how to create a grayscale 2d array out of a .png file and how to make a .png file out of a grayscale 2d matrix. 编辑:我仍然无法弄清楚如何从.png文件创建灰度2d数组以及如何从灰度2d矩阵中生成.png文件。 Can anyone else help? 其他人可以帮忙吗?

You can use a more general purpose image handling library and you might find it easier to use. 您可以使用更通用的图像处理库,您可能会发现它更容易使用。 I recommend FreeImage http://freeimage.sourceforge.net/ . 我推荐FreeImage http://freeimage.sourceforge.net/ See the pixel access section of the manual to get access to the pixel data. 请参阅手册的像素访问部分以访问像素数据。 You can then work with it directly or copy it into your own matrix. 然后,您可以直接使用它或将其复制到您自己的矩阵中。

To install a library in Linux, typically you will use the package manager. 要在Linux中安装库,通常您将使用包管理器。 For example, in Debian (this includes Ubuntu) you might do: 例如,在Debian(包括Ubuntu)中你可能会这样做:

$ apt-cache search libpng

You'll decide which package to install based on the results of running this command and then you will run 您将根据运行此命令的结果决定安装哪个软件包,然后运行

$ sudo apt-get install <package-name>

This command will likely install png.h in a location that is already included in gcc's search path. 此命令可能会在已包含在gcc搜索路径中的位置安装png.h This means that to use png.h in your program, all you have to do is include it. 这意味着要在程序中使用png.h ,您只需要包含它。

#include <png.h>

Skip to chapter 3 in the libpng manual for a walkthrough on reading a png file. 跳至libpng手册中的第3章,了解有关读取png文件的演练。

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

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