繁体   English   中英

访问16位TIFF中的像素

[英]Access pixels in a 16-bit TIFF

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

方法一:

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

我认为这可能更简单(在这里找到),但后来我不清楚如何访问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();
           }
       }

谢谢,丹

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

为了帮助您开始并提供一个快速示例:

  1. 下载二进制发行版
  2. 在visual studio中打开FreeImage.NET.sln(我使用2010)
  3. 构建库项目
  4. 如果您在XML注释中收到错误,请执行以下操作:进入项目属性,在构建下,将“将警告视为错误”从“全部”更改为“无”
  5. 创建新的控制台项目。
  6. 添加对在步骤3中构建的程序集的引用。
    FreeImage3154Win32 \\的FreeImage \\包装\\ FreeImage.NET \\ CS \\图书馆\\ BIN \\调试\\ 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二进制文件复制到bin目录中。
    FreeImage3154Win32 \\的FreeImage \\ DIST \\ FreeImage.dll
  9. 运行该应用程序

包含Library项目的解决方案中有很多示例。

你的详细步骤非常需要我...非常感谢。

我在相关的主题中按照与您类似的指示进行安装(您的步骤1-3)。 这是成功的,正如罗穆卢斯所说。

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

我按照你从第4步开始的指示(除了我将它添加到我的exisitng windows应用程序中)。 我将FreeImage.dll(步骤8)复制到我项目的bin目录中。

我运行代码,并得到此错误:

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

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

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

\\ WindowsFormsApplication1 \\ BIN \\调试

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

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

附加信息:'WindowsFormsApplication1.Form1'的类型初始值设定项引发了异常。

这里的解决方案是将Build平台更改为x86。 我找到了选择x86,或任何带有Prefer 32位盒检查工作的CPU。 像我这样的初学者的提示:通过右键单击项目名称(而不是解决方案)并选择属性来访问构建平台。 它也可以从DEBUG菜单访问,其底部是“MyProject Properties”。 构建x86而不检查32位首选项 或者这也有效: 在选择任何CPU的情况下构建并优先选择32位

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

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM