用C语言读取并显示灰度图像。

2023-12-08

我想在C中加载灰度图像,对其进行预处理,然后显示修改后的图像。我的问题是:

在C语言中导入灰度图像(jpeg、png格式)的正确方法是什么?

在问这个问题之前我自己搜索过。

1-fread或文件管理,我们可以读取图像,但数据将被加密(压缩),我想要每个像素的灰度值(0-255)。

2-只有一个API图像魔术师这可能会有帮助,但在 Mac OS X 上有安装问题。

我用python和matlab做过图像处理,但对C语言一无所知。

Thanks


您有多种选择,但我将从最简单且与 OSX 集成程度最低的选项开始,逐步与 OSX 集成程度更高。

最简单的选择

就我个人而言,如果我打算处理灰度图像,我会编写我的软件以使用 NetPBM 的便携式灰度图 (PGM) 格式,因为这是最简单的读写方式,并且可以轻松与其他格式互换。没有压缩、DCT、量化、色彩空间、EXIF 数据 - 只有带有简单标头的数据。文档是here.

基本上 PGM 文件如下所示:

enter image description here

P2
# Shows the word "FEEP" (example from Netpbm man page on PGM)
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

您可以看到P2说它是 ASCII(易于阅读)和灰度图。然后下一行表示它的宽度为 24 像素,高为 7 像素,最亮的像素为 15。非常易于读写。您可以更改P2 to P5并将 MAXVAL 之后的所有内容都写成二进制以节省空间。

现在,您可以在程序外部使用 ImageMagick 将 JPEG、PNG、GIF、TIFF 文件转换为 PGM,如下所示 - 无需任何链接或库或编译器开关:

convert input.png output.pgm

convert input.jpg output.pgm

同样,当您完成处理并创建 PGM 格式的结果输出文件时,只需反转参数即可将其转换为 JPEG 或 TIFF:

convert result.pgm result.jpg

就个人而言,我会使用安装 ImageMagickhomebrew。你去自制网站复制一行代码并将其粘贴到终端中进行安装homebrew。然后你可以使用以下命令安装 ImageMagick:

brew install imagemagick

顺便说一下,如果你想尝试这里的其他建议,使用 OpenCV,那么就很简单

brew search opencv
brew install homebrew/science/opencv

如果您想要一个 OpenCV 项目的小型独立示例,请查看我对另一个问题的回答here- 您还可以在我的其他项目中使用 ImageMagick 从命令行看到这样的项目是如何实现的answer对于同样的问题。

Magick++

如果您选择使用安装 ImageMagickhomebrew您将获得 Magick++,它允许您用 C 和 C++ 编写算法。它非常易于使用,可以在任何平台上运行,包括 OSX、Windows 和 Linux,因此从这个角度来看它很有吸引力。它还内置了很多很多图像处理功能。有一个很好的教程here,和文档here.

您的代码将如下所示:

// Read an image from URL
Image url_image("http://www.serverName.com/image.gif");

// Read image from local filesystem
Image local_file_image("my_image.gif"); 

// Modify image
Pixels my_pixel_cache(my_image);
PixelPacket* pixels;
// define the view area that will be accessed via the image pixel cache
int start_x = 10, start_y = 20, size_x = 200, size_y = 100;
// return a pointer to the pixels of the defined pixel cache
pixels = my_pixel_cache.get(start_x, start_y, size_x, size_y);

// set the color of the first pixel from the pixel cache to black (x=10, y=20 on my_image) 
*pixels = Color("black");

// set to green the pixel 200 from the pixel cache:
// this pixel is located at x=0, y=1 in the pixel cache (x=10, y=21 on my_image) 
*(pixels+200) = Color("green");

// now that the operations on my_pixel_cache have been finalized
// ensure that the pixel cache is transferred back to my_image
my_pixel_cache.sync();

// Save results as BMP file
image.write(“result.bmp”);

苹果 OSX 选项

另一个完全独立的选项是使用 Apple 提供的用于操作图像的工具 - 它们快速且易于使用,但不适用于 Linux 或 Windows。所以,举例来说,如果你想

a) 加载 PNG 文件(或 TIFF 或 JPEG,只需更改扩展名)

b) 将其保存为 JPEG 文件

c) 处理各个像素

// Load a PNG file
NSImage * strImage = [[NSImage alloc]initWithContentsOfFile:@"/Users/mark/Desktop/input.png"];

// Save NSImage as JPG
NSData *imageData = [strImage TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:@"/Users/Mark/Desktop/result.jpg" atomically:YES];

// Access individual pixels
int w=imageRep.pixelsWide;
int h=imageRep.pixelsHigh;
int bps=imageRep.bitsPerSample;

printf("Dimensions: %dx%d\n",w,h);
printf("bps: %d\n",bps);

// Get a pointer to the uncompressed, unencoded pixel data
unsigned char *pixelData = [imageRep bitmapData];

for(int j=0;j<10;j++){
    printf("Pixel %d: %d\n",j,pixelData[j]);
}

当然,您可以使用上面的代码并轻松制作一个小实用程序,将任何文件格式转换为 PGM,然后您可以采用我使用 PGM 格式的第一个建议,并且不需要安装 ImageMagick - 尽管它实际上非常简单和homebrew.

请记住,您可以使用 clang (Apple 的编译器)在单个项目中将 Objective-C (如上一个示例)与 C++ 和 C 混合,因此您可以按照您在问题中使用任何示例指出的方式继续使用 C我上面已经给出了。

如果您不熟悉 OSX 开发,则需要前往 AppStore 免费下载 Apple 的Xcode获取编译器和库。那么你必须做

xcode-select --install 

如果您希望使用 Makefile 和命令行编译/链接进行传统开发,请安装命令行工具。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用C语言读取并显示灰度图像。 的相关文章

随机推荐