gpt4 book ai didi

c# - 灰度图像的 MSE 计算

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:35 25 4
gpt4 key购买 nike

我有两张图片(原始的和嘈杂的)。我正在计算 PSNR。我有点为彩色 RGB 图像做了它,但我不知道如何用灰度来做。正如我所读,MSE 计算是不同的。对于 RGB,我的做法与您在以下代码中看到的一样(我使用的是 Visual C#):

for (int i = 0; i < bmp1.Width; i++)
{
for (int j = 0; j < bmp1.Height; j++)
{
mseR += Math.Pow(bmp1.GetPixel(i, j).R - bmp2.GetPixel(i, j).R, 2);
mseG += Math.Pow(bmp1.GetPixel(i, j).G - bmp2.GetPixel(i, j).G, 2);
mseB += Math.Pow(bmp1.GetPixel(i, j).B - bmp2.GetPixel(i, j).B, 2);

}
}
mse = (mseR + mseG + mseB) / ((bmp1.Width * bmp1.Height) * 3);

在这里,我正在处理 R、G、B 像素。但是我不知道在灰度图像的情况下我应该采取什么措施。我是否也可以使用 RGB,因为它实际上给出了一些结果,或者我应该采用其他方法?

最佳答案

要制作灰度,您可以使用平均值制作图片(无需改变您的实现)。我假设您的图像是 bmp1 = grayImage 和 bmp2 = 噪声图像。

for (int i = 0; i < bmp1.Width; i++)
{
for (int j = 0; j < bmp1.Height; j++)
{
// As a grayscale image has rthe same color on all RGB just pick one
int gray1 = bmp1.GetPixel(i, j).R;
int gray2 = bmp2.GetPixel(i, j).R;
double sum = Math.Pow(gray1 - gray2, 2)
mseGray += sum;
}
}
mse = (mseGray) / ((bmp1.Width * bmp1.Height) * 3);

同时一次获取一个像素是使用索引的缓慢过程,也是循环中的优化。它应该提供大约十倍的性能。

您需要将位图制作成可索引的 img,在本例中我假设它是 BitmapSource。有趣的部分是循环和索引构建而不是预编码,预编码只是为了使图像可索引。

var height = bmp1.Height;
var width = bmp1.Width;
var pixelBytes1 = new byte[height * width * 4];
var pixelBytes2 = new byte[height * width * 4];
bmp1.CopyPixels(pixelBytes1, stride, 0);
bmp2.CopyPixels(pixelBytes2, stride, 0);

for (int x = 0; x < width; x++)
{
int woff = x * height;
for (int y = 0; y < height; y++)
{(R*0.3 + G*0.59+ B*0.11)
int index = woff + y;
int gray1 = bmp1[index];
int gray2 = bmp2[index];
double sum = Math.Pow(gray1 - gray2, 2)
mseGray += sum;
}
}
mse = (mseGray) / ((bmp1.Width * bmp1.Height) * 3);

编辑:

http://www.mathworks.com/matlabcentral/answers/49906-how-to-calculate-psnr-of-compressed-images-and-how-to-compare-psnr-of-images-compressed-by-two-diff

我对你的 PSNR 实现有疑问,但我认为它不符合定义这是来自 java 的示例(与 C# 非常相似) http://www.cyut.edu.tw/~yltang/program/Psnr.java

关于c# - 灰度图像的 MSE 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com