gpt4 book ai didi

c# - 基于锁定位图、getpixel 和优化的辅助函数提高视频校正算法的速度

转载 作者:行者123 更新时间:2023-11-30 16:39:37 25 4
gpt4 key购买 nike

我编写此代码是为了消除视频中因相机故障而导致的恼人模式。问题是要对 2 分钟的视频进行编码,此算法需要超过 2 小时。我想显着减少所需的时间。

该算法遍历每个图像,从那里开始查看每 4 个像素,创建平均值,如果平均值低于阈值,则将当前像素设置为白色。我可以使用 step = 2 并将 2x2 矩阵设置为白色,但这会降低图像质量并且只会将速度提高一半。

我已经添加了lockbitmaplockbits并改进了辅助功能。

使用 (d2 = reader.ReadVideoFrame()) 之前和之后-snippet 我有一个基于 ffmpeg 的 aforge videoreader 和 writer。

using (d2 = reader.ReadVideoFrame())
{
LockBitmap lockBitmap = new LockBitmap(d2);
lockBitmap.LockBits();

Color v = Color.FromArgb(240, 237, 241);
for (int x = 0; x < lockBitmap.Width-1; x = x + 1)
{
for (int y = 0; y < lockBitmap.Height-1; y = y + 1)
{
Color dus = durchschnitt(lockBitmap.GetPixel(x, y),
lockBitmap.GetPixel(x + 1, y),
lockBitmap.GetPixel(x, y + 1),
lockBitmap.GetPixel(x + 1, y + 1));
if (abstand(dus, v) < 50)
{
lockBitmap.SetPixel(x, y, Color.White);
}
}
}
lockBitmap.UnlockBits();
}

辅助功能:

private Color durchschnitt(Color c1, Color c2, Color c3, Color c4)
{
return Color.FromArgb((int)((c1.R + c2.R + c3.R + c4.R) / 4),
(int)((c1.G + c2.G + c3.G + c4.G) / 4),
(int)((c1.B + c2.B + c3.B + c4.B) / 4));
}

private double abstand(Color c1, Color c2)
{
return Math.Sqrt(Math.Pow(c2.R - c1.R, 2) + Math.Pow(c2.G - c1.G, 2) + Math.Pow(c2.B - c1.B, 2));
}

LockBitmap 来自 here .

最佳答案

这不是 lockBits 的工作方式。

简而言之,您需要锁定位才能通过指针访问扫描线。最好使用 32bpp 进行整数访问。您可以按如下方式计算连续数组中的像素。

你需要用 unsafe 装饰你的类或你的方法关键字并将项目构建选项设置为使用不安全代码,您使用指针。

var w = bmp.Width;
var h = bmp.Height;

// lock the array for direct access
var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
var scan0Ptr = (int*)data.Scan0;

// get the stride
var stride = data.Stride / 4;

// scan all x
for (var x = 0; x < w; x++)
{
var pX = scan0Ptr + x;

// scan all y
for (var y = 0; y < h; y++)
{
// this is now your pixel *p, which essentially is a pointer to
// to a memory which contains your pixel
var p = pX + y * stride;

// or for better access to X and Y
var p = scan0Ptr + x + y * stride;

// or alternatively you can just access you pixel with the following notation
*(scan0Ptr + x + y * stride) // <== just use this like any variable

// example how to convert pixel *p to a Color
var color = Color.FromArgb(*p);

// example Convert a Color back to *p and update the pixel
*p = Color.White.ToArgb();
}
}
// unlock the bitmap
bmp.UnlockBits(data);

我会将其余的细节留给您,但这将为您提供最佳性能(减去一些微优化)。

最后,减少对外部方法的调用也会提高性能;但是,如果您真的需要,您可以通过使用 MethodImplOptions Enum for AggressiveInlining

来帮助 CLR

例如

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Color durchschnitt(Color c1, Color c2, Color c3, Color c4)

此外,通过移出组件,您可能会获得性能提升:

var r = ((*p >> 16) & 255);
var g = ((*p >> 8) & 255);
var b = ((*p >> 0) & 255);

而且您可能会对该工作负载进行多线程处理。

关于c# - 基于锁定位图、getpixel 和优化的辅助函数提高视频校正算法的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798377/

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