|
Thanks for this! But there seem to be a couple typos:
the negative function should be :
Dim cm 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() {0, 0, 0, 1, 0}, _
New Single() {1, 1, 1, 1, 1}})
you had 0,0,0,0,1 in the last line
and in the Translate function it should be
Dim cm As ColorMatrix = New ColorMatrix
not
Dim New ColorMatrix
Cheers!
|
|
|
|
|
This code really help me.
bravo
|
|
|
|
|
Note 10.
|
|
|
|
|
Where is the demo ? I test demo first before reading the article. Don't want to waste time on non-related thing.
|
|
|
|
|
|
I have an error "A Graphics object cannot be created from an image that has an index". any ideas?
|
|
|
|
|
Best of all,
is there anyone who can share a complete example in an .aspx file so that I can really learn about your scripts ?
I think to use the ColorMatrix, the code of the .aspx file must contain this kind of code :
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Page.IsPostBack Then
Dim oBitmap As Bitmap = New Bitmap(468, 60)
Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
.......
MANY THANKS, I TRIED SINCE MANY DAYS AND DON'T KNOW HOW TO USE, SO THANK YOU ALL 5I'm not a coder, I'm a pofessional photographer and I would like to see my picture in grayscale in my webpage, that's all. All the time I'm trying to learn vbscript I can not work my normal job) Tx, Alain
|
|
|
|
|
I want to create a disabled image in Office 2007 style. It's a kind of sephia image with a blue color key.
In photoshop i can archive this by following tasks:
1. Use The Colorize Dialog with Hue 214, Saturation 10
2. Set the transparency to 50%
How would the ColorMatrix look like in this case?
Greets Daniel
|
|
|
|
|
In Sub OffsetMatrix, the variables roffset, goffset, and boffset are associated with the 'Alpha' elements. Should be associated with the 'W' elements as follows:
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}})
|
|
|
|
|
I just thought I would post to point out a slight error (unsure if it is in the download, I have only been using the code in the article)
the colormatrix is created like this:
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} _
})
However after experimenting and a look here (msdn)[^]
I noticed the alpha channel should be here (row 4 and column 4):
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, sa, 0}, _
New Single() {sr, sg, sb, 0, 1} _
})
Other than that, excellent article!
|
|
|
|
|
Hi Michael,
congratulations for this great article!
I'm neither a professional programmer nor a picture specialist, but still help in a project for digital restoration of a historical movie. One of the (many) problems is "flash frames", which are single frames (usually right before or after a scene cut) that are completely damaged in terms of colors. Coming from a C-Reality film scanner, the file format btw. is 10-bit DPX (a bitmap format).
When I read your article, I was thinking if the ColorMatrix could open an elegant way to resolve the problem... e.g. if it was possible to read (or get somehow) the color profile (ColorMatrix?) from a neighbor frame and apply it to the "flash frame". From your article I couldn't figure if the ColorMatrix can also be read from an existing picture. Would you think ColorMatrix's could be a good way? In case you see a chance (I don't want to be bold here) do you probably have an idea HOW to do that with VB 20008 (Express)???
Thank you very much,
regards from Germany and
have a nice day
Michael
modified on Sunday, September 21, 2008 9:35 AM
|
|
|
|
|
Hi, all.
Does anyone have a ColorMatrix to make an image use only 2 colors (Black and White)?
The effect should be similar to that of a fax print out.
Possibly, passing in a "quality" (threshold) parameter.
Let's say the average ((r + g + b) /3) of the color components is lower or equal to 128 *, the result will be 0 for all the components.
Else, it would be 255 (1, in the ColorMatrix "language").
* 128 (0.5 is my standard threshold).
Maybe, it can be done in two passages, first converting the image to gray and then applying a "threshold filter", to call it so?
Bob Powell (Converting an RGB image to 1 bit-per-pixel monochrome) succeeded in, but:
1. He uses unmanaged APIs.
2. He uses InterOps.
3. He does the thing one pixel at a time...
Bob is really an experienced developer, buy this "naive" solution is totally unacceptable for me.
Is there anyone that can do the same by using a ColorMatrix?
Or an ImageAttributes?
Or a combination of the two?
modified on Monday, May 26, 2008 7:22 AM
|
|
|
|
|
Sorry, but I had to reply myself, since I did find a "not too bad" solution, and it might be that someone of you finds it useful for their needs.
Well, I ended up with a two steps solution.
I'm still waiting to find a single ColorMatrix that could do that on the fly...
Also, I noticed that some Images don't pruduce a TRUE B/W, but there's still some color bleeding.
This happens with some PNG Images.
I think it's due to a "dirty" Alpha Channel, but still can't help.
Maybe, a solution could be calling the ImageBlackAndWhite Method twice, or to put a For cicle inside it to repeat the two steps twice.
' The Methods I use to do the Conversions:
Friend Sub ImageBlackAndWhite(ByVal img As Image, _
ByVal Threshold As Int32)
' This Method returns the Image in pure Black & White Colors.
' Very much like a Fax Print out.
' Two Steps are required, and the Order is meaningful.
' Adjust the Threshold Value.
If Threshold < 0 Then Threshold = 0
If Threshold > 100 Then Threshold = 100
' This is a two Step Operation:
' First Step:
' Convert the Image to GrayScale.
ImageGrayScale(img)
' Second Step:
' Set a Threshold to the Image.
ImageThreshold(img, Threshold)
End Sub
Friend Sub ImageGrayScale(ByVal img As Image)
' This Method converts an Image to GrayScale.
' Create the Color Matrix.
'' SimpleAverage GrayScale Matrix.
'Dim cm As New ColorMatrix(New Single()() { _
' New Single() {0.333F, 0.333F, 0.333F, 0, 0}, _
' New Single() {0.333F, 0.333F, 0.333F, 0, 0}, _
' New Single() {0.333F, 0.333F, 0.333F, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
'' WeightAverage GrayScale Matrix.
'Dim cm As New ColorMatrix(New Single()() { _
' New Single() {0.333F, 0.333F, 0.333F, 0, 0}, _
' New Single() {0.444F, 0.444F, 0.444F, 0, 0}, _
' New Single() {0.222F, 0.222F, 0.222F, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
'' Bob Powell's GrayScale Matrix.
'Dim cm As ColorMatrix = New ColorMatrix(New Single()() { _
' New Single() {0.5, 0.5, 0.5, 0, 0}, _
' New Single() {0.5, 0.5, 0.5, 0, 0}, _
' New Single() {0.5, 0.5, 0.5, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
'' Gilles Khouzam's Colour corrected Shear GrayScale Matrix.
'Dim cm As ColorMatrix = New ColorMatrix(New Single()() { _
' New Single() {0.3, 0.3, 0.3, 0, 0}, _
' New Single() {0.59, 0.59, 0.59, 0, 0}, _
' New Single() {0.11, 0.11, 0.11, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
'' Luminance Vector GrayScale Matrix.
'' This is the one Microsoft prefers.
'Dim cm As New ColorMatrix(New Single()() { _
' New Single() {0.3086F, 0.3086F, 0.3086F, 0, 0}, _
' New Single() {0.6094F, 0.6094F, 0.6094F, 0, 0}, _
' New Single() {0.082F, 0.082F, 0.082F, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
'' NtscPal (CCIR Rec. 604 standard) GrayScale Matrix.
'Dim cm As New ColorMatrix(New Single()() { _
' New Single() {0.299F, 0.299F, 0.299F, 0, 0}, _
' New Single() {0.587F, 0.587F, 0.587F, 0, 0}, _
' New Single() {0.114F, 0.114F, 0.114F, 0, 0}, _
' New Single() {0, 0, 0, 1, 0}, _
' New Single() {0, 0, 0, 0, 1}})
' CCIR Rec. 709 standard GrayScale Matrix.
' This one is the most used.
Dim cm As New ColorMatrix(New Single()() { _
New Single() {0.213F, 0.213F, 0.213F, 0, 0}, _
New Single() {0.715F, 0.715F, 0.715F, 0, 0}, _
New Single() {0.072F, 0.072F, 0.072F, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
' Apply the Matrix to the Image.
DrawImage(img, cm)
End Sub
Friend Sub ImageThreshold(ByVal img As Image, _
ByVal Threshold As Int32)
' This Method sets an Image Threshold.
' Adjust Threshold Value.
If Threshold < 0 Then Threshold = 0
If Threshold > 100 Then Threshold = 100
' 0.01F * x is faster than x / 100
Dim tsh As Single = 0.01F * Threshold
' Create an ImageAttributes Object, and set its Bitmap Threshold.
Dim ia As New ImageAttributes
ia.SetThreshold(tsh, ColorAdjustType.Bitmap)
' Apply the ImageAttributes to the Image.
DrawImage(img, ia)
End Sub
' I also use these Overloads:
Private Overloads Sub DrawImage(ByVal img As Image, _
ByVal cm As ColorMatrix)
' Draw an Image with a specified ColorMatrix Object.
Try
' Create a Copy of the Source Image.
Dim bmp As New Bitmap(img)
Dim ia As New ImageAttributes
Dim rect As New Rectangle(0, 0, img.Width, img.Height)
Using gr As Graphics = Graphics.FromImage(img)
' Associate the ColorMatrix Object with an ImageAttributes
' Object.
ia.SetColorMatrix(cm)
' Draw the Copy of the Source Image back over the Original
' Image, applying the ColorMatrix.
gr.DrawImage(bmp, rect, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, ia)
End Using
Catch
' Eat the Exception.
End Try
End Sub
Private Overloads Sub DrawImage(ByVal img As Image, _
ByVal ia As ImageAttributes)
' Draw an Image with a specified ImageAttributes Object.
Try
' Create a Copy of the Source Image.
Dim bmp As New Bitmap(img)
Dim rect As New Rectangle(0, 0, img.Width, img.Height)
Using gr As Graphics = Graphics.FromImage(img)
' Draw the Copy of the Source Image back over the Original
' Image, applying the ImageAttributes.
gr.DrawImage(bmp, rect, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, ia)
End Using
Catch
' Eat the Exception.
End Try
End Sub
' Somewhere in the Form I call these Instructions and do the Trick:
' Load the Image from the original one, which is stored in PictureBox1.
PictureBox7.Image = CType(PictureBox1.Image.Clone, Image)
' Apply the Transformation with a Thresold of 50%.
.ImageBlackAndWhite(PictureBox7.Image, 50)
<div class="ForumMod">modified on Tuesday, May 27, 2008 7:04 AM</div>
|
|
|
|
|
hi
can u help me converting this code in vb.net and then how to apply it
thanks
Aamir Mustafa
|
|
|
|
|
What language do you think it is?
On top of my contribution, the methods I used, and near the end, you'll find a statement like "somewhere in the code ..." - that's how to use the above code.
Of course, it's all meant to be used inside the existing project code, just as an addition.
Luca Crisi, MCP
|
|
|
|
|
I know your article is quite old but it seems most relevant to what I am trying to do.
I have been searching for how to write an image in Lab format instead of RGB, I have converted the RGB values into a decimal array (decimal for precision) but now I just dont know what to do with them - how do I write them back to a file in the format of Lab ?
|
|
|
|
|
Hi..
Plz tell me how to apply this code...
Thanks
|
|
|
|
|
help,Michael.
How to do softlight on an image?
What are the Values to pass for the matrix ?
Is there some releated algorithms to do on the pixel?
Any help is appreciated, Thank you!
|
|
|
|
|
Hi Michael
I know it's a bit long since you have done this article but I am having a problem saving the adjusted image.
I display the image and a copy. I adjust the copy using a matrix, using a copy of the source. It shows great on the screen but I am having problems actually saving the image with the matrix applied. All it does is save the copy without the matrix values applied.
This displays the source image
Private Function draw_source_image(ByVal g As Graphics) As Boolean<br />
<br />
Try<br />
Dim bmp As New Bitmap(sourceImage, 256, 256) ' draw the source image <br />
Dim rc As New Rectangle(150, 50, sourceImage.Width, sourceImage.Height)<br />
<br />
g.DrawImage(bmp, rc, 0, 0, sourceImage.Width, sourceImage.Height, _<br />
GraphicsUnit.Pixel)<br />
Return True<br />
Catch<br />
Return False<br />
End Try<br />
<br />
End Function<br />
This displays the adjusted image
Private Function draw_adjusted_image(ByVal cm As ColorMatrix, ByVal g As Graphics) As Boolean<br />
<br />
Try<br />
adjustedImage = New Bitmap(sourceImage, 256, 256) ' create a copy of the source image <br />
Dim imgattr As New ImageAttributes()<br />
Dim rc As New Rectangle(450, 50, sourceImage.Width, sourceImage.Height)<br />
<br />
' associate the ColorMatrix object with an ImageAttributes object<br />
imgattr.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Default)<br />
<br />
'applying the ColorMatrix<br />
g.DrawImage(adjustedImage, rc, 0, 0, sourceImage.Width, sourceImage.Height, _<br />
GraphicsUnit.Pixel, imgattr)<br />
<br />
Return True<br />
Catch<br />
Return False<br />
End Try<br />
<br />
End Function<br />
This saves the adjusted image to disc. I'm not too worried about the size yet as I can adjust that before the save.
Private Sub saveImage()<br />
'here we save the image to file<br />
Dim mDiag As New SaveFileDialog<br />
<br />
mDiag.Filter = "Bitmap Image|*.bmp"<br />
mDiag.ShowDialog()<br />
adjustedImage.Save(mDiag.FileName, ImageFormat.Bmp)<br />
<br />
End Sub<br />
I just want to save as a bitmap so that I can later compress it to DXT5 if necessary
Many thanks
Regards
Carl
-- modified at 12:56 Monday 12th March, 2007
|
|
|
|
|
i want to ask source code (in vb6)for transform RGB image to grayscale image.thank for your attention
|
|
|
|
|
Hey there,
I did some head-scratching on this one and I've come with this:
All values in row 0 are based on the red component of the pixel to which they are applied. Likewise, all values in rows 1, 2 and 3 are based on the green, blue and alpha components of the pixel consecutively.
The values in column 0 apply to the amount of red that will be placed in a given pixel. Similarly, columns 1, 2 and 3 are applied to the green, blue and alpha components.
So, for example, if I set 00, 11, 22 and 33 to 1.0, I'll get a regular picture, because I'm specifying regular amounts of each component based on only that component.
But if I were to set 01 to 0.5 then I would be adding 0.5 times the green value of each pixel to the resulting colors red component. And so forth...
|
|
|
|
|
I use the following method for coloring an image. My source image is a black font with a transparent background. I just want to color that graphical font. This font is not absolutely black it contains grayscales so ColorMap is not the right option.
private Bitmap ColorBitmap(Bitmap _b, Color _color)
{
Bitmap bmp = new Bitmap(_b.Width, _b.Height);
Graphics g = Graphics.FromImage(bmp);
float cr = _color.R / 255;
float cg = _color.G / 255;
float cb = _color.B / 255;
float ca = _color.A / 255;
float[][] colorMatrixElements = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {cr, cg, cb, ca, 1}};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
Rectangle rectangle = new Rectangle(0, 0, _b.Width, _b.Height);
g.DrawImage(_b, rectangle, 0, 0, _b.Width, _b.Height, GraphicsUnit.Pixel, imageAttributes);
return bmp;
}
I must admit that I don´t understand the concepts of the ColorMatrix. Is anybody able to modify my method in a way that I am able to color a grayscale image into any color ?
Best Regards, Kevin
|
|
|
|
|
Try using a matrix that combines grayscaling with the color component addition. Here goes one:
float[][] colorMatrixElements = {<br />
new float[] { 0.213f, 0.213f, 0.213f, 0, 0 }, <br />
new float[] { 0.715f, 0.715f, 0.715f, 0, 0 }, <br />
new float[] { 0.072f, 0.072f, 0.072f, 0, 0 }, <br />
new float[] { 0, 0, 0, 1, 0 },<br />
new float[] { cr, cg, cb, ca, 1 }};
|
|
|
|
|
Nice article Michael..
It helped me a lot..
Is it possible to do sharpness and blur operation on the image using the Colormatrix.
If so what are all the values we have to pass for the matrix elements..
Any help is appreciated..
Thanks in advance.
sing
-- modified at 9:36 Friday 7th July, 2006
|
|
|
|
|
I think ColorMatrix can't sharp and blur image. ColorMatrix only changes every pixel, doesn't consider the relation of pixels, while sharp and blur must consider neighboring pixels.
It's easy to blur image, create a 3*3 matrix, calculate every pixel by matrix and neighboring pixel, for example,
matrix is
0.05 0.05 0.05
0.05 0.60 0.05
0.05 0.05 0.05
pixels' gray value are
100 110 120
130 140 160
170 180 190
after blur, the center pixel's gray value is not 140, it's
(100+110+120+130+160+170+180+190)*0.05+140*0.6=145.
It's easy to apply this method to RGB color space.
I don't how to sharp an image.
-- modified at 5:48 Thursday 20th July, 2006
|
|
|
|
|