问题描述
我导入了一个灰度16位图像。我把它作为BitmapSource和一个Image(控件命名空间)。我如何访问各个像素? CopyPixels我读过唯一或最好的方法吗?如果是这样,我不知道如何设置步幅,以及哪个像素包含像素强度值。
I have imported a greyscale 16-bit image. I have it bot as a BitmapSource and an Image (of Controls namespace). How can i access the individual pixels? Is the CopyPixels i have read about the only or best way? If so, i don't get how to set the stride, and which pixel contains the pixel intensity value.
方法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;
}
}
}
我认为这可能更简单(找到),但后来我不清楚如何访问1D中的单个像素字节数组。
I think this might be simpler (found here), but then i am unclear how to access individual pixels in a 1D byte array.
方法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();
}
}
谢谢,
Dan
Thanks,Dan
推荐答案
您可能想要查看免费图片库。
它有一个C#包装器。
You might want to check out the free image library.It has a C# wrapper.http://freeimage.sourceforge.net/index.html
为了帮助您入门并提供一个简单示例:
To get you started and give a quick example:
- 下载二进制分发
- 在visual studio中打开FreeImage.NET.sln(我使用2010)
- 构建库项目
- 如果你接收XML注释中的错误执行以下操作:进入项目属性,在构建下,将将警告视为错误从全部更改为无
- 创建新的控制台项目。
- 添加对在步骤3中构建的程序集的引用。
FreeImage3154Win32\FreeImage \Wrapper\FreeImage.NET\cs\Library\bin \Debug \FreeImageNET.dll -
将以下代码添加到Program.cs
- download the binary distribution
- open FreeImage.NET.sln in visual studio (I used 2010)
- build the Library project
- if you receive errors in the XML comments do the following: go in the project properties, under build, change "treat warnings as errors" from "all" to "none"
- Create new console project.
- Add reference to the assembly you built in step 3.
FreeImage3154Win32\FreeImage\Wrapper\FreeImage.NET\cs\Library\bin\Debug\FreeImageNET.dll Add the following code to 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);
}
}
}
解决方案中有很多包含库项目的示例。
There is a good selection of examples in the solution that contained the Library project.
这篇关于访问16位TIFF中的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!