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


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

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

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


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
[求助]图片亮度和对比度问题  发帖心情 Post By:2015/3/16 16:42:00 [只看该作者]

1.如何调整图片的亮度和对比度。
2.如何将图片去色,变成黑白。

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


加好友 发短信
等级:贵宾 帖子:35433 积分:178524 威望:0 精华:3 注册:2013/3/30 16:36:00
  发帖心情 Post By:2015/3/16 17:06:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | 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")


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


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

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

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


加好友 发短信
等级:六尾狐 帖子:1294 积分:9852 威望:0 精华:0 注册:2012/9/19 21:13:00
  发帖心情 Post By:2015/3/16 18:26:00 [只看该作者]

从网上找了个调亮度的代码,不知如何用

'|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'Author: Larry Nung
'Date: 2009/6/2
'File: 
'Memo: 
'|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public
			Class Form1
 
 
			'***************************************************************************
			'Author: Larry Nung
			'Date: 2009/6/2
			'Purpose: 
			'Memo: 
			'***************************************************************************
			''' <summary>
			''' Handles the Click event of the btnLoad control.
			''' </summary>
			''' <param name="sender">The source of the event.</param>
			''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
			''' <remarks></remarks>
			Private
			Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
			If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
			Me.PictureBox1.Image = New Bitmap(OpenFileDialog1.FileName)
			End
			If
			End
			Sub
 
 
			'***************************************************************************
			'Author: Larry Nung
			'Date: 2009/6/2
			'Purpose: 
			'Memo: 
			'***************************************************************************
			''' <summary>
			''' Brights the bitmap.
			''' </summary>
			''' <param name="bmp">The BMP.</param>
			''' <param name="brighValue">The brigh value.</param>
			''' <remarks></remarks>
			Private
			Function BrightBitmap(ByVal bmp As Bitmap, ByVal brighValue As
			Integer) As Bitmap
			For x As
			Integer = 0 To bmp.Width - 1
			For y As
			Integer = 0 To bmp.Height - 1
			Dim color As Color = bmp.GetPixel(x, y)
			Dim r As
			Integer = If(color.R + brighValue > 255, 255, color.R + brighValue)
			Dim g As
			Integer = If(color.G + brighValue > 255, 255, color.G + brighValue)
			Dim b As
			Integer = If(color.B + brighValue > 255, 255, color.B + brighValue)
                bmp.SetPixel(x, y, color.FromArgb(r, g, b))
			Next
			Next
			Return bmp
			End
			Function
 
			'***************************************************************************
			'Author: Larry Nung
			'Date: 2009/6/3
			'Purpose: 
			'Memo: 
			'***************************************************************************
			''' <summary>
			''' Handles the Click event of the btnBright control.
			''' </summary>
			''' <param name="sender">The source of the event.</param>
			''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
			''' <remarks></remarks>
			Private
			Sub btnBright_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBright.Click
        PictureBox1.Image = BrightBitmap(DirectCast(PictureBox1.Image, Bitmap), TrackBar1.Value)
			End
			Sub
End
			Class

图片旋转

VB.Net图像文件旋转变换的实例
作者:Ilu    来源:乐博网整理     更新时间:2009-8-21

VB.Net图像文件旋转变换的代码

乐博网lob.cn提示:此代码运行环境为vb.net2008

Public Class Form1
    '浏览图像文件
    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        Me.openFileDialog1.Filter = "图像文件(JPeg,Gif,Bmp,etc.)|*.jpg;*.jpeg;*.gif; *.bmp; *.tif; *.tiff; *.png| JPeg 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 文件 (*.gif)|*.gif |BMP 文件 (*.bmp)|*.bmp|Tiff 文件 (*.tif;*.tiff)|*.tif;*.tiff|Png 文件 (*.png)| *.png |所有文件(*.*)|*.*"
        If (Me.openFileDialog1.ShowDialog() = DialogResult.OK) Then
            Me.textBox1.Text = Me.openFileDialog1.FileName
            Dim MyImage = Image.FromFile(Me.textBox1.Text)
            Me.pictureBox1.Image = MyImage
        End If
    End Sub
    '旋转变换    多更.net源代码  请关注lob.cn
    Private Sub button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button6.Click
        Dim MyImage = Image.FromFile(Me.textBox1.Text)
        Dim MyAttributes As New System.Drawing.Imaging.ImageAttributes()
        Dim MyWidth = MyImage.Width
        Dim MyHeight = MyImage.Height
        Dim degrees = 60.0F
        Dim r = degrees * System.Math.PI / 180
        Dim MyElements As Single()() = {New Single() {CType(System.Math.Cos(r), Single), CType(System.Math.Sin(r), Single), 0, 0, 0}, New Single() {-System.Math.Sin(r), -System.Math.Cos(r), 0, 0, 0}, New Single() {0, 0, 2, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}}
        Dim MyMatrix As New System.Drawing.Imaging.ColorMatrix(MyElements)
        MyAttributes.SetColorMatrix(MyMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap)
        Me.pictureBox1.CreateGraphics().DrawImage(MyImage, New Rectangle(0, 0, MyWidth, MyHeight), 0, 0, MyWidth, MyHeight, GraphicsUnit.Pixel, MyAttributes)
    End Sub
End Class

[此贴子已经被作者于2015/3/16 18:36:37编辑过]

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


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

看3楼


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


加好友 发短信
等级:七尾狐 帖子:1791 积分:12764 威望:0 精华:1 注册:2013/7/18 15:51:00
  发帖心情 Post By:2015/3/17 16:13:00 [只看该作者]

1.如何获取当前图片的亮度和对比度值?
2.如何将当前图片由彩色变成黑白?

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | 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")

 

 


 回到顶部