- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写此代码是为了消除视频中因相机故障而导致的恼人模式。问题是要对 2 分钟的视频进行编码,此算法需要超过 2 小时。我想显着减少所需的时间。
该算法遍历每个图像,从那里开始查看每 4 个像素,创建平均值,如果平均值低于阈值,则将当前像素设置为白色。我可以使用 step = 2 并将 2x2 矩阵设置为白色,但这会降低图像质量并且只会将速度提高一半。
我已经添加了lockbitmap
、lockbits
并改进了辅助功能。
在使用 (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
例如
[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/
我正在比较工作簿中的工作表。该工作簿有两张名为 PRE 和 POST 的工作表,每张工作表都有相同的 19 列。行数每天都不同,但特定一天的两张表的行数相同。该宏将 PRE 工作表中的每一行与 POS
我有一个对象数组,我一次循环遍历该数组一个对象,然后进行几次检查以查看该数组中的每个对象是否满足特定条件,如果该对象满足此条件,则复制一个属性将此对象放入数组中(该属性还包含另一个对象)。 for(v
我正在编写一个必须非常快的应用程序。我使用 Qt 5.5 和 Qt Creator,Qt 的 64 位 MSVC2013 编译版本。 我使用非常困倦的 CS 来分析我的应用程序,我看到占用最多独占时间
我有以下 CountDownTimer 在我的 Android 应用程序中不断运行。 CountDownTimer timer_status; timer_status = new CountDown
有一个优化问题,我必须调用随机森林回归器的预测函数数千次。 from sklearn.ensemble import RandomForestRegressor rfr = RandomForestR
我正在努力提高现有 Asp.Net Web 应用程序的数据访问层的性能。场景是。 它是一个基于 Web 的 Asp.Net 应用程序。 数据访问层使用 NHibernate 1.2 构建并作为 WCF
我在我的 Intel Edison 上运行 Debian,并尝试使用 ffmpeg 通过 USB 网络摄像头捕获视频。我正在使用的命令是: ffmpeg -f video4linux2 -i /dev
我有一个 For循环遍历整数 1 到 9 并简单地找到与该整数对应的最底部的条目(即 1,1,1,2,3,4,5 将找到第三个“1”条目)并插入一个空白行。我将数字与仅对应于此代码的应用程序的字符串“
我有一个带有非规范化架构(1 个表)的 postgresql 数据库,其中包含大约 400 万个条目。现在我有这个查询: SELECT count(*) AS Total, (SELECT c
在 Ltac 中实现复杂的策略时,有一些 Ltac 命令或策略调用我预计会失败以及预期失败(例如终止 repeat 或导致回溯)。这些故障通常在故障级别 0 时引发。 更高级别引发的故障“逃避”周
我正在尝试提高 ansible playbook 的性能。我有一个测试剧本如下: --- - name: Test hosts: localhost connection: local g
我正在使用 axios从 Azure 存储 Blob 下载文件 (~100MB)。 axios({ method: 'get', url: uri, onDownloadProgress:
我有一个 ClojureScript 程序,主要对集合执行数学计算。它是在惯用的、独立于主机的 Clojure 中开发的,因此很容易对其进行基准测试。令我惊讶的是(与答案对 Which is fast
我有一个程序必须在硬件允许的情况下尽快发出数千个 http 请求。在现实世界中,这些连接中的每一个都将连接到一个离散的服务器,但我已经编写了一个测试程序来帮助我模拟负载(希望如此)。 我的程序使用 A
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在计算 Clojure 中 3d 点云的边界框。点云表示为 Java 原始浮点数组,点云中的每个点都使用 4 个浮点存储,其中最后一个浮点未使用。像这样: [x0 y0 z0 u0 x1 y1
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在尝试使用rayshader 包制作图像。我很高兴能够使用如下代码创建一个 png 文件: library(ggplot2) library(rayshader) example_plot <-
更新 显然,jQuery 模板可以被编译,并且它有助于显示带有 if 语句 的模板的性能 here . 但是如图here ,预编译的 jQuery 模板对我的情况没有多大作用,因为我的模板不包含逻辑
我是编程新手。我有一个启用分页的 ScrollView ,其中包含许多页面(最多十个),并且在每个页面上都有一个自定义按钮。每个自定义按钮都有一个自定义图像。我在 Interface Builder
我是一名优秀的程序员,十分优秀!