【问题标题】:Access pixels in a 16-bit TIFF访问 16 位 TIFF 中的像素
【发布时间】:2013-05-01 01:39:26
【问题描述】:

我导入了一张 16 位灰度图像。我把它作为一个 BitmapSource 和一个 Image (属于 Controls 命名空间)。我如何访问单个像素?我读过的 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),但我不清楚如何访问一维字节数组中的单个像素。

方法二:

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();
           }
       }

谢谢, 丹

【问题讨论】:

    标签: c# image-processing tiff


    【解决方案1】:

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

    让您开始并举一个简单的例子:

    1. 下载二进制分发版
    2. 在visual studio中打开FreeImage.NET.sln(我用的是2010)
    3. 构建库项目
    4. 如果您在 XML cmets 中收到错误,请执行以下操作:进入项目属性,在构建下,将“将警告视为错误”从“全部”更改为“无”
    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. 运行应用程序

    在包含库项目的解决方案中有大量示例。

    【讨论】:

    • 我正在研究它...看起来很有希望!我会发布它是怎么回事。谢谢,丹佛。
    • 我整天都在搞这个,但无法让它在 Visual Studio 中工作。有什么帮助吗?我需要特定的步骤来将 FreeImage.NET 解决方案和 C++ DLL 加载到我现有的项目中。我是一个初学者,很想快速开始......
    • @DanG 我用一个示例更新了我的帖子以帮助您入门。
    • 代码真的有效吗?如果加载 jpg 而不是 tif,我可以让它工作。
    • @DanG 应该可以。我在几个项目中使用了带有 16 位 tiff 文件的 FreeImage。在发布之前,我还用一个代码测试了上面的代码:)
    【解决方案2】:

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

    我在相关的thread 中遵循了与您类似的安装说明(您的步骤 1-3)。这是成功的,正如罗穆卢斯所说的那样。

    下面是一些需要的额外细节和我遇到的错误(我希望它不是太多细节......希望能帮助遇到这个问题的其他人)。

    我从第 4 步开始按照您的指示进行操作(除了我将它添加到我现有的 Windows 应用程序中)。我将 FreeImage.dll(第 8 步)复制到项目的 bin 目录中。

    我运行代码,得到这个错误:

    WindowsFormsApplication1.exe 中出现“System.BadImageFormatException”类型的未处理异常

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

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

    \WindowsFormsApplication1\bin\Debug

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

    WindowsFormsApplication1.exe 中出现“System.TypeInitializationException”类型的未处理异常

    附加信息:“WindowsFormsApplication1.Form1”的类型初始化程序引发异常。

    这里的解决方案是将Build平台改为x86。我发现选择 x86 或任何带有 Prefer 32-bit 框的 CPU 都可以工作。给像我这样的初学者的提示:通过右键单击项目名称(不是解决方案)并选择属性来访问构建平台。它也可以从 DEBUG 菜单访问,底部是“MyProject Properties”。 或者这也有效:

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

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多