c#中多张图片平均去噪
Noise Removal by Averaging Multiple Images in c#
我尝试了两张图片的代码,但在这个限制之后它失败了..并且没有得到正确的解决方案以及您可以访问的图片集
Click Here For collection of images
在这些图像中,绿色图像上的数字是噪声
请帮我找到这些解决方案
我试过遵循代码
public Bitmap GetAveragedBitmap()
{
int numberOfImages = _imageCollection.Count;
Bitmap bmpresult = null;
if (numberOfImages > 1)
{
Stopwatch clock = new Stopwatch();
clock.Start();
BitmapData[] bmpData = new BitmapData[numberOfImages];
Bitmap[] bit = new Bitmap[numberOfImages];
int width = 0;
int height = 0;
for (int index = 0; index < numberOfImages; index++)
{
bmpData[index] = _imageCollection[index].LockBits(new Rectangle(0, 0, _imageCollection[index].Width, _imageCollection[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
if (index > 0)
{
if (bmpData[index].Width > width)
{
width = bmpData[index].Width;
}
if (bmpData[index].Height > height)
{
height = bmpData[index].Height;
}
}
else
{
width = bmpData[0].Width;
height = bmpData[0].Height;
}
_imageCollection[index].UnlockBits(bmpData[index]);
}//end of for loop
for (int index = 0; index < numberOfImages; index++)
{
bit[index] = new Bitmap(_imageCollection[index], width, height);
}
bmpresult = new Bitmap(width, height);
BitmapData[] data = new BitmapData[numberOfImages];
for (int index = 0; index < numberOfImages; index++)
{
data[index] = bit[index].LockBits(new Rectangle(0, 0, bit[index].Width, bit[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
}
data[1].Reserved = 1;
BitmapData dataResult = bmpresult.LockBits(new Rectangle(0, 0, bmpresult.Width, bmpresult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
int[] remain = new int[numberOfImages];
byte*[] ptr = new byte*[numberOfImages];
long totalIntensity = 0;
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] = (byte*)data[index].Scan0;
remain[index] = data[index].Stride - data[index].Width * 3;
}
byte* ptrResult = (byte*)dataResult.Scan0;
//for resultant image
int remainResult = dataResult.Stride - dataResult.Width * 3;
for (int i = 0; i < height; i++)
for (int j = 0; j < width * 3; j++)
{
for (int index = 0; index < numberOfImages; index++)
{
totalIntensity += (int)(ptr[index][0]);
ptr[index]++;
}
}
totalIntensity= totalIntensity/ numberOfImages;
int average = (int)totalIntensity / (numberOfImages * height * width);
//reset the pointer
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] = (byte*)data[index].Scan0;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width * 3; j++)
{
int result = 1;
for (int k = 0; k < numberOfImages; k++)
{
result = (ptr[k][0] * 1 * result);
}
ptrResult[0] = (byte)(result / average);
for (int index = 0; index < numberOfImages; index++)
{
ptr[index]++;
}
ptrResult++;
}
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] += remain[index];
}
ptrResult += remainResult;
}
// }//end of for loop
}//end of unsafe
for (int index = 0; index < numberOfImages; index++)
{
bit[index].UnlockBits(data[index]);
}
bmpresult.UnlockBits(dataResult);
clock.Stop();
Debug.WriteLine("Time for average " + clock.ElapsedMilliseconds.ToString());
}
return bmpresult;
}
我做到了,
我去寻找 AForge 框架的想法,幸运的是有过滤器 AForge.NET Framework Morph Class 可以从两个图像创建平均值。我认为最初的意图是将两个不同的图像变形为一种效果,但是如果您使用同一场景的两个镜头,您可以获得降噪效果。
我假设您想要 2-n 平均,所以我建议您以 Merge 过滤器的源代码为起点,并为多个图像创建您的自定义版本。
这是来源:GitHub.com | AForge.NET / Merge.cs
PS。您可能需要下载整个 AForge 项目源代码,以找到更好地放置您的实现的位置。
我尝试了两张图片的代码,但在这个限制之后它失败了..并且没有得到正确的解决方案以及您可以访问的图片集 Click Here For collection of images
在这些图像中,绿色图像上的数字是噪声 请帮我找到这些解决方案
我试过遵循代码
public Bitmap GetAveragedBitmap()
{
int numberOfImages = _imageCollection.Count;
Bitmap bmpresult = null;
if (numberOfImages > 1)
{
Stopwatch clock = new Stopwatch();
clock.Start();
BitmapData[] bmpData = new BitmapData[numberOfImages];
Bitmap[] bit = new Bitmap[numberOfImages];
int width = 0;
int height = 0;
for (int index = 0; index < numberOfImages; index++)
{
bmpData[index] = _imageCollection[index].LockBits(new Rectangle(0, 0, _imageCollection[index].Width, _imageCollection[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
if (index > 0)
{
if (bmpData[index].Width > width)
{
width = bmpData[index].Width;
}
if (bmpData[index].Height > height)
{
height = bmpData[index].Height;
}
}
else
{
width = bmpData[0].Width;
height = bmpData[0].Height;
}
_imageCollection[index].UnlockBits(bmpData[index]);
}//end of for loop
for (int index = 0; index < numberOfImages; index++)
{
bit[index] = new Bitmap(_imageCollection[index], width, height);
}
bmpresult = new Bitmap(width, height);
BitmapData[] data = new BitmapData[numberOfImages];
for (int index = 0; index < numberOfImages; index++)
{
data[index] = bit[index].LockBits(new Rectangle(0, 0, bit[index].Width, bit[index].Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
}
data[1].Reserved = 1;
BitmapData dataResult = bmpresult.LockBits(new Rectangle(0, 0, bmpresult.Width, bmpresult.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
int[] remain = new int[numberOfImages];
byte*[] ptr = new byte*[numberOfImages];
long totalIntensity = 0;
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] = (byte*)data[index].Scan0;
remain[index] = data[index].Stride - data[index].Width * 3;
}
byte* ptrResult = (byte*)dataResult.Scan0;
//for resultant image
int remainResult = dataResult.Stride - dataResult.Width * 3;
for (int i = 0; i < height; i++)
for (int j = 0; j < width * 3; j++)
{
for (int index = 0; index < numberOfImages; index++)
{
totalIntensity += (int)(ptr[index][0]);
ptr[index]++;
}
}
totalIntensity= totalIntensity/ numberOfImages;
int average = (int)totalIntensity / (numberOfImages * height * width);
//reset the pointer
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] = (byte*)data[index].Scan0;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width * 3; j++)
{
int result = 1;
for (int k = 0; k < numberOfImages; k++)
{
result = (ptr[k][0] * 1 * result);
}
ptrResult[0] = (byte)(result / average);
for (int index = 0; index < numberOfImages; index++)
{
ptr[index]++;
}
ptrResult++;
}
for (int index = 0; index < numberOfImages; index++)
{
ptr[index] += remain[index];
}
ptrResult += remainResult;
}
// }//end of for loop
}//end of unsafe
for (int index = 0; index < numberOfImages; index++)
{
bit[index].UnlockBits(data[index]);
}
bmpresult.UnlockBits(dataResult);
clock.Stop();
Debug.WriteLine("Time for average " + clock.ElapsedMilliseconds.ToString());
}
return bmpresult;
}
我做到了,
我去寻找 AForge 框架的想法,幸运的是有过滤器 AForge.NET Framework Morph Class 可以从两个图像创建平均值。我认为最初的意图是将两个不同的图像变形为一种效果,但是如果您使用同一场景的两个镜头,您可以获得降噪效果。
我假设您想要 2-n 平均,所以我建议您以 Merge 过滤器的源代码为起点,并为多个图像创建您的自定义版本。
这是来源:GitHub.com | AForge.NET / Merge.cs
PS。您可能需要下载整个 AForge 项目源代码,以找到更好地放置您的实现的位置。