Foxtable(狐表)用户栏目专家坐堂 → [求助]图片亮度和对比度问题


  共有4359人关注过本帖树形打印复制链接

主题:[求助]图片亮度和对比度问题

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2015/3/16 17:58:00 [显示全部帖子]

亮度

 

Dim degree As Integer = 50 '-255 到 255
Dim Width As Integer, Height As Integer
Dim Pic As Bitmap = New Bitmap(getimage("d:\test.jpg"))
Width = Pic.Width
Height = Pic.Height
Dim rect As New Rectangle(0, 0, Width, Height)
Dim bmpData As BitmapData = Pic.LockBits(rect, ImageLockMode.ReadWrite, Pic.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0

Dim offset As Integer = bmpData.Stride - bmpData.Width * 4
For j As Integer = 0 To Height - 1
    For i As Integer = 0 To Width - 1
        For k As Integer = 0 To 2
            Dim pix As Integer = Marshal.ReadByte(ptr, k)
            pix = pix + degree
            If pix < 0 Then pix = Math.Max(0, pix)
            If pix > 0 Then pix = Math.Min(255, pix)
            Marshal.WriteByte(ptr, k, cByte(pix))
        Next
        ptr = CType(ptr.ToInt32 + 4, IntPtr)
    Next
    ptr = CType(ptr.ToInt32 + offset, IntPtr)
Next

Pic.UnlockBits(bmpData)
pic.Save("d:\test3.jpg")

 

 

----------------

 

对比度

 

Dim degree As Integer = -20 '-100 到 100

Dim contrast As  Double  = (100 + degree) / 100.0
contrast *= contrast

Dim Width As Integer, Height As Integer
Dim Pic As Bitmap = New Bitmap(getimage("d:\test.jpg"))
Width = Pic.Width
Height = Pic.Height
Dim rect As New Rectangle(0, 0, Width, Height)
Dim bmpData As BitmapData = Pic.LockBits(rect, ImageLockMode.ReadWrite, Pic.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0

Dim offset As Integer = bmpData.Stride - bmpData.Width * 4
For j As Integer = 0 To Height - 1
    For i As Integer = 0 To Width - 1
        For k As Integer = 0 To 2
            Dim pix As Integer = Marshal.ReadByte(ptr, k)
            pix = ((pix / 255 - 0.5) * contrast + 0.5) * 255
            If pix < 0 Then pix = Math.Max(0, pix)
            If pix > 0 Then pix = Math.Min(255, pix)
            Marshal.WriteByte(ptr, k, cByte(pix))
        Next
        ptr = CType(ptr.ToInt32 + 4, IntPtr)
    Next
    ptr = CType(ptr.ToInt32 + offset, IntPtr)
Next

Pic.UnlockBits(bmpData)
pic.Save("d:\test3.jpg")


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2015/3/16 17:59:00 [显示全部帖子]

 汗...对图片处理不熟......

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2015/3/16 18:59:00 [显示全部帖子]

看3楼


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2015/3/17 16:26:00 [显示全部帖子]

 1、你用变量记录

 

 2、

 


Dim Width As Integer, Height As Integer
Dim Pic As Bitmap = New Bitmap(getimage("d:\test.jpg"))
Width = Pic.Width
Height = Pic.Height
Dim rect As New Rectangle(0, 0, Width, Height)
Dim bmpData As BitmapData = Pic.LockBits(rect, ImageLockMode.ReadWrite, Pic.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0

Dim offset As Integer = bmpData.Stride - bmpData.Width * 4
For j As Integer = 0 To Height - 1
    For i As Integer = 0 To Width - 1

            Dim R As Integer = Marshal.ReadByte(ptr, 0)
            Dim G As Integer = Marshal.ReadByte(ptr, 1)
            Dim B As Integer = Marshal.ReadByte(ptr, 2)
            Dim avg As Integer = (R + G + B) / 3
            Marshal.WriteByte(ptr, 0, cByte(avg))
            Marshal.WriteByte(ptr, 1, cByte(avg))
            Marshal.WriteByte(ptr, 2, cByte(avg))

        ptr = CType(ptr.ToInt32 + 4, IntPtr)
    Next
    ptr = CType(ptr.ToInt32 + offset, IntPtr)
Next

Pic.UnlockBits(bmpData)
pic.Save("d:\test3.jpg")

 

 


 回到顶部