如何在C中以像素级操作图像?
如何在 C 中读取图像,以便可以直接控制其像素(就像我们在 MATLAB 中所做的那样)?
我尝试了旧方法:
FILE *fp;
fp = fopen("C:\\a.tif","r");
这只是给了我图像文件的 ascii
形式(我认为)。
如何获得对图像的像素级控制并执行一些基本操作,例如反转图像?
How do I read an image in C so that I can have direct control over its pixels (like we do in MATLAB)?
I tried the old way:
FILE *fp;
fp = fopen("C:\\a.tif","r");
that just gives me the ascii
form of the image file (I think).
How can I get pixel level control over the image and do some basic operations like say, inverting the image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
OpenCV 是计算机视觉库,但也可用于“低级”任务。它支持 BMP、DIB、JPEG、JPG、JPE、PNG、PBM、PGM、PPM、SR、RAS、TIFF、TIF。
OpenCV is computer vision library, but can be used for "low level" tasks too. It supports BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, SR, RAS, TIFF, TIF.
看看 libtiff。几乎所有图像格式都具有某种标头数据,并且许多图像格式都经过压缩以保持文件大小合理。您当然可以编写C代码来读取标头并执行解压缩,但没有必要,因为其他人已经这样做了。请在此处查找一些 C 映像库。
另一个问题是您希望数据以什么形式进行操作 - 常见的选择是 24 位 RGB(即 R、G 和 B 各自在 0 到 255 之间变化)。在 MATLAB 中,您还可以看到 R、G、B 作为从 0.0 到 1.0 变化的双精度值。或者您可能需要不同的色彩空间(HSV、YUV 等)。
请记住,整数运算速度更快,但也可能更麻烦。
Have a look at libtiff. Nearly all image formats have some sort of header data and many are compressed to keep filesize reasonable. You can of course write C code to read the headers and perform decompression, but there's no need since someone else has already done it. Look here for some C image libraries.
Another question is what form you want the data in for manipulation - a common choice is 24-bit RGB (i.e. R, G, and B each vary from 0 to 255). In MATLAB you also see R, G, B as doubles varying from 0.0 to 1.0. Or you may want a different colour space (HSV, YUV, etc.).
Remember that integer operations are faster, but they can be more troublesome.
要操作jpeg文件,请使用libjpeg,tiff 文件使用 libtiff 等。
另一个简单的选择是 libtiff 等。 boutell.com/gd/" rel="nofollow noreferrer">gd 允许操作多种图像格式(PNG、JPEG、GIF...)。
To manipulate jpeg files use libjpeg, the tiff files use libtiff, etc.
Another easy option is gd which allows manipulation of many image formats (PNG, JPEG, GIF, ...).
有一些开源库可以加载不同类型的图像,还有一些 libtiff 可以加载 tiff 图像。如果您只是想玩玩并且文件格式不是很重要,那么我会看看 netpbm 格式(特别是 P5 和 P6)。为它编写一个加载器和保存器非常容易,当然也是一个很好的练习。
There are some open-source libs to load images of different types and libtiff to load tiff images. If you just want to play around and the file format is not very important, then I would take a look on netpbm format (inparticular P5 and P6). It is pretty easy to write a loader and saver for it and of course a good exercise.
ImageMagick 的 MagickWand 是另一个不错的选择
MagickWand from ImageMagick is another good option