vb.net 将RGB转换为灰度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17216883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting RGB to Gray scale?
提问by dirty_sanchez
I am new to Visual Basic, I've done image processing in matlab in the past. But require Image Processing in Visual Basic as of this moment. Okay, I've been able to display the image and read up on converting to grayscale. However my image is in jpeg format and i keep running into the Bitmap function for only bmp images in several grayscale converter tutorials and my code keep producing errors for attempts in manipulation for JPEG format. How do i read in the jpeg and perform the grayscale manipulation. Here is the code.
我是 Visual Basic 的新手,过去我在 matlab 中做过图像处理。但目前需要在 Visual Basic 中进行图像处理。好的,我已经能够显示图像并阅读转换为灰度。但是,我的图像是 jpeg 格式,并且在几个灰度转换器教程中,我一直运行仅用于 bmp 图像的位图功能,并且我的代码不断产生错误以尝试操作 JPEG 格式。我如何读取 jpeg 并执行灰度操作。这是代码。
Public Class Form1
Private Sub showButton_Click(sender As System.Object, e As System.EventArgs) Handles showButton.Click
' Show the Open File dialog. If the user clicks OK, load the
' picture that the user chose.
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Load(OpenFileDialog1.FileName)
End If
End Sub
Private Sub GrayImageButton_Click(sender As System.Object, e As System.EventArgs) Handles GrayImageButton.Click
Dim bm As New jpeg(PictureBox1.Image)
Dim X As Integer
Dim Y As Integer
Dim clr As Integer
For X = 0 To bm.Width - 1
For Y = 0 To bm.Height - 1
clr = (CInt(bm.GetPixel(X, Y).R) + _
bm.GetPixel(X, Y).G + _
bm.GetPixel(X, Y).B) \ 3
bm.SetPixel(X, Y, Color.FromArgb(clr, clr, clr))
Next Y
Next X
PictureBox1.Image = bm
End Sub
The error I'm recieving is
我收到的错误是
Error1 : Value of type 'WindowsApplication1.jpeg' cannot be converted to 'System.Drawing.Image'.
错误 1:“WindowsApplication1.jpeg”类型的值无法转换为“System.Drawing.Image”。
When I implement this with a bmp image it works perfectly, but not with a jpeg. I will be grateful for any help with this issue. Thanks
当我使用 bmp 图像实现它时,它可以完美运行,但不能使用 jpeg。对于这个问题的任何帮助,我将不胜感激。谢谢
回答by Idle_Mind
Just change:
只是改变:
Dim bm As New jpeg(PictureBox1.Image)
To:
到:
Dim bm As New Bitmap(PictureBox1.Image)
It would be faster, though, to use a ColorMatrix like this:
但是,使用这样的 ColorMatrix会更快:
Private Sub GrayImageButton_Click(sender As System.Object, e As System.EventArgs) Handles GrayImageButton.Click
Dim grayscale As New Imaging.ColorMatrix(New Single()() _
{ _
New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1} _
})
Dim bmp As New Bitmap(PictureBox1.Image)
Dim imgattr As New Imaging.ImageAttributes()
imgattr.SetColorMatrix(grayscale)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _
0, 0, bmp.Width, bmp.Height, _
GraphicsUnit.Pixel, imgattr)
End Using
PictureBox1.Image = bmp
End Sub
回答by David Freitag
Where is jpeg defined? Is that a VB.Net Library you are using? Or have you written this object yourself?
jpeg 在哪里定义?那是您正在使用的 VB.Net 库吗?或者你自己写了这个对象?
.Net has some built-in Jpeg utilities, you may want to check this out:
.Net 有一些内置的 Jpeg 实用程序,你可能想看看这个:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.jpegbitmapdecoder.aspx
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.jpegbitmapdecoder.aspx
With this you should be able to access the pixels, from there your background in color manipulation should allow you to use this to do exactly what you are looking to do.
有了这个,您应该能够访问像素,从那里您的颜色操作背景应该允许您使用它来完成您想要做的事情。
回答by Douglas Barbin
I would suggest looking at an imaging library like AForge or OpenCV. They have lots of useful functions built-in (several different RGB-to-Grayscale algorithms, for example). OpenCV is written in C++ so it's probably going to be faster than anything you can write in VB. I'm not sure about AForge, but I think it is written in C#.
我建议查看像 AForge 或 OpenCV 这样的成像库。它们内置了许多有用的功能(例如,几种不同的 RGB 到灰度算法)。OpenCV 是用 C++ 编写的,所以它可能比你用 VB 编写的任何东西都要快。我不确定 AForge,但我认为它是用 C# 编写的。
回答by RobS
There is no such type as jpegin vb.net so the line:
jpeg在 vb.net 中没有这样的类型,所以该行:
Dim bm As New jpeg(PictureBox1.Image)
should be replaced by
应该替换为
Dim bm as Bitmap = New Bitmap(PictureBox1.image)
回答by Jason Dine
Here is a good code
这是一个很好的代码
Sub BlackAndWhite() Dim x As Integer Dim y As Integer Dim red As Byte Dim green As Byte Dim blue As Byte For x = 0 To I.Width - 1 For y = 0 To I.Height - 1 red = I.GetPixel(x, y).R green = I.GetPixel(x, y).G blue = I.GetPixel(x, y).B I.SetPixel(x, y, Color.FromArgb(blue, blue, blue)) Next Next PictureBox1.Image = I End Sub
Sub BlackAndWhite() Dim x As Integer Dim y As Integer Dim red As Byte Dim green As Byte Dim blue As Byte For x = 0 To I.Width - 1 For y = 0 To I.Height - 1 red = I.GetPixel( x, y).R green = I.GetPixel(x, y).G blue = I.GetPixel(x, y).B I.SetPixel(x, y, Color.FromArgb(blue, blue, blue)) 接下来下一个 PictureBox1.Image = I End Sub

