## CSDN博客

### VB图像处理，（六）图像的亮度对比度调整

如图中直线A所表示的，角T为45度，表示它的对比度正好为1。

NewRed = (OldRed -127 ) * A + 127＋ B
NewGreen = (OldGreen -127 ) * A + 127＋B
NewBlue = (OldBlue -127 ) * A + 127＋B

Y=( X - 127 ) * A + B + 127  =>  Y = X * A - 127 * A + 127＋B      (1)
令：B = B -127 * A ＋127       (2)

Public Sub BrightnessAndContrast(ByVal RedOffset As Long, ByVal GreenOffset As Long, ByVal BlueOffset As Long, Optional ByVal RedContrast As Single = 1, Optional ByVal GreenContrast As Single = 1, Optional ByVal BlueContrast As Single = 1)
Dim X As Long
Dim Y As Long
Dim MidR As Integer
Dim MidG As Integer
Dim MidB As Integer
Dim Max As Long

On Error GoTo ErrLine
Done = False
TimeFilter = timeGetTime
MidR = RedOffset - 127 * (RedContrast - 1)        '计算新的位移量B
MidG = GreenOffset - 127 * (GreenContrast - 1)
MidB = BlueOffset - 127 * (BlueContrast - 1)
Max = 255
For X = 0 To OutPutWid
For Y = 0 To OutPutHei
R = ColOut(2, X, Y)
G = ColOut(1, X, Y)
B = ColOut(0, X, Y)
R = R * RedContrast + MidR      '计算Y = X * A + B
G = G * GreenContrast + MidG
B = B * BlueContrast + MidB
If R > Max Then R = Max           '输出值判断是否在0到255之间
If R < 0 Then R = 0
If G > Max Then G = Max
If G < 0 Then G = 0
If B > Max Then B = Max
If B < 0 Then B = 0
ColOut(2, X, Y) = R
ColOut(1, X, Y) = G
ColOut(0, X, Y) = B
Next
Next
Done = True
TimeFilter = timeGetTime - TimeFilter
Exit Sub
ErrLine:
MsgBox Err.Description
Done = True
End Sub

（这里只是说了我自己在写程序的时候用到的方法，存在很多的不足。并且因为在贴上来的时候作了部分修改，可能会存在部分错误，请各位高手不吝赐教，将您用到的更好的方法提供一下，我将不胜感激。）

0 0