C# Windows 7上的位图像素值与XP上的不同
我有一个Windows窗体应用程序,我正在升级该应用程序,以实现更新的.Net Framework。在我的一个单元测试中,使用灰度TIFF图像创建位图。提取像素数据,然后通过算法确定图像中基准点的中心。在Windows XP、Visual Studio 2005、.Net Framework 2.0上,找到了正确的中心,并且测试通过。在64位Windows 7、Visual Studio 2013、.Net Framework 4.0上,测试失败,因为它发现的中心有少量关闭 通过调试和查看字节数组,我在一台机器和另一台机器上检查了像素数据。XP机器上的像素值与Windows 7机器上的像素值略有不同。两台机器上的图像和代码完全相同。另一件事是,这只发生在灰度图像上,所有的彩色图像都通过,我检查的那些具有相同的像素值。最后,根据调试时检查的位图属性,所讨论的图像的宽度和高度似乎完全相同 以下是Windows XP机器的前10个像素值:C# Windows 7上的位图像素值与XP上的不同,c#,.net,windows,bitmap,C#,.net,Windows,Bitmap,我有一个Windows窗体应用程序,我正在升级该应用程序,以实现更新的.Net Framework。在我的一个单元测试中,使用灰度TIFF图像创建位图。提取像素数据,然后通过算法确定图像中基准点的中心。在Windows XP、Visual Studio 2005、.Net Framework 2.0上,找到了正确的中心,并且测试通过。在64位Windows 7、Visual Studio 2013、.Net Framework 4.0上,测试失败,因为它发现的中心有少量关闭 通过调试和查看字节数
[0] 94 byte
[1] 92 byte
[2] 92 byte
[3] 91 byte
[4] 92 byte
[5] 94 byte
[6] 93 byte
[7] 92 byte
[8] 90 byte
[9] 91 byte
[10] 91 byte
以下是Windows 7计算机的前10个值:
[0] 85 byte
[1] 102 byte
[2] 85 byte
[3] 85 byte
[4] 85 byte
[5] 85 byte
[6] 102 byte
[7] 85 byte
[8] 85 byte
[9] 85 byte
[10] 85 byte
以下是测试方法:
public void Test_CenteringBottomMiddle8BitMono()
{
int centerX, centerY;
DoCenter(path + "BottomMiddle - 8 - Mono.tif", out centerX, out centerY, "npf02");
CheckOffsets(centerX, centerY, 0, centerOffsetPixels);
}
和DoCenter方法:
private void DoCenter(string filename, out int centerX, out int centerY, string fiducialType)
{
if (File.Exists(filename))
{
Bitmap bmp = new Bitmap(filename);
CenterCalibration center = new CenterCalibration();
byte[] bytes = ImageProcessing.GetByteStream(bmp);
int numChan = 1;
if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
numChan = 3;
}
center.GetCenterPoint(bytes, bmp.Width, bmp.Height, numChan, out centerX, out centerY, fiducialType);
Console.WriteLine("{0} (Center Coords): {1},{2}", filename, centerX, centerY);
}
else
{
string msg = string.Format("File: {0} Does Not Exist.", filename);
throw new FileNotFoundException(msg);
}
}
以及GetByTestStream方法:
public static byte[] GetByteStream(Bitmap bmp)
{
DateTime startFunctionTimer = DateTime.Now;
DateTime endFunctionTimer;
DateTime startTimer, endTimer;
startTimer = DateTime.Now;
int numChan = 1;
if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
numChan = 3;
}
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
byte[] dst = new byte[bmp.Width * bmp.Height * numChan];
byte[] src = new byte[bmpData.Stride * bmp.Height];
startTimer = DateTime.Now;
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, src, 0, dst.Length);
for (int i = 0; i < bmp.Height; i++)
{
Buffer.BlockCopy(src, (i * bmpData.Stride), dst, (i * bmp.Width * numChan), bmp.Width * numChan);
}
endTimer = DateTime.Now;
bmp.UnlockBits(bmpData);
TimeSpan functionInterval, timerInterval;
timerInterval = endTimer - startTimer;
endFunctionTimer = DateTime.Now;
functionInterval = endFunctionTimer - startFunctionTimer;
// Console.WriteLine("GetByteStream() Total = {0}, CopyTimer = {1}", functionInterval.TotalMilliseconds, timerInterval.TotalMilliseconds);
return dst;
}
我尝试过的事情:
在较新的计算机上以原始.Net Framework为目标。
在创建位图对象时打开和关闭颜色校正。
确认两台机器上的像素格式相同。格式索引
我很确定这与像素值不同有关,而不是与实际找到基准点的算法有关,但我可能错了。我首先想知道为什么像素值显示的不同
谢谢你的帮助/线索
**更新**
由于TaW在评论中的建议,我取得了一些进展。他问TIF是否被压缩
当我用IrfanViewer查看TIF信息时,它显示它正在使用LZW压缩。我保存了一份有关灰度TIF的副本,并选择不压缩图像。当我用这个图像调试单元测试时,我能够看到我希望的值,并且单元测试通过了。似乎压缩正在影响像素值,在我的新设置中,有些东西的工作方式有点不同
有趣的是,这种像素值的差异似乎不会发生在彩色图像上,即使它们也是用LZW压缩的
对于新的设置可能会有所不同的原因,是否有进一步的想法?听起来很奇怪。TIF压缩了吗?您是否控制/检查了ColorContexts颜色配置文件?谢谢!我已经用调查结果更新了我的问题。我仍然不确定颜色背景,但压缩是一个很好的建议。嗯,现在这更奇怪了。TIF LZW应该是无损的;我只是为了完整而问。颜色配置文件(OTOH)是使显示机器依赖的一种方法。。在像素值方面;并且希望机器在外观上独立。如果您在一台机器上安装了颜色配置文件,这可能是原因;即使彩色图像似乎没有受到影响。