读取和C语言显示灰度图像。 [英] Read and display gray scale images in C language.

查看:1232
本文介绍了读取和C语言显示灰度图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C,pre过程中,它加载灰度图像,然后显示修改后的图像。我的问题是:

I want to load gray scale images in C, pre process it and then display the modified image. My question is:

什么是导入灰阶图像(JPEG,PNG格式)的C语言的正确方法?

What is the right way to import gray scale images(jpeg,png formats)in C language ?

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

My own search before asking this question.

1 FREAD或文件管理,我们可以看到图像但数据将被加密(COM pressed),我想每一个像素的灰度值(0-255)。

1- fread or with file management, we can read image but data will be encrypted(compressed), I want gray scale values(0-255) of each pixel.

2 - 有一个API ImageMagick的可以是有益的,但​​它是具有Mac OS X安装问题。

2- There is one API ImageMagick which can be helpful but it is having installation problem with Mac OS X.

我已经做了图像处理的Python和MATLAB,但没有关于C语言的想法。

I have done image processing in python and matlab but have no idea about C language.

感谢

推荐答案

这样的回答是演示如何使用的ImageMagick的使用X code MagickWand API开发的尝试,并基于从OP的评论

ImageMagick的安装后,启动新的X code命令-C线项目。写任何code之前,你需要告诉 LLVM 有关ImageMagick的资源/库/等。

After installing ImageMagick, start a new Xcode command-line C project. Before writing any code, you need to tell llvm about the ImageMagick resources/libraries/etc.

有很多很多,方法来实现这一点,但这里是我能想到的最快的。

There are many, many, ways to achieve this, but here's the quickest I can think of.


  • 导航到顶级项目的构建设置

  • 在全部(而不是基本)搜索其他链接器标记

  • X $ C $的C之外,开拓Terminal.app并输入以下内容

    • MagickWand-配置--ldflags

    • Navigate to top-level project's "Build Settings"
    • Under "All" (not "Basic") search for "Other Linker Flags"
    • Outside of Xcode, open up Terminal.app and enter the following
      • MagickWand-config --ldflags

      其它链接器标记


      • 早在设置搜索;输入其他C标记

      • 早在Terminal.app运行以下

        • MagickWand-配置--cflags

        • Back in the settings search; enter "Other C Flags"
        • Back in Terminal.app run the following
          • MagickWand-config --cflags

          其他C标志

          在文件的main.c ,你应该注意到X $ C $ç拾起MagickWand马上命令。

          Over in file main.c, you should notice Xcode picking-up MagickWand commands right away.

          MagickWand在X

          请尝试以下操作(需要安装X11)...

          Try the following (needs X11 installed) ...

          #include <stdio.h>
          #include <wand/MagickWand.h>
          
          int main(int argc, const char * argv[]) {
              MagickWandGenesis();
              MagickWand * wand;
              wand = NewMagickWand();
              MagickReadImage(wand, "wizard:");
              MagickQuantizeImage(wand, 255, GRAYColorspace, 0, MagickFalse, MagickTrue);
              MagickDisplayImage(wand, ":0");
              wand = DestroyMagickWand(wand);
              MagickWandTerminus();
              return 0;
          }
          

          ...并建立运行+来验证。

          ... and build + run to verify.

          读取并显示在C语言中灰度图像

          修改

          要获得灰度每个像素(0-255)值,你可以调用一个像素迭代器(见的第二个例子这里),或出口值。这里是通过导出动态填充灰度值的列表的例子...

          To get the gray scale (0-255) value for each pixel, you can invoke a pixel iterator (see second example here), or export the values. Here is an example of dynamically populating a list of gray values by exporting...

              // Get image size from wand instance
              size_t width = MagickGetImageWidth(wand);
              size_t height = MagickGetImageHeight(wand);
              size_t total_gray_pixels =  width * height;
          
              // Allocate memory to hold values (total pixels * size of data type)
              unsigned char * blob = malloc(total_gray_pixels);
          
              MagickExportImagePixels(wand,      // Image instance
                                      0,         // Start X
                                      0,         // Start Y
                                      width,     // End X
                                      height,    // End Y
                                      "I",       // Value where "I" = intensity = gray value
                                      CharPixel, // Storage type where "unsigned char == (0 ~ 255)
                                      blob);     // Destination pointer
          
              // Dump to stdout
              for (int i = 0; i < total_gray_pixels; i++ ) {
                  printf("Gray value @ %lux%lu = %d\n", i % width, i / height, (int)blob[i]);
              }
              /** Example output...
               * Gray value @ 0x0 = 226
               * Gray value @ 1x0 = 189
               * Gray value @ 2x0 = 153
               * Gray value @ 3x0 = 116
               * Gray value @ 4x0 = 80
               * ... etc
               */
          

          这篇关于读取和C语言显示灰度图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆