代码之家  ›  专栏  ›  技术社区  ›  DanG

访问16位TIFF中的像素

  •  1
  • DanG  · 技术社区  · 11 年前

    我导入了一个灰度级的16位图像。我有一个机器人作为BitmapSource和一个图像(控件命名空间)。如何访问各个像素?我读过的CopyPixels是唯一的还是最好的方法?如果是这样的话,我不知道如何设置步幅,以及哪个像素包含像素强度值。

    方法1:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    using System.IO;
    using Nikon;
    using WindowsFormsApplication1;
    using System.Windows.Media.Imaging;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Controls;
    
    namespace Map
    {
        class OpenTIFF
        {
            static void OpenOne()
            {
            // Open a Stream and decode a TIFF image
            Stream imageStreamSource = new FileStream("C:\\Users\\Me\\Desktop\\"MyTif.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
            TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            image.Source = bitmapSource;
            }
        }
    }
    

    我认为这可能更简单(发现 here ),但我不清楚如何访问1D字节数组中的各个像素。

    方法2:

    static void OpenTwo()
           {
               System.Drawing.Image imageToConvert = System.Drawing.Image.FromFile("aa.tif", true);
               byte[] Ret = new byte[0];
               using (MemoryStream ms = new MemoryStream())
               {
                   imageToConvert.Save(ms, ImageFormat.Tiff);
                   Ret = ms.ToArray();
               }
           }
    

    谢谢 丹

    2 回复  |  直到 11 年前
        1
  •  2
  •   denver    11 年前

    您可能想要查看免费的图像库。 它有一个C#包装器。 http://freeimage.sourceforge.net/index.html

    要开始并给出一个快速示例:

    1. 下载二进制发行版
    2. 在visual studio中打开FreeImage.NET.sln(我使用的是2010)
    3. 构建Library项目
    4. 如果在XML注释中收到错误,请执行以下操作:进入项目属性,在生成下,将“将警告视为错误”从“全部”更改为“无”
    5. 创建新的控制台项目。
    6. 添加对在步骤3中生成的程序集的引用。
      FreeImage3154Win32\FreeImage\Wrapper\FreeImage.NET\cs\Library\bin\Debug\FreeImageNET.dll
    7. 将以下代码添加到Program.cs

      using FreeImageAPI;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // load image, this can by your 16-bit tif
                  FIBITMAP dib = FreeImage.LoadEx("myfile.tif");
                  // convert to 8-bits
                  dib = FreeImage.ConvertToGreyscale(dib);
                  // rotate image by 90 degrees
                  dib = FreeImage.Rotate(dib, 90);
                  // save image
                  FreeImage.SaveEx(dib, "newfile.png");
                  // unload bitmap
                  FreeImage.UnloadEx(ref dib);
             }
          }
      

      }

    8. 将下载的FreeImage二进制dll复制到bin目录中。
      FreeImage3154Win32\FreeImage\Dist\FreeImage.dll
    9. 运行应用程序

    在包含Library项目的解决方案中有很多示例可供选择。

        2
  •  2
  •   Community CDub    4 年前

    你的详细步骤正是我所需要的。。。非常感谢。

    我在一个相关的 thread 。这是成功的,正如罗穆卢斯所说。

    以下是一些需要的额外细节和我遇到的错误(我希望不要太详细……希望能帮助其他遇到这种情况的人)。

    从第4步开始,我遵循了您的指示(只是我将其添加到了现有的windows应用程序中)。我将FreeImage.dll(步骤8)复制到我的项目的bin目录中。

    我运行了代码,得到了以下错误:

    WindowsFormsApplication1.exe中发生类型为“System.BadImageFormatException”的未处理异常

    附加信息:试图加载格式不正确的程序。(HRESULT中的异常:0x8007000B)

    我犯了两个错误。首先,我没有将FreeImage二进制dll(上面的第8步)复制到项目的正确bin目录中(我已将其复制到bin,而不是bin\Debug 8-)。我将其复制到:

    \Windows窗体应用程序1\bin\调试

    第二,如果我现在运行它,我会得到一个错误:

    WindowsFormsApplication1.exe中发生类型为“System.TypeInitializationException”的未处理异常

    其他信息:“WindowsFormsApplication1.Form1”的类型初始值设定项引发异常。

    这里的解决方案是将Build平台更改为x86。我发现选择x86或选中“首选32位”框的任何CPU都可以工作。给像我这样的初学者的提示:右键单击项目名称(而不是解决方案)并选择属性,即可访问构建平台。它也可以从DEBUG菜单访问,菜单底部将是“MyProject Properties”。 Build x86 without 32-bit preferred checked 或者这也起作用: Build with Any CPU selected AND Prefer 32-bit checked

    因此,通过这两个修复,代码运行良好。但是,文件输出为0KB。但那是另一个帖子。。。

    谢谢