HighTechTalks DotNet Forums  

Colormatrix to create mask

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Colormatrix to create mask in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Carlos Alloatti
 
Posts: n/a

Default Colormatrix to create mask - 11-16-2007 , 03:11 PM






I need a colormatrix that will change every non white pixel in an
image to black, and leave all white pixels white. Any ideas?

Carlos Aloatti

Reply With Quote
  #2  
Old   
Michael Phillips, Jr.
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-16-2007 , 06:15 PM






Quote:
I need a colormatrix that will change every non white pixel in an
image to black, and leave all white pixels white. Any ideas?
One way to achieve this effect is by using two passes with scaling matrices.

The first pass is to scale each color by 1.0f/255.0f.
This results in each white pixel scaled to R =1.0f, G = 1.0f, B =1.0f. and
each non white pixel truncated to 0.0f.

The second pass scale each color by 255.0f.
This result does not change each non-white pixel but scales the each
original white pixel back to white.

Here is an example of some scaling color matrices:
http://msdn2.microsoft.com/en-us/library/ms533869.aspx


"Carlos Alloatti" <calloatti (AT) gmail (DOT) com> wrote

Quote:
I need a colormatrix that will change every non white pixel in an
image to black, and leave all white pixels white. Any ideas?

Carlos Aloatti



Reply With Quote
  #3  
Old   
Carlos Alloatti
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-17-2007 , 08:35 AM



Michael:

Thank you very much for your answer. There is only one problem: colors
that have one or more of the RGB channels = 255 do not turn black, but
for example RGB(255,0,0) or RGB(0,255,0)

Maybe one option would be to remap this colors to black, there are
only six colors to remap:

255,0,0
0,255,0
0,0,255
255,255,0
0,255,255
255,0,255

Another option (that I don't like, I considered it not elegant) is
adding an internediate pass to your solution to convert to
"grayscale":

First pass:
1/255, 0, 0, 0, 0, ;
0, 1/255, 0, 0, 0, ;
0, 0, 1/255, 0, 0, ;
0, 0, 0, 1, 0, ;
0, 0, 0, 0, 1)

Second pass:
0.17, 0.17, 0.17, 0, 0, ;
0.17, 0.17, 0.17, 0, 0, ;
0.17, 0.17, 0.17, 0, 0, ;
0, 0, 0, 1, 0, ;
0, 0, 0, 0, 1)

Third pass:
255, 0, 0, 0, 0, ;
0, 255, 0, 0, 0, ;
0, 0, 255, 0, 0, ;
0.0, 0.0, 0.0, 1, 0, ;
0.0, 0.0, 0.0, 0, 1)


The result is that only rgb(1,1,1) pixels get converted to rgb(1,1,1)
and pixels like (1,1,0) get converted to (0,0,0)

Why 0.17? Because 0.17 * 3 = 0.51 so only RGB(1,1,1) make it to 0.51
and get rounded up to 1

The big problem I see here is that colormatrices are 1 based instead
of being 255 based, that would have eliminated this kind of "playing
with rounding" things.

I have found another solution, but somehow I don't like it either.
What I do is apply a colormatrix that will turn the image to "half
grayscale":

0.2, 0.2, 0.2, 0, 0
0.2, 0.2, 0.2, 0, 0
0.1, 0.1, 0.1, 0, 0
0.0, 0.0, 0.0, 1, 0
0.0, 0.0, 0.0, 0, 1

Why do I use 0.2,0.2,01? Because of rounding errors, this gives me a
one decimal value for white: rgb(127.5,127.5,127.5)

Then I apply a Threshold of 0.5 (=127.5) and I get what I wanted.

The big problem I encountered is rounding errors, problems with colors
like RGB(254,255,255) getting converted to white for example.

I guess I will just check which one is faster and use that one.

I must mention that I am using the Visual FoxPro GDIPlusX port of
the .NET drawing classes.

Carlos Alloatti

Reply With Quote
  #4  
Old   
Michael Phillips, Jr.
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-17-2007 , 09:05 AM



It looks like the rounding will be an issue.

You can always try to create a 1bpp empty mask bitmap and use LockBits to
cycle through all of the colors of the image and set the corresponding mask
pixels accordingly.

This approach may be faster as there is no floating point operations and
float to integer conversions.


"Carlos Alloatti" <calloatti (AT) gmail (DOT) com> wrote

Quote:
Michael:

Thank you very much for your answer. There is only one problem: colors
that have one or more of the RGB channels = 255 do not turn black, but
for example RGB(255,0,0) or RGB(0,255,0)

Maybe one option would be to remap this colors to black, there are
only six colors to remap:

255,0,0
0,255,0
0,0,255
255,255,0
0,255,255
255,0,255

Another option (that I don't like, I considered it not elegant) is
adding an internediate pass to your solution to convert to
"grayscale":

First pass:
1/255, 0, 0, 0, 0, ;
0, 1/255, 0, 0, 0, ;
0, 0, 1/255, 0, 0, ;
0, 0, 0, 1, 0, ;
0, 0, 0, 0, 1)

Second pass:
0.17, 0.17, 0.17, 0, 0, ;
0.17, 0.17, 0.17, 0, 0, ;
0.17, 0.17, 0.17, 0, 0, ;
0, 0, 0, 1, 0, ;
0, 0, 0, 0, 1)

Third pass:
255, 0, 0, 0, 0, ;
0, 255, 0, 0, 0, ;
0, 0, 255, 0, 0, ;
0.0, 0.0, 0.0, 1, 0, ;
0.0, 0.0, 0.0, 0, 1)


The result is that only rgb(1,1,1) pixels get converted to rgb(1,1,1)
and pixels like (1,1,0) get converted to (0,0,0)

Why 0.17? Because 0.17 * 3 = 0.51 so only RGB(1,1,1) make it to 0.51
and get rounded up to 1

The big problem I see here is that colormatrices are 1 based instead
of being 255 based, that would have eliminated this kind of "playing
with rounding" things.

I have found another solution, but somehow I don't like it either.
What I do is apply a colormatrix that will turn the image to "half
grayscale":

0.2, 0.2, 0.2, 0, 0
0.2, 0.2, 0.2, 0, 0
0.1, 0.1, 0.1, 0, 0
0.0, 0.0, 0.0, 1, 0
0.0, 0.0, 0.0, 0, 1

Why do I use 0.2,0.2,01? Because of rounding errors, this gives me a
one decimal value for white: rgb(127.5,127.5,127.5)

Then I apply a Threshold of 0.5 (=127.5) and I get what I wanted.

The big problem I encountered is rounding errors, problems with colors
like RGB(254,255,255) getting converted to white for example.

I guess I will just check which one is faster and use that one.

I must mention that I am using the Visual FoxPro GDIPlusX port of
the .NET drawing classes.

Carlos Alloatti



Reply With Quote
  #5  
Old   
Michael C
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-18-2007 , 11:34 PM



"Carlos Alloatti" <calloatti (AT) gmail (DOT) com> wrote

Quote:
I need a colormatrix that will change every non white pixel in an
image to black, and leave all white pixels white. Any ideas?
Why not just use LockBits? It will be faster and cleaner code.

Michael




Reply With Quote
  #6  
Old   
Carlos Alloatti
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-19-2007 , 09:37 AM



On Nov 17, 12:05 pm, "Michael Phillips, Jr."
<mphillip... (AT) nospam (DOT) jun0.c0m> wrote:
Quote:
It looks like the rounding will be an issue.

You can always try to create a 1bpp empty mask bitmap and use LockBits to
cycle through all of the colors of the image and set the corresponding mask
pixels accordingly.

This approach may be faster as there is no floating point operations and
float to integer conversions.
Thanks for your reply. I will look into this, I dont know how to do
it, yet.

Carlos Alloatti


Reply With Quote
  #7  
Old   
Carlos Alloatti
 
Posts: n/a

Default Re: Colormatrix to create mask - 11-19-2007 , 09:37 AM



On Nov 19, 2:34 am, "Michael C" <m... (AT) nospam (DOT) com> wrote:
Quote:
"Carlos Alloatti" <calloa... (AT) gmail (DOT) com> wrote in message

news:c7b91cf8-3d88-43ca-87e4-391951a356b5 (AT) c29g2000hsa (DOT) googlegroups.com...

I need a colormatrix that will change every non white pixel in an
image to black, and leave all white pixels white. Any ideas?

Why not just use LockBits? It will be faster and cleaner code.

Michael
I will try that, thank you for your reply.

Carlos Alloatti


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.