|
I try this on 256-colored pictures and have no result.
What can you advice me?
|
|
|
|
|
I noticed this on GIF pictures.
So, I decided to promote the image, if it isn't in the Format32bppARGB Pixel Format, to this Pixel Format, and it worked just fine.
That's the code I use:
Public Shared Function PictureConvertToRGB(ByVal bmp As Bitmap) As Bitmap
Dim img As New Bitmap(bmp.Width, bmp.Height, _
Imaging.PixelFormat.Format32bppArgb)
img.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution)
Using gr As Graphics = Graphics.FromImage(img)
gr.DrawImageUnscaled(bmp, 0, 0)
End Using
Return img
End Function
Give it a trial: it will surely work for you, too.
<div class="ForumMod">modified on Monday, May 26, 2008 7:38 AM</div>
|
|
|
|
|
Any ideas on how to implement...
Currently working on a project which requires hue, looks as if this could do it, somehow!
Help would be greatly appreciated.
|
|
|
|
|
' Matrix Operations for Image Processing
' Converted from C to VB.NET By Headkaze (headkaze [at] gmail.com)
' Thanks to Paul Haeberli
' http://www.sgi.com/misc/grafica/matrix/
Option Explicit On
Option Strict On
Imports System.Drawing.Imaging
Module ColourProcessing
Const RLUM As Single = 0.3086
Const GLUM As Single = 0.6094
Const BLUM As Single = 0.082
' MultiplyMatrix - multiply two matricies
Public Sub MultiplyMatrix(ByRef a As ColorMatrix, ByRef b As ColorMatrix, ByRef c As ColorMatrix)
Dim x, y As Integer
Dim temp As New ColorMatrix
For y = 0 To 4
For x = 0 To 4
temp(y, x) = b(y, 0) * a(0, x) _
+ b(y, 1) * a(1, x) _
+ b(y, 2) * a(2, x) _
+ b(y, 3) * a(3, x) _
+ b(y, 4) * a(4, x)
Next
Next
For y = 0 To 4
For x = 0 To 4
c(y, x) = temp(y, x)
Next
Next
End Sub
' CreateIdentityMatrix - make an identity matrix
Public Sub CreateIdentityMatrix(ByRef matrix As ColorMatrix)
matrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
End Sub
' Transform3dPointMatrix - transform a 3D point using a matrix
Public Sub Transform3dPointMatrix(ByRef matrix As ColorMatrix, ByVal x As Single, ByVal y As Single, ByVal z As Single, ByRef tx As Single, ByRef ty As Single, ByRef tz As Single)
tx = x * matrix(0, 0) + y * matrix(1, 0) + z * matrix(2, 0) + matrix(3, 0)
ty = x * matrix(0, 1) + y * matrix(1, 1) + z * matrix(2, 1) + matrix(3, 1)
tz = x * matrix(0, 2) + y * matrix(1, 2) + z * matrix(2, 2) + matrix(3, 2)
End Sub
' ColourScaleMatrix - make a color scale matrix
Public Sub ColourScaleMatrix(ByRef mat As ColorMatrix, ByVal rscale As Single, ByVal gscale As Single, ByVal bscale As Single)
Dim nmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rscale, 0, 0, 0, 0}, _
New Single() {0, gscale, 0, 0, 0}, _
New Single() {0, 0, bscale, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(nmat, mat, mat)
End Sub
' LuminanceMatrix - make a luminance matrix
Public Sub LuminanceMatrix(ByRef mat As ColorMatrix)
Dim rwgt, gwgt, bwgt As Single
rwgt = RLUM
gwgt = GLUM
bwgt = BLUM
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rwgt, rwgt, rwgt, 0, 0}, _
New Single() {gwgt, gwgt, gwgt, 0, 0}, _
New Single() {bwgt, bwgt, bwgt, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' SaturateMatrix - make a saturation matrix
Public Sub SaturateMatrix(ByRef mat As ColorMatrix, ByVal sat As Single)
Dim a, b, c, d, e, f, g, h, i As Single
Dim rwgt, gwgt, bwgt As Single
rwgt = RLUM
gwgt = GLUM
bwgt = BLUM
a = CSng((1.0# - sat) * rwgt + sat)
b = CSng((1.0# - sat) * rwgt)
c = CSng((1.0# - sat) * rwgt)
d = CSng((1.0# - sat) * gwgt)
e = CSng((1.0# - sat) * gwgt + sat)
f = CSng((1.0# - sat) * gwgt)
g = CSng((1.0# - sat) * bwgt)
h = CSng((1.0# - sat) * bwgt)
i = CSng((1.0# - sat) * bwgt + sat)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {a, b, c, 0, 0}, _
New Single() {d, e, f, 0, 0}, _
New Single() {g, h, i, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' OffsetMatrix - offset r, g, and b
Public Sub OffsetMatrix(ByRef mat As ColorMatrix, ByVal roffset As Single, ByVal goffset As Single, ByVal boffset As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {roffset, goffset, boffset, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' XRotateMatrix - rotate about the x (red) axis
Public Sub XRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, rc, rs, 0, 0}, _
New Single() {0, -rs, rc, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' YRotateMatrix - rotate about the y (green) axis
Public Sub YRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rc, 0, -rs, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {rs, 0, rc, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' ZRotateMatrix - rotate about the z (blue) axis
Public Sub ZRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rc, rs, 0, 0, 0}, _
New Single() {-rs, rc, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' ZShearMatrix - shear z using x and y.
Public Sub ZShearMatrix(ByRef mat As ColorMatrix, ByVal dx As Single, ByVal dy As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, dx, 0, 0}, _
New Single() {0, 1, dy, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' HueRotateMatrix - rotate the hue, while maintaining luminance.
Public Sub HueRotateMatrix(ByRef mat As ColorMatrix, ByVal rot As Single)
Dim mmat As ColorMatrix
Dim mag As Single
Dim lx, ly, lz As Single
Dim xrs, xrc As Single
Dim yrs, yrc As Single
Dim zrs, zrc As Single
Dim zsx, zsy As Single
CreateIdentityMatrix(mmat)
' rotate the grey vector into positive Z
mag = CSng(Math.Sqrt(2.0))
xrs = CSng(1.0# / mag)
xrc = CSng(1.0# / mag)
XRotateMatrix(mmat, xrs, xrc)
mag = CSng(Math.Sqrt(3.0#))
yrs = CSng(-1.0# / mag)
yrc = CSng(Math.Sqrt(2.0#) / mag)
YRotateMatrix(mmat, yrs, yrc)
' shear the space to make the luminance plane horizontal
Transform3dPointMatrix(mmat, RLUM, GLUM, BLUM, lx, ly, lz)
zsx = lx / lz
zsy = ly / lz
ZShearMatrix(mmat, zsx, zsy)
' rotate the hue
zrs = CSng(Math.Sin(rot * Math.PI / 180.0#))
zrc = CSng(Math.Cos(rot * Math.PI / 180.0#))
ZRotateMatrix(mmat, zrs, zrc)
' unshear the space to put the luminance plane back
ZShearMatrix(mmat, -zsx, -zsy)
' rotate the grey vector back into place
YRotateMatrix(mmat, -yrs, yrc)
XRotateMatrix(mmat, -xrs, xrc)
MultiplyMatrix(mmat, mat, mat)
End Sub
Public Function DrawImageWithMatrix(ByVal img As Image, ByVal cm As ColorMatrix) As Boolean
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
Dim imgattr As New ImageAttributes
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr)
g.Dispose()
Return True
Catch
Return False
End Try
End Function
End Module
|
|
|
|
|
What are the range of values that can be passed into for the HueRotateMatrix Value parameter? -180 to 180? -1 to 1? 0 to 360?
|
|
|
|
|
HueRotateMatrix() takes 0 to 360 (0 being normal)
SaturateMatrix() takes 0 to 2 (1 being normal)
ColourScaleMatrix() (Brightness) takes 0 to 255 (I think, havn't tried it yet)
Check this link for more information
http://www.sgi.com/misc/grafica/matrix/[^]
|
|
|
|
|
Sub OffsetMatrix change:
Dim mmat as ColorMatrix = _
New ColorMatrix(New Single()() _
{New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
New Single() {roffset, goffset, boffset, 0.0, 1.0}})
|
|
|
|
|
Public Function Translucent(ByVal img As Image) As Boolean
Dim cm As New Imaging.ColorMatrix(New Single()() { _
New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, 0.4F, 1.0F}})
Return draw_adjusted_image(img, cm)
End Function
Thanks,
Michaelas10;)
|
|
|
|
|
Hay There,
This article looks to have what i need. I copied the code to get started and now i get an error Type expected
My code looks like:
Imports System.Drawing.Imaging
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub
#End Region
Public Function translate(ByVal img As Image, ByVal red As Single, _
ByVal green As Single, ByVal blue As Single, _
Optional ByVal alpha As Single = 0) As Boolean
Dim sr, sg, sb, sa As Single
' noramlize the color components to 1
sr = red / 255
sg = green / 255
sb = blue / 255
sa = alpha / 255
' create the color matrix
Dim cm As New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {sr, sg, sb, sa, 1}})
' apply the matrix to the image
Return draw_adjusted_image(img, cm)
End Function
End Class
VB.NET tells me that the the type expected error is under ColorMatrix
Can anyone tell me what i'am doing wrong?
|
|
|
|
|
Hi BigBertB,
you're missing the path to ColorMatrix. Either use an imports-statement (Imports System.Drawing.Drawing2D) at the top of your class or precede the full path when declaring your cm-variable.
BTW - in cases like this, hit F2 (Object Browser) and locate an object. Use the "Find Symbol"-command to search for the object you don't know the path to (either by Edit/Find and Replace/Find Symbol or using the commandbar's button on the RHS)
Cheers,
Olaf
|
|
|
|
|
Imports System.Drawing.Imaging.ColorMatrix
|
|
|
|
|
Michael,
This is a great (if not the better!) article for color adjustment. Thank You a lot for share it with us.
Best ragards
Khe ps
Merry Christmas
|
|
|
|
|
Good article, I understand what's going on, but how on earth do you impliment it. I've been playing with it for about an hour and I still can't get it to display the image with the new Colormatrix...
|
|
|
|
|
Michael,
Thank you for the excellent article.
I've noticed a little issue with the way you define your negative matrix. With your definition of the matrix pure black colors (0,0,0,x) will not be inverted. Some other strange color "artifacts" could be produced as well.
I believe that a better negative could be produced by subtracting current color components from 255 (from 1 in the matrix world). To achieve this you should scale components by -1 and then translate them by 1. Following matrix should do the trick:
{-1, 0, 0, 0, 0}
{ 0, -1, 0, 0, 0}
{ 0, 0, -1, 0, 0}
{ 0, 0, 0, 1, 0}
{ 1, 1, 1, 0, 1}
Thank you.
|
|
|
|
|
Yeah, I came up with the same matrix you did. It does produce a better looking negative image.
|
|
|
|
|
With the original definition all the values where out by 1, so when a color should have been 255 it was 0. For some reason when the values in the last row are 0 the results from the matrix are rolled over (eg 256 becomes 0) but when the values in the last row are no zero the results are clamped (eg 256 becomes 255). I'm not sure if this is a bug or feature but the original example shouldn't work at all because the colors all come out to be negative so should really be clamped at zero.
|
|
|
|
|
Hi
i have tried this code. its working okay. i have a question. when i change the color of picture by giving red, green, blue color values. it displays the new picture on the picturebox. but when i minimize the form and then maximize it, the picture is no more there.
how can i save the changed picture because i need to store this new changed picture in database.
thanks a lot in advance.
bye
|
|
|
|
|
It really depends on how you are using the code. It sounds like you are updating an image using the image property of a picturebox. I would suggest loading the image from disk into an image object and then making modifications to that object. You can then resave the image to disk, display the results in a picturebox, etc. Otherwise, I would suggest reading up on the paint related events and properties of a picturebox control.
-Michael Combs
|
|
|
|
|
hey Michael
sorry, actually i m new to VB.NET. i have worked much on Java. now i m switching to VB.NET just to change taste
anyway, can u write a small code to save this picture.
i hope u will not mind it
thanks a lot in advance
|
|
|
|
|
The Bitmap class inherits from Image, and Image has a Save method. That should be enough.
Basic example:
<br />
Public Sub SaveImage(ByVal img As System.Drawing.Image, ByVal filename As String)<br />
img.Save(filename)<br />
End Sub<br />
|
|
|
|
|
Is there way, using matrix functions, to compute the average R, G and B values for a image. I have 10s or 1000s of images that I need to calculate avg RGB for and looping through each pixel is very slow.
|
|
|
|
|
Not as a whole. You can change each individual pixel to the average of r, g and b
|
|
|
|
|
Have you looked into the lockbits function? This is very fast. If you're using getpixel you will notice a huge speed improvement.
|
|
|
|
|
Hi,
I had try the code n it works fine. However, is there a way to manipulate the pixel/images ( as in change a 24 bit image to 8 bit image or select all the even number of pixel bit...etc) in Vb.net and how??
|
|
|
|
|
|