首页 文章

访问16位TIFF中的像素

提问于
浏览
1

我导入了一个灰度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;
        }
    }
}

我认为这可能更简单(找到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 回答

  • 2

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

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

    • 下载二进制发行版

    • 在visual studio中打开FreeImage.NET.sln(我用过2010)

    • 构建库项目

    • 如果您在XML注释中收到错误,请执行以下操作:进入项目属性,在构建下,将"treat warnings as errors"从"all"更改为"none"

    • 创建新的控制台项目 .

    • 添加对在步骤3中构建的程序集的引用 .
      FreeImage3154Win32 \的FreeImage \包装\ FreeImage.NET \ CS \图书馆\ BIN \调试\ FreeImageNET.dll

    • 将以下代码添加到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);
           }
        }
    

    }

    • 将您下载的FreeImage二进制文件复制到bin目录中 .
      FreeImage3154Win32 \的FreeImage \ DIST \ FreeImage.dll

    • 运行应用程序

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

  • 2

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

    我在相关的thread中按照与您类似的指示进行安装(您的步骤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 \ Debug

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

    WindowsFormsApplication1.exe中出现未处理的“System.TypeInitializationException”类型的异常附加信息:“WindowsFormsApplication1.Form1”的类型初始化程序引发了异常 .

    这里的解决方案是将Build平台更改为x86 . 我找到了选择x86,或任何带有Prefer 32位盒检查工作的CPU . 像我这样的初学者的提示:通过右键单击项目名称(而不是解决方案)并选择属性来访问构建平台 . 它也可以从DEBUG菜单访问,底部的位置为"MyProject Properties" .
    Build x86 without 32-bit preferred checked
    或者这也有效:
    Build with Any CPU selected AND Prefer 32-bit checked

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

    谢谢!

相关问题