是否有转换位图的像素为灰度更快的方法? [英] Is there a faster method to convert bitmap pixels to greyscale?

查看:192
本文介绍了是否有转换位图的像素为灰度更快的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用的与setPixel()方法来改变每个像素的位图的颜色。这工作得很好小的图像尺寸小,但是当我测试它在大型图像它确实需要一段时间。

我还没有在VB.Net图像之前工作过,所以我可能只是忽视的东西明显。我这样做是为了使程序将图像转换为灰度。这将产生正确的结果,但在较低的速度,在此期间,用户界面​​冻结,所以我热衷于最大限度地提高转换的速度。

这是我的code的时刻:

 昏暗tmpImg作为新位图(IMG)'IMG是原始图像的参考
当x为整数= 0〜tmpImg.Width - 1
    y的作为整数= 0〜tmpImg.Height - 1
        昏暗的CLR为字节
        与tmpImg.GetPixel(X,Y)
            CLR = ConvertToGrey(.R,.G,.B)
        结束与
        tmpImg.SetPixel(X,Y,Color.FromArgb(CLR,CLR,CLR))
    下一个
下一个专用功能ConvertToGrey(BYVALř为字节,BYVAL G系列因此字节,BYVAL b以字节)作为字节
    返回(0.2126 * R)+(0.7152 * B)+(0.0722 * G)的
结束功能


解决方案

快速是相对而言的,但这将转换为小480x270图像会在10-12毫秒(显然取决于系统),它似乎并不太长灰度。我敢肯定它会比快的setPixel

 专用功能GrayedImage(orgBMP作为位图)作为位图    昏暗的灰度作为新Imaging.ColorMatrix(新单曲()()_
        {新单(){0.3,0.3,0.3,0,0},
         新单曲(){0.59,0.59,0.59,0,0},
         新单(){0.11,0.11,0.11,0,0},
         新单(){0,0,0,1,0},
         新单(){0,0,0,0,1}})    昏暗bmpTemp作为新位图(orgBMP)
    昏暗grayattr作为新Imaging.ImageAttributes()
    grayattr.SetColorMatrix(灰度)    定义g作为图形= Graphics.FromImage(bmpTemp)
        g.DrawImage(bmpTemp,新的Rectangle(0,0,bmpTemp.Width,bmpTemp.Height)
                    0,0,bmpTemp.Width,bmpTemp.Height,
                    GraphicsUnit.Pixel,grayattr)
    使用完    返回bmpTemp
结束功能

数值将从0.299,587,0.114四舍五入

At the moment, I am using the SetPixel() method to change the colour of every pixel in a bitmap. This works fine on small images with small dimensions, but when I test it on large images it does take a while.

I haven't worked with images in VB.Net before, so I might just be overlooking something obvious. I'm doing this to make a program which converts an image to grey scale. This produces the right result but at a low speed, and during this time the UI freezes, so I'm keen to maximize the speed of conversion.

This is my code at the moment:

Dim tmpImg As New Bitmap(img) '"img" is a reference to the original image 
For x As Integer = 0 To tmpImg.Width - 1
    For y As Integer = 0 To tmpImg.Height - 1
        Dim clr As Byte
        With tmpImg.GetPixel(x, y)
            clr = ConvertToGrey(.R, .G, .B)
        End With
        tmpImg.SetPixel(x, y, Color.FromArgb(clr, clr, clr))
    Next
Next

Private Function ConvertToGrey(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Byte
    Return (0.2126 * R) + (0.7152 * B) + (0.0722 * G)
End Function

解决方案

Fast is a relative term, but this will convert a 480x270 image to greyscale in 10-12 ms (obviously system dependent) which does not seem unduly long. I'm quite sure it will be faster than SetPixel.

Private Function GrayedImage(orgBMP As Bitmap) As Bitmap

    Dim grayscale As New Imaging.ColorMatrix(New Single()() _
        {New Single() {0.3, 0.3, 0.3, 0, 0},
         New Single() {0.59, 0.59, 0.59, 0, 0},
         New Single() {0.11, 0.11, 0.11, 0, 0},
         New Single() {0, 0, 0, 1, 0},
         New Single() {0, 0, 0, 0, 1}})

    Dim bmpTemp As New Bitmap(orgBMP)
    Dim grayattr As New Imaging.ImageAttributes()
    grayattr.SetColorMatrix(grayscale)

    Using g As Graphics = Graphics.FromImage(bmpTemp)
        g.DrawImage(bmpTemp, New Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height), 
                    0, 0, bmpTemp.Width, bmpTemp.Height, 
                    GraphicsUnit.Pixel, grayattr)
    End Using

    Return bmpTemp
End Function

Values are rounded from .299, .587, .114

这篇关于是否有转换位图的像素为灰度更快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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