Android Example Using MFColorMatrix (from my MFLib)

A short description using the MFColorMatrix class from my MFLib.

A color matrix is a 4x5 matrix for transforming the color and alpha components of a Bitmap.
The definition of the color matrix:
B4X:
Input              R   G   B   A   O
                   e   r   l   l   f
                   d   e   u   p   f
                       e   e   h   s
                       n       a   e
                                   t
                   v   v   v   v

        Red    < [01][02][03][04][05]
Output  Green  < [06][07][08][09][10]
        Blue   < [11][12][13][14][15]
        Alpha  < [16][17][18][19][20]

Therefore the columns defines the input for the RGBA values and the rows defines the output.
The parts of the color value (red, green, blue and alpha) will be multiplied with the corresponding matrix node, then all results will be added to define the output value.

To define the value for the red channel of the output color:
The red value of the input color is multiplied with the value in matrix node [01].
The green value of the input color is multiplied with the value in matrix node [02].
The blue value of the input color is multiplied with the value in matrix node [03].
The alpha value of the input color is multiplied with the value in matrix node [04].
Then all four results will be added and additionally added the offset value of node [05].


Same for the green channel with the nodes [06], [07], [08], [09] and [10].
Same for the blue channel with the nodes [11], [12], [13], [14] and [15].
Same for the alpha channel with the nodes [16], [17], [18], [19] and [20].

The values for the columns 1-4 must be from -1.0 to 2.0 (1.0 doesn't change anything).
The value for the column 5 must be from -255.0 to 255.0 (0.0 doesn't change anything).

The definition of a color matrix which does nothing:
B4X:
[1.0][0.0][0.0][0.0][0.0]
[0.0][1.0][0.0][0.0][0.0]
[0.0][0.0][1.0][0.0][0.0]
[0.0][0.0][0.0][1.0][0.0]

A color matrix which swaps the red and blue channel:
B4X:
[0.0][0.0][1.0][0.0][0.0]
[0.0][1.0][0.0][0.0][0.0]
[1.0][0.0][0.0][0.0][0.0]
[0.0][0.0][0.0][1.0][0.0]

A color matrix which inverts the color:
B4X:
[-1.0][ 0.0][ 0.0][0.0][255.0]
[ 0.0][-1.0][ 0.0][0.0][255.0]
[ 0.0][ 0.0][-1.0][0.0][255.0]
[ 0.0][ 0.0][ 0.0][1.0][  0.0]
Multiplication with -1 results in an negative value so the offset in column 5 is used to compensate.
A color with red:224 green:96 blue:64 will result in red:31 green:159 blue:191
red channel: 224 * -1.0 + 96 * 0.0 + 64 * 0.0 + 255.0 = 31
green channel: 224 * 0.0 + 96 * -1.0 + 64 * 0.0 + 255.0 = 159
blue channel: 224 * 0.0 + 96 * 0.0 + 64 * -1.0 + 255.0 = 191


Using the MFColorMatrix class

You must first create a class instance:
B4X:
Dim cm As MFColorMatrix
cm.Initialize

Then assign one or more matrices to it:
Append(id As Int) adds a predefined matrix
Append2(id As Int, value As Float) adds a predefined matrix (for conversions which needs an additional value)
With Append3() and Append4() you can create your own matrix (enter the values as numbered above).
Append3(matrix() As Float) (the array must contain exact 25 values)
Append4(rr As Float, gr As Float, br As Float, ar As Float, roff As Float, rg As Float, gg As Float, bg As Float, ag As Float, goff As Float, rb As Float, gb As Float, bb As Float, ab As Float, boff As Float, ra As Float, ga As Float, ba As Float, aa As Float, aoff As Float)

Finally assign a bitmat:
Assign(bmp As Bitmap) As Boolean changes the original bitmap
Assign2(bmpIn As Bitmap, bmpOut As Bitmap) As Boolean reads from bmpIn and write the changes in bmpOut
Assign3(bmp As Bitmap) As Bitmap reads from bmp and returns a new bitmap


List of predefined color matrices (no value needed, used with Append):
B4X:
MATRIX_BLACKANDWHITE
MATRIX_GRAYSCALE
MATRIX_INVERTED
MATRIX_SEPIA
MATRIX_ROSE
MATRIX_COLORSWAP_RGB_BGR
MATRIX_COLORSWAP_RGB_BRG
MATRIX_COLORSWAP_RGB_GBR
MATRIX_COLORSWAP_RGB_GRB
MATRIX_COLORSWAP_RGB_RBG
MATRIX_RED
MATRIX_REDTOGRAY
MATRIX_GREEN
MATRIX_GREENTOGRAY
MATRIX_BLUE
MATRIX_BLUETOGRAY
MATRIX_CYAN
MATRIX_MAGENTA
MATRIX_YELLOW
MATRIX_HUE
MATRIX_SATURATION
MATRIX_BRIGHTNESS
MATRIX_CONTRAST
MATRIX_COLORBOOST
MATRIX_ROTATE_RED
MATRIX_ROTATE_GREEN
MATRIX_ROTATE_BLUE
MATRIX_POLAROID
MATRIX_OLDPHOTO
MATRIX_RED_BLINDNESS
MATRIX_RED_DEBILITY
MATRIX_GREEN_BLINDNESS
MATRIX_GREEN_DEBILITY
MATRIX_BLUE_BLINDNESS
MATRIX_BLUE_DEBILITY
MATRIX_COLOR_BLINDNESS
MATRIX_COLOR_DEBILITY
MATRIX_LUMINANCE2ALPHA
MATRIX_LUMINANCE2ALPHA2
MATRIX_RGB2YUV
MATRIX_YUV2RGB

List of predefined color matrices (value needed, used with Append2):
B4X:
matrix                   value
MATRIX_HUE               0.0 - 360.0
MATRIX_SATURATION       -1.0 - 1.0
MATRIX_BRIGHTNESS       -1.0 - 1.0
MATRIX_CONTRAST         -1.0 - 1.0
MATRIX_ROTATE_RED        0.0 - 360.0
MATRIX_ROTATE_GREEN      0.0 - 360.0
MATRIX_ROTATE_BLUE       0.0 - 360.0
MATRIX_LUMINANCE2ALPHA  -1.0 - 1.0
MATRIX_LUMINANCE2ALPHA2 -1.0 - 1.0
 

hayderOICO

Member
Licensed User
Longtime User
hi manfred

I want to make a "false colour" image from a greyscale image. any tips on how to do that?
 

hayderOICO

Member
Licensed User
Longtime User
hi manfred

for example.

here's a greyscale image ;

Fig-1-Green-channel-of-the-fundus-image.png




and a colour version would look like

slide_1.jpg





I've done such transformations using the convert (tint) function in imagemagick in the past... but don't know how to approach this here.
 

MaFu

Well-Known Member
Licensed User
Longtime User
This result is not reachable with ColorMatrix only.
You can colorize the image:
Fig-1-Green-channel-of-the-fundus-image_colored.png

This example is created with this values:
B4X:
Dim cm As MFColorMatrix
cm.Initialize
cm.Append4(2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
cm.Assign(bmp)
Afterwards try to change the contrast.
I simulated it with Paint.net (contrast: +65 / luminace: + 10):
Fig-1-Green-channel-of-the-fundus-image_colored_contrast.png
 
Last edited:

hayderOICO

Member
Licensed User
Longtime User
Hi I get "unknown member: append4" do I have an old version of the library? also I don't have assign2 and assign3 either.

I have version 2.01 of the libs.
 
Last edited:

MaFu

Well-Known Member
Licensed User
Longtime User
Version 2.01 is the current version. And the ColorMatrix class in this version includes the Append4() function and also Assign2() and Assign3().
Can you post your used source?
 

hayderOICO

Member
Licensed User
Longtime User
hi MF

I used your exact source code as an example, and the "autocomplete" shows all the functions like append and append2 as well as assign. but not append3 or append4.

I'm using B4A version 5 and the MFlibs show as version 2.01

kParpnl.png


kParpnl.png
 
Last edited:

hayderOICO

Member
Licensed User
Longtime User
if i try to force append4 or assign2 and compile I get "unknown member" error for both.

I also tried the same with B4A version 6.5 with the same result.

I would really appreciate if you can send out an updated lib.
 
Last edited:
Top