对于位图较快对比算法 [英] Faster contrast algorithm for a bitmap

查看:137
本文介绍了对于位图较快对比算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用来调整图像的亮度,对比度,伽玛等的TrackBar滑块控制的工具。

I have a tool with trackbar slider controls used to adjust an image's brightness, contrast, gamma, etc.

我想获得实时更新到我的形象,而在用户拖动滑块。亮度和伽马算法是可以接受的速度(约170ms)。但是对比算法约为380ms。

I am trying to get real-time updates to my image while the user drags the slider. The brightness and gamma algorithms are an acceptable speed (around 170ms). But the contrast algorithm is about 380ms.

基本上我的形式是一个工具窗口滑块。每个图像被更新时,它会发送一个事件,它重绘新形象的父。该工具的窗口都会锁定在内存中的原始未修改图像,所以我总是有机会获得它的字节。所以基本上我这样做,每次ValueChanged事件的滑块(如对比度滑块)被改变。

Basically my form is a tool window with sliders. Each time the image is updated, it sends an event to the parent which redraws the new image. The tool window keeps the original unmodified image locked in memory so I always have access to the bytes of it. So basically I do this each time the ValueChanged event for a slider (such as the Contrast slider) is changed.


  • 工作(目标)位图作为Format24bppRgb的LockBits(原始位是Format32bppPArgb)

  • Marshal.Copy位为byte []数组

  • 检查我在做这些操作(这是选择滑块)

  • 使用以下code的对比:

code:

double newValue = 0;
double c = (100.0 + contrast) / 100.0;

c *= c;

for (int i = 0; i < sourcePixels.Length; i++)
{
    newValue = sourcePixels[i];

    newValue /= 255.0;
    newValue -= 0.5;
    newValue *= c;
    newValue += 0.5;
    newValue *= 255;

    if (newValue < 0)
        newValue = 0;
    if (newValue > 255)
        newValue = 255;

    destPixels[i] = (byte)newValue;
}

我如何使用整数,而不是浮点值,以增加对比度的速度读取一次,但我无法再次找到那篇文章。

I read once about using integer instead of floating point values to increase the speed of contrast, but I couldn't find that article again.

我尝试使用不安全code(指针),但实际上发现的速度下降。我想那是因为code,采用嵌套的for循环迭代x和y,而不是一个单一的循环。

I tried using unsafe code (pointers) but actually noticed a speed decrease. I assume it was because the code was using nested for loops to iterate x and y instead of a single loop.

推荐答案

根据不同的机器上所运行这一点,你的技术可能会相当缓慢。如果您使用的是ARM系统没有FPU,每这些行动将需要相当长一段时间。既然你申请相同的操作,以每一个字节,更快的方法是创建一个256项查找表的对比度水平,然后每个图像字节通过表翻译。那么你的循环将如下所示:

Depending on the machine you're running this on, your technique could be quite slow. If you're using an ARM system without an FPU, each of those operations would take quite a while. Since you're applying the same operation to every byte, a faster technique would be to create a 256-entry lookup table for the contrast level and then translate each image byte through the table. Your loop would then look like:

byte contrast_lookup[256];
double newValue = 0;
double c = (100.0 + contrast) / 100.0;

c *= c;

for (int i = 0; i < 256; i++)
{
    newValue = (double)i;
    newValue /= 255.0;
    newValue -= 0.5;
    newValue *= c;
    newValue += 0.5;
    newValue *= 255;

    if (newValue < 0)
        newValue = 0;
    if (newValue > 255)
        newValue = 255;
    contrast_lookup[i] = (byte)newValue;
}

for (int i = 0; i < sourcePixels.Length; i++)
{
    destPixels[i] = contrast_lookup[sourcePixels[i]];
}

这篇关于对于位图较快对比算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆