The MASM Forum

Projects => Rarely Used Projects => RosAsm => Topic started by: guga on October 22, 2013, 09:51:05 AM

Title: need help with mean and standard deviation
Post by: guga on October 22, 2013, 09:51:05 AM
Hi guys, i need help understanding how to build a formula for mean and standard deviations for Lab colorspace


I´m trying to build a color transfer algorithm, but i didn´t understood the formulas neither how o create them.

The document i´m reading to build the formula is this (http://www.4shared.com/rar/sVGa3s3X/p66-huang.html)
Title: Re: need help with mean and standard derivation
Post by: dedndave on October 22, 2013, 12:36:51 PM
i have downloaded the PDF - let me see if i can understand it - lol
Title: Re: need help with mean and standard deviation
Post by: guga on October 22, 2013, 02:24:52 PM
Tks dedndave

For the regular mean and standard deviations, i succeed to make. The problem is with this weight variable, covariance and Q attributes. I´m not being able to understand how to code them, because i don´t know how to analyse those mathematical formulas

For getting the regular standard deviation, i succedded making this:

Code: [Select]
[CieLabVariance:
 CieLabVariance.Light: R$ 0
 CieLabVariance.aFactor: R$ 0
 CieLabVariance.bFactor: R$ 0
 CieLabVariance.KFactor: R$ 0]

[CieLabMean:
 CieLabMean.Light: R$ 0
 CieLabMean.aFactor: R$ 0
 CieLabMean.bFactor: R$ 0
 CieLabMean.KFactor: R$ 0]

[CieLabStdDev:
 CieLabStdDev.Light: R$ 0
 CieLabStdDev.aFactor: R$ 0
 CieLabStdDev.bFactor: R$ 0
 CieLabStdDev.KFactor: R$ 0]


Proc GetCieLabStandardDeviation:
    Arguments @pColorData, @ImgWidth, @ImgHeight

    call CieLabGetMean D@pColorData, D@ImgWidth, D@ImgHeight, CieLabMean
    call CieLabGetVariance D@pColorData, CieLabMean, D@ImgWidth, D@ImgHeight

    finit
    fld R$CieLabVariance.Light | fsqrt | fstp R$CieLabStdDev.Light
    fld R$CieLabVariance.aFactor | fsqrt | fstp R$CieLabStdDev.aFactor
    fld R$CieLabVariance.bFactor | fsqrt | fstp R$CieLabStdDev.bFactor
    ;fld R$CieLabVariance.KFactor | fsqrt | fstp R$StdDev.KFactor <------ weight ??????
EndP

Code: [Select]
Proc CieLabGetMean:
    Arguments @pColorData, @ImgWidth, @ImgHeight, @pCieLabMeanStr
    Local @iCounter, @Red, @Green, @Blue, @Y, @MaxCount, @MyPixelSrc, @MaxCount2
    Structure @TempStorage 44, @HueDis 0, @SaturationDis 8, @LightDis 16, @LumaDis 24, @aFactorDis 32, @bFactorDis 40
    Uses eax, ebx, ecx, esi, edi, edx

    mov D@iCounter 0
    mov D@MaxCount2 0
    mov ebx D@ImgWidth
    imul ebx D@ImgHeight
    mov D@MaxCount ebx

    finit
    mov esi D@pColorData

    .While D@iCounter < ebx

        lea ebx D@Red | movzx edi B$esi+RGBTRIPLE.rgbtRedDis | mov D$ebx edi
        lea edx D@Green | movzx edi B$esi+RGBTRIPLE.rgbtGreenDis | mov D$edx edi
        lea ecx D@Blue | movzx edi B$esi+RGBTRIPLE.rgbtBlueDis | mov D$ecx edi

        lea edi D@MyPixelSrc | mov D$edi 0
        mov eax D@Red | mov B$edi+ARGB.RedDis al
        mov eax D@Green | mov B$edi+ARGB.GreenDis al
        mov eax D@Blue | mov B$edi+ARGB.BlueDis al

        lea ebx D@HueDis | mov D$ebx 0 | mov D$ebx+4 0
        lea edx D@SaturationDis | mov D$edx 0 | mov D$edx+4 0
        lea ecx D@LightDis | mov D$ecx 0 | mov D$ecx+4 0
        call RGBtoHSL edi, ebx, edx, ecx
        ...If eax <> 0 ; do not compute gray color
            lea ebx D@LumaDis | mov D$ebx 0 | mov D$ebx+4 0
            lea edx D@aFactorDis | mov D$edx 0 | mov D$edx+4 0
            lea ecx D@bFactorDis | mov D$ecx 0 | mov D$ecx+4 0
            call RGBtoCieLab D@Red, D@Green, D@Blue, ebx, edx, ecx
            fld R$CieLabMean.Light | fadd R$ebx | fstp R$CieLabMean.Light
            fld R$CieLabMean.aFactor | fadd R$edx | fstp R$CieLabMean.aFactor
            fld R$CieLabMean.bFactor | fadd R$ecx | fstp R$CieLabMean.bFactor
            ;fld R$CieLabMean.bFactor | fdiv R$CieLabMean.aFactor | fadd R$CieLabMean.KFactor | fstp R$CieLabMean.KFactor
            inc D@MaxCount2
        ...End_If

        add esi 3
        inc D@iCounter
        mov ebx D@MaxCount

    .End_While

    fld R$CieLabMean.Light | fidiv F@MaxCount2 | fstp R$CieLabMean.Light
    fld R$CieLabMean.aFactor | fidiv F@MaxCount2 | fstp R$CieLabMean.aFactor
    fld R$CieLabMean.bFactor | fidiv F@MaxCount2 | fstp R$CieLabMean.bFactor
   ; fld R$CieLabMean.KFactor | fidiv F@MaxCount2 | fstp R$CieLabMean.KFactor

EndP

Code: [Select]
Proc CieLabGetVariance:
    Arguments @pColorData, @pMean, @ImgWidth, @ImgHeight
    Local @iCounter, @Red, @Green, @Blue, @Y, @MaxCount, @MyPixelSrc, @MaxCount2
    Structure @TempStorage 44, @HueDis 0, @SaturationDis 8, @LightDis 16, @LumaDis 24, @aFactorDis 32, @bFactorDis 40
    Uses eax, ebx, ecx, esi, edi, edx

    finit
    mov D@iCounter 0
    mov D@MaxCount2 0
    mov ebx D@ImgWidth
    imul ebx D@ImgHeight
    mov D@MaxCount ebx

    mov esi D@pColorData

    .While D@iCounter < ebx

        lea ebx D@Red | movzx edi B$esi+RGBTRIPLE.rgbtRedDis | mov D$ebx edi
        lea edx D@Green | movzx edi B$esi+RGBTRIPLE.rgbtGreenDis | mov D$edx edi
        lea ecx D@Blue | movzx edi B$esi+RGBTRIPLE.rgbtBlueDis | mov D$ecx edi

        lea edi D@MyPixelSrc | mov D$edi 0
        mov eax D@Red | mov B$edi+ARGB.RedDis al
        mov eax D@Green | mov B$edi+ARGB.GreenDis al
        mov eax D@Blue | mov B$edi+ARGB.BlueDis al

        lea ebx D@HueDis | mov D$ebx 0 | mov D$ebx+4 0
        lea edx D@SaturationDis | mov D$edx 0 | mov D$edx+4 0
        lea ecx D@LightDis | mov D$ecx 0 | mov D$ecx+4 0
        call RGBtoHSL edi, ebx, edx, ecx
        ...If eax <> 0 ; do not compute gray color

            lea ebx D@LumaDis | mov D$ebx 0 | mov D$ebx+4 0
            lea edx D@aFactorDis | mov D$edx 0 | mov D$edx+4 0
            lea ecx D@bFactorDis | mov D$ecx 0 | mov D$ecx+4 0
            call RGBtoCieLab D@Red, D@Green, D@Blue, ebx, edx, ecx

            fld R$CieLabMean.Light | fsub R$ebx | fabs | fmul ST0 | fadd R$CieLabVariance.Light | fstp R$CieLabVariance.Light
            fld R$CieLabMean.aFactor | fsub R$edx | fabs | fmul ST0 | fadd R$CieLabVariance.aFactor | fstp R$CieLabVariance.aFactor
            fld R$CieLabMean.bFactor | fsub R$ecx | fabs | fmul ST0 | fadd R$CieLabVariance.bFactor | fstp R$CieLabVariance.bFactor
            ;fdiv R$Variance.Saturation | fld R$Mean.KFactor | fsub ST1 | fabs | fmul ST0 | fadd R$Variance.KFactor | fstp R$Variance.KFactor
            inc D@MaxCount2
        ...End_If

        add esi 3
        inc D@iCounter
        mov ebx D@MaxCount

    .End_While

    fld R$CieLabVariance.Light | fidiv F@MaxCount2 | fstp R$CieLabVariance.Light
    fld R$CieLabVariance.aFactor | fidiv F@MaxCount2 | fstp R$CieLabVariance.aFactor
    fld R$CieLabVariance.bFactor | fidiv F@MaxCount2 | fstp R$CieLabVariance.bFactor

EndP

The GetCieLabStandardDeviation does not compute gray values. I´ll later make this as a variable and also make another variable to work with colors schemes different then D65, observer 2, such as D55, D50, observer 10 etc.

For now, i´m just testing the standard deviation and see how to implement it as the ones in the pdf with the weight and other variables.

Note: a few variables names are incorrect, cause i didn´t had time to fix that yet. Since i was trying to make it work the std dev i let some variables there even if they are unused.
Title: Re: need help with mean and standard deviation
Post by: jj2007 on October 22, 2013, 04:22:03 PM
If it gets really mathematical, you can check with either GSL (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1123) or YEPPP! (http://masm32.com/board/index.php?topic=2445.msg25742#msg25742)...
Title: Re: need help with mean and standard deviation
Post by: dedndave on October 22, 2013, 08:23:06 PM
the RosAsm syntax looks a bit strange   :P
Title: Re: need help with mean and standard deviation
Post by: guga on October 22, 2013, 08:28:19 PM
If you found out how, try port to masm and will try to assemble it .

I´m currently trying to fix a RGBtoCielab/CieLabtoRGB functions, because it seems that there are some inconsistencies in those colorspaces.

I suceeded to convert the color spaces with http://www.easyrgb.com/index.php?X=MATH&H=01#text1, but....the resultant image losts quality due o gamma error i suppose. I´ll see later what is going on after i try this other code below:

http://www.bastijn.nl/archives/153

About the syntax....mainly this is the macro set i use.
The B$ = byte ptr
D$ = dword ptr
W$ word ptr
@ = local variable

while/end_while macros same as in masm
If/..If...same as in masm

[CieLabStdDev:
 CieLabStdDev.Light: R$ 0
 CieLabStdDev.aFactor: R$ 0
 CieLabStdDev.bFactor: R$ 0
 CieLabStdDev.KFactor: R$ 0]

Global structure where each member has 8 bytes (Fpu Real)
Title: Re: need help with mean and standard deviation
Post by: guga on October 22, 2013, 09:17:08 PM
Here are some preliminary results from both algos of RBGtoCielab

From easyRGB site
(http://i42.tinypic.com/14cuohk.jpg)

From www.bastijn.nl (http://www.bastijn.nl) site
(http://i44.tinypic.com/35jmh3m.jpg)

Although bastin site looks way better....I just converted half of the function. I had no time to finish the CieLabtoRGB of it, so i used the one in EasyRGB. On this image it was ok, but on several others, there are showing artifacts and gamma errors (shows red, green, yellow, orange pixels)

I´ll fully convert it tonight an see what is the result.

If i suceed to convert it, then i can use the standard deviation etc, once dedndave suceed to translate it.
Title: Re: need help with mean and standard deviation
Post by: FORTRANS on October 22, 2013, 11:48:49 PM
Hi,

   Following that link gets me to an *.EXE, not a *.PDF.  Is
there another link?

   I have a book that mentions the L-a-b (or L*a*b*) color
coordinate system, but just gives a formula for the coord-
inate transformation and a description.

Regards,

Steve
Title: Re: need help with mean and standard deviation
Post by: dedndave on October 23, 2013, 12:04:56 AM
hi Steve
well - the 4shared page has about 20 download buttons on it - lol
and when you find the right one - you have to sign in to get it

maybe Christian Graus can help with the colors...
this is part 6 of a 6-part series
http://www.codeproject.com/Articles/7138/Image-Processing-for-Dummies-with-C-and-GDI-Part-6 (http://www.codeproject.com/Articles/7138/Image-Processing-for-Dummies-with-C-and-GDI-Part-6)
his other articles are listed here
http://www.codeproject.com/Articles/Christian-Graus#articles (http://www.codeproject.com/Articles/Christian-Graus#articles)
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 12:07:55 AM
Hi Steve

a exe ? Are u sure ? The link is in my 1st post

it is in 4shared here
http://search.4shared.com/postDownload/sVGa3s3X/p66-huang.html

It is a rar file that inside contains the pdf

The formula is what i´m trying to understand. I suceeded to convert some color functions on the easyrgb site,  but this last one from them is too slow and also innacurate. I`m trying to convert the 2nd example. The results so far are somewhat similar to a IPT colorspace, except that they don´t show so much details when the image is too dark.

What i´m trying to understand and create is the formulas described in the chapter

3. AUTOMATIC SELECTIVE COLOR TRANSFER ALGORITHM FOR IMAGES
3.1 New mean and standard deviation values of ASCT algorithm

page 2 of the pdf i uploaded

These formulas:
(http://i42.tinypic.com/jhucdx.jpg)

I don´t know what they means so i can try to code them.
Title: Re: need help with mean and standard deviation
Post by: qWord on October 23, 2013, 12:11:53 AM
guga,
dedndave,

did you have the rights to share that paper?
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 12:14:46 AM
Nope, this article is on internet, but, it is not mine. The article was released by the authors on some color related websites for free.
It is not from a book or something.

Btw...the article is not commercial. As far i remember it is a study from students from a university related to color transfer techniques.
Title: Re: need help with mean and standard deviation
Post by: qWord on October 23, 2013, 12:21:35 AM
Quote
Permission to make digital or hard copies of all or part of this work for
personal or classroom use is granted without fee provided that copies are
not made or distributed for profit or commercial advantage and that
copies bear this notice and the full citation on the first page. To copy
otherwise, or republish, to post on servers or to redistribute to lists,
requires prior specific permission and/or a fee.
SAC’11, March 21-25, 2011, TaiChung, Taiwan.
Copyright 2011 ACM 978-1-4503-0113-8/11/03…$10.00
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 12:46:15 AM
Yeah, like i said, it is for free as long you don´t use for commercial purposes. But the copy or post the whole pages (or the book, that contains this article) requires permission.

The article is at page 66, therefore it is not the whole "book" and neither for commercial purposes, so using a few pages (6) for study and educational is granted.

This is related to the whole book that seems to be released articles from several people on a symposium.

Look at google and you will see this article in hundreds of universities sites with links etc
https://www.google.com.br/search?q=Copyright+2011+ACM+978-1-4503-0113-8/11/03%E2%80%A6%2410.00.&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&gws_rd=cr&ei=7YBmUtPZNsOrkQeqsYHABA#psj=1&q=ACM+978-1-4503-0113-8%2F11%2F03&rls=org.mozilla:en-US%3Aofficial&safe=off

What is not allowed is copy the whole book of this Symposhium and share it. But isolated articles for study and educational purposes this is ok.

I´m not sure if this Yao-Hsien Huang is the same guy that send me trough email his app that is able to unblur images that will be releases on Adobe CS, but definitelly, reproducing and article for this purposes and situation is legal.
Title: Re: need help with mean and standard deviation
Post by: qWord on October 23, 2013, 01:13:51 AM
Your argumentation makes not much sense, because the Article can be download from the ACM digital library (copyright holder) for 10$ (15$ for non members). Educational (or private) purposes implies that the paper is only shared within the study group and not the WWW.
Your google result only shows references to that paper.

You might ask Hutch what he thinks on that or simply type out the formulas and correctly quote the text!
Title: Re: need help with mean and standard deviation
Post by: FORTRANS on October 23, 2013, 01:22:43 AM
hi Steve
well - the 4shared page has about 20 download buttons on it - lol
and when you find the right one - you have to sign in to get it

Hi Dave,

   Right.  That explains what happened, except for the EXE
download.  Thanks.

Quote
maybe Christian Graus can help with the colors...
this is part 6 of a 6-part series
http://www.codeproject.com/Articles/7138/Image-Processing-for-Dummies-with-C-and-GDI-Part-6 (http://www.codeproject.com/Articles/7138/Image-Processing-for-Dummies-with-C-and-GDI-Part-6)

   That looks okay to explain the HLS color space.  Lab is a
bit different.


What i´m trying to understand and create is the formulas described in the chapter

3. AUTOMATIC SELECTIVE COLOR TRANSFER ALGORITHM FOR IMAGES
3.1 New mean and standard deviation values of ASCT algorithm

page 2 of the pdf i uploaded

I don´t know what they means so i can try to code them.

   He is applying image processing to images based on the
the statistics (mean, standard deviation) of portions of the
images.  He can adjust the processing so it applies to image
regions that appear rather different.  The book I have uses
something similar to sharpen images.  Using the average
pixel values of a small region to lighten dark areas for instance.
And using differing amounts of sharpening based on the local
standard deviation of the pixels.

Regards,

Steve
Title: Re: need help with mean and standard deviation
Post by: dedndave on October 23, 2013, 04:23:57 AM
the EXE is probably an "ilivid" downloader, custom to that specific file
i think they're safe - but i still prefer not to download and run EXE's like that   :redface:
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 07:09:03 AM
livid ? dont dl it !. 4shared have some merchandising...Only dl the rar file after the circle is runnin when you press to dl
Title: Re: need help with mean and standard deviation
Post by: dedndave on October 23, 2013, 10:48:54 AM
i have tried ilivid in the past
as i recall, it was pretty easy to remove any ad-ware remnants after you get what you want
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 02:21:58 PM
dave, i´m trying to convert this to C so it can be easy to analyse, but VisualStudio 10 is refusing to compiler....What i´m making wrong here ?

I´m trying to convert it to plain C (No c++), but VS keeps giving me errors

Original code
Code: [Select]
vec3 RGB2Lab(in vec3 rgb){
    float R = rgb.x;
    float G = rgb.y;
    float B = rgb.z;
    // threshold
    float T = 0.008856;

    float X = R * 0.412453 + G * 0.357580 + B * 0.180423;
    float Y = R * 0.212671 + G * 0.715160 + B * 0.072169;
    float Z = R * 0.019334 + G * 0.119193 + B * 0.950227;

    // Normalize for D65 white point
    X = X / 0.950456;
    Y = Y;
    Z = Z / 1.088754;

    bool XT, YT, ZT;
    XT = false; YT=false; ZT=false;
    if(X > T) XT = true;
    if(Y > T) YT = true;
    if(Z > T) ZT = true;

    float Y3 = pow(Y,1/3f);
    float fX, fY, fZ;
    if(XT){ fX = pow(X, 1/3f);} else{ fX = 7.787 * X + 16/116; }
    if(YT){ fY = Y3; } else{ fY = 7.787 * Y + 16/116 ; }
    if(ZT){ fZ = pow(Z,1/3f); } else{ fZ = 7.787 * Z + 16/116; }

    float L; if(YT){ L = (116 * Y3) - 16.0; }else { L = 903.3 * Y; }
    float a = 500 * ( fX - fY );
    float b = 200 * ( fY - fZ );

    return vec3(L,a,b);
}

Code: [Select]
vec3 Lab2RGB(in vec3 lab){
    //Thresholds
    float T1 = 0.008856;
    float T2 = 0.206893;

    float X,Y,Z;

    //Compute Y
    bool XT, YT, ZT;
    XT = false; YT=false; ZT=false;

    float fY = pow(((lab.x + 16.0) / 116.0),3);
    if(fY > T1){ YT = true; }
    if(YT){ fY = fY; } else{ fY = (lab.x / 903.3); }
    Y = fY;

    //Alter fY slightly for further calculations
    if(YT){ fY = pow(fY,1/3f); } else{ fY = (7.787 * fY + 16.0/116.0); }

    //Compute X
    float fX = ( lab.y / 500.0 ) + fY;
    if(fX > T2){ XT = true; }
    if(XT){ X = pow(fX,3); } else{X = ((fX - (16/116.0)) / 7.787); }

    //Compute Z
    float fZ = fY - ( lab.z / 200.0 );
    if(fZ > T2){ ZT = true; }
    if(ZT){ Z = pow(fZ,3); } else{ Z = ((fZ - (16/116.0)) / 7.787); }

    //Normalize for D65 white point
    X = X * 0.950456;
    Z = Z * 1.088754;

    //XYZ to RGB part
    float R =  3.240479 * X + -1.537150 * Y + -0.498535 * Z;
    float G = -0.969256 * X +  1.875991 * Y +  0.041556 * Z;
    float B =  0.055648 * X + -0.204043 * Y +  1.057311 * Z;

    return vec3(R,G,B);
}


Mine code:
Code: [Select]
struct lab{
  float x;
  float y;
  float z;
} lab; // this is just a random name not mine!

void Lab2RGB(struct lab *lab){
    //Thresholds
    float T1 = 0.008856;
    float T2 = 0.206893;

    float X,Y,Z;

    //Compute Y
    bool XT, YT, ZT;
    XT = false; YT=false; ZT=false;

    float fY = pow(((lab.x + 16.0) / 116.0),3);
    if(fY > T1){ YT = true; }
    if(YT){ fY = fY; } else{ fY = (lab.x / 903.3); }
    Y = fY;

    //Alter fY slightly for further calculations
    if(YT){ fY = pow(fY,1/3f); } else{ fY = (7.787 * fY + 16.0/116.0); }

    //Compute X
    float fX = ( lab.y / 500.0 ) + fY;
    if(fX > T2){ XT = true; }
    if(XT){ X = pow(fX,3); } else{X = ((fX - (16/116.0)) / 7.787); }

    //Compute Z
    float fZ = fY - ( lab.z / 200.0 );
    if(fZ > T2){ ZT = true; }
    if(ZT){ Z = pow(fZ,3); } else{ Z = ((fZ - (16/116.0)) / 7.787); }

    //Normalize for D65 white point
    X = X * 0.950456;
    Z = Z * 1.088754;

    //XYZ to RGB part
    float R =  3.240479 * X + -1.537150 * Y + -0.498535 * Z;
    float G = -0.969256 * X +  1.875991 * Y +  0.041556 * Z;
    float B =  0.055648 * X + -0.204043 * Y +  1.057311 * Z;

    //return vec3(R,G,B); ????????????? how to return it properly ?
}


The reason is that i suceeded (i guess) to port the RGB2Lab, but when i did the opposite function (LAb2RGB) the image turns onto Green !

Can someone port this functions to VS or masm, so i can analyse them to see how they works ?
Title: Re: need help with mean and standard deviation
Post by: TWell on October 23, 2013, 05:59:11 PM
try this:
Code: [Select]
// TestRGB2Lab.c for msvc
#include <stdio.h>
//#include <stdbool.h>
#include <math.h>

typedef int bool;
#define true 1
#define false 0

typedef struct _VEC3
{
float x,y,z;
} VEC3, *PVEC3;

VEC3 RGB2Lab(VEC3 rgb)
{
VEC3 rv3;
float R = rgb.x;
float G = rgb.y;
float B = rgb.z;
// threshold
float T = 0.008856;

float X = R * 0.412453 + G * 0.357580 + B * 0.180423;
float Y = R * 0.212671 + G * 0.715160 + B * 0.072169;
float Z = R * 0.019334 + G * 0.119193 + B * 0.950227;
bool XT, YT, ZT;
float fX, fY, fZ;
float Y3;
float L;
float a, b;

// Normalize for D65 white point
X = X / 0.950456;
//Y = Y;
Z = Z / 1.088754;

if (X > T) XT = true; else XT = false;
if (Y > T) YT = true; else YT = false;
if (Z > T) ZT = true; else ZT = false;

Y3 = pow(Y, 1 / 3.0);
if (XT) {
fX = pow(X, 1 / 3.0);
} else {
fX = 7.787 * X + 16 / 116;
}
if (YT) {
fY = Y3;
} else {
fY = 7.787 * Y + 16 / 116;
}
if (ZT) {
fZ = pow(Z, 1 / 3.0);
} else {
fZ = 7.787 * Z + 16 / 116;
}

if (YT) {
L = (116 * Y3) - 16.0;
} else {
L = 903.3 * Y;
}
a = 500 * (fX - fY);
b = 200 * (fY - fZ);

//return vec3(L, a, b);
rv3.x = L;
rv3.y = a;
rv3.z = b;
return rv3;
}

VEC3 Lab2RGB(VEC3 lab)
{
VEC3 rv3;
//Thresholds
float T1 = 0.008856;
float T2 = 0.206893;

float X, Y, Z;
float fX, fY, fZ;
float R, G, B;

//Compute Y
bool XT, YT, ZT;
XT = false;
YT = false;
ZT = false;

fY = pow(((lab.x + 16.0) / 116.0), 3);
if (fY > T1) {
YT = true;
}
if (YT) {
fY = fY;
} else {
fY = (lab.x / 903.3);
}
Y = fY;

//Alter fY slightly for further calculations
if (YT) {
fY = pow(fY, 1 / 3.0);
} else {
fY = (7.787 * fY + 16.0 / 116.0);
}

//Compute X
fX = (lab.y / 500.0) + fY;
if (fX > T2) {
XT = true;
}
if (XT) {
X = pow(fX, 3);
} else {
X = ((fX - (16 / 116.0)) / 7.787);
}

//Compute Z
fZ = fY - (lab.z / 200.0);
if (fZ > T2) {
ZT = true;
}
if (ZT) {
Z = pow(fZ, 3);
} else {
Z = ((fZ - (16 / 116.0)) / 7.787);
}

//Normalize for D65 white point
X = X * 0.950456;
Z = Z * 1.088754;

//XYZ to RGB part
R = 3.240479 * X + -1.537150 * Y + -0.498535 * Z;
G = -0.969256 * X + 1.875991 * Y + 0.041556 * Z;
B = 0.055648 * X + -0.204043 * Y + 1.057311 * Z;

rv3.x = R;
rv3.y = G;
rv3.z = B;
return rv3;
//return vec3(R, G, B);
}

int main(int argc, char **argv)
{
VEC3 v3;
v3.x = 50.0;
v3.y = 50.0;
v3.z = 50.0;
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = Lab2RGB(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = RGB2Lab(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
return 0;
}
Can anyone try this version with MSVS 2013 in C mode (C99)?
With PellesC one can compile it.
Code: [Select]
// RGB2LabTest.c for C99
#include <stdio.h>
#include <stdbool.h>
#include <math.h>

typedef struct _VEC3
{
float x,y,z;
} VEC3, *pvec3;

VEC3 RGB2Lab(VEC3 rgb)
{
VEC3 rv3;
float R = rgb.x;
float G = rgb.y;
float B = rgb.z;
// threshold
float T = 0.008856;

float X = R * 0.412453 + G * 0.357580 + B * 0.180423;
float Y = R * 0.212671 + G * 0.715160 + B * 0.072169;
float Z = R * 0.019334 + G * 0.119193 + B * 0.950227;

// Normalize for D65 white point
X = X / 0.950456;
//Y = Y;
Z = Z / 1.088754;

bool XT, YT, ZT;
if (X > T) XT = true; else XT = false;
if (Y > T) YT = true; else YT = false;
if (Z > T) ZT = true; else ZT = false;

float Y3 = pow(Y, 1 / 3.0);
float fX, fY, fZ;
if (XT) {
fX = pow(X, 1 / 3.0);
} else {
fX = 7.787 * X + 16 / 116;
}
if (YT) {
fY = Y3;
} else {
fY = 7.787 * Y + 16 / 116;
}
if (ZT) {
fZ = pow(Z, 1 / 3.0);
} else {
fZ = 7.787 * Z + 16 / 116;
}

float L;
if (YT) {
L = (116 * Y3) - 16.0;
} else {
L = 903.3 * Y;
}
float a = 500 * (fX - fY);
float b = 200 * (fY - fZ);

//return vec3(L, a, b);
rv3.x = L;
rv3.y = a;
rv3.z = b;
return rv3;
}

VEC3 Lab2RGB(VEC3 lab)
{
VEC3 rv3;
//Thresholds
float T1 = 0.008856;
float T2 = 0.206893;

float X, Y, Z;

//Compute Y
bool XT, YT, ZT;
XT = false;
YT = false;
ZT = false;

float fY = pow(((lab.x + 16.0) / 116.0), 3);
if (fY > T1) {
YT = true;
}
if (YT) {
fY = fY;
} else {
fY = (lab.x / 903.3);
}
Y = fY;

//Alter fY slightly for further calculations
if (YT) {
fY = pow(fY, 1 / 3.0);
} else {
fY = (7.787 * fY + 16.0 / 116.0);
}

//Compute X
float fX = (lab.y / 500.0) + fY;
if (fX > T2) {
XT = true;
}
if (XT) {
X = pow(fX, 3);
} else {
X = ((fX - (16 / 116.0)) / 7.787);
}

//Compute Z
float fZ = fY - (lab.z / 200.0);
if (fZ > T2) {
ZT = true;
}
if (ZT) {
Z = pow(fZ, 3);
} else {
Z = ((fZ - (16 / 116.0)) / 7.787);
}

//Normalize for D65 white point
X = X * 0.950456;
Z = Z * 1.088754;

//XYZ to RGB part
float R = 3.240479 * X + -1.537150 * Y + -0.498535 * Z;
float G = -0.969256 * X + 1.875991 * Y + 0.041556 * Z;
float B = 0.055648 * X + -0.204043 * Y + 1.057311 * Z;

rv3.x = R;
rv3.y = G;
rv3.z = B;
return rv3;
//return vec3(R, G, B);
}

int main(int argc, char **argv)
{
VEC3 v3;
v3.x = 50.0;
v3.y = 50.0;
v3.z = 50.0;
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = Lab2RGB(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = RGB2Lab(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
return 0;
}
Title: Re: need help with mean and standard deviation
Post by: guga on October 23, 2013, 10:55:38 PM
Tks twell

I compiled it for console mode and it works on VS2008. I just needed to change the pow function  to powf due to ambiguity found.

Code: [Select]
// ColorTestConvert.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

/*
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/


// RGB2LabTest.c for C99
#include <stdio.h>
//#include <stdbool.h>
#include <math.h>

typedef struct _VEC3
{
float x,y,z;
} VEC3, *pvec3;

VEC3 RGB2Lab(VEC3 rgb)
{
VEC3 rv3;
float R = rgb.x;
float G = rgb.y;
float B = rgb.z;
// threshold
float T = 0.008856;

float X = R * 0.412453 + G * 0.357580 + B * 0.180423;
float Y = R * 0.212671 + G * 0.715160 + B * 0.072169;
float Z = R * 0.019334 + G * 0.119193 + B * 0.950227;

// Normalize for D65 white point
X = X / 0.950456;
//Y = Y;
Z = Z / 1.088754;

bool XT, YT, ZT;
if (X > T) XT = true; else XT = false;
if (Y > T) YT = true; else YT = false;
if (Z > T) ZT = true; else ZT = false;

float Y3 = powf(Y, 1 / 3.0);
float fX, fY, fZ;
if (XT) {
fX = powf(X, 1 / 3.0);
} else {
fX = 7.787 * X + 16 / 116;
}
if (YT) {
fY = Y3;
} else {
fY = 7.787 * Y + 16 / 116;
}
if (ZT) {
fZ = powf(Z, 1 / 3.0);
} else {
fZ = 7.787 * Z + 16 / 116;
}

float L;
if (YT) {
L = (116 * Y3) - 16.0;
} else {
L = 903.3 * Y;
}
float a = 500 * (fX - fY);
float b = 200 * (fY - fZ);

//return vec3(L, a, b);
rv3.x = L;
rv3.y = a;
rv3.z = b;
return rv3;
}

VEC3 Lab2RGB(VEC3 lab)
{
VEC3 rv3;
//Thresholds
float T1 = 0.008856;
float T2 = 0.206893;

float X, Y, Z;

//Compute Y
bool XT, YT, ZT;
XT = false;
YT = false;
ZT = false;

float fY = pow(((lab.x + 16.0) / 116.0), 3);
if (fY > T1) {
YT = true;
}
if (YT) {
fY = fY;
} else {
fY = (lab.x / 903.3);
}
Y = fY;

//Alter fY slightly for further calculations
if (YT) {
fY = powf(fY, 1 / 3.0);
} else {
fY = (7.787 * fY + 16.0 / 116.0);
}

//Compute X
float fX = (lab.y / 500.0) + fY;
if (fX > T2) {
XT = true;
}
if (XT) {
X = pow(fX, 3);
} else {
X = ((fX - (16 / 116.0)) / 7.787);
}

//Compute Z
float fZ = fY - (lab.z / 200.0);
if (fZ > T2) {
ZT = true;
}
if (ZT) {
Z = pow(fZ, 3);
} else {
Z = ((fZ - (16 / 116.0)) / 7.787);
}

//Normalize for D65 white point
X = X * 0.950456;
Z = Z * 1.088754;

//XYZ to RGB part
float R = 3.240479 * X + -1.537150 * Y + -0.498535 * Z;
float G = -0.969256 * X + 1.875991 * Y + 0.041556 * Z;
float B = 0.055648 * X + -0.204043 * Y + 1.057311 * Z;

rv3.x = R;
rv3.y = G;
rv3.z = B;
return rv3;
//return vec3(R, G, B);
}

int main(int argc, char **argv)
{
VEC3 v3;
v3.x = 50.0;
v3.y = 50.0;
v3.z = 50.0;
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = Lab2RGB(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
v3 = RGB2Lab(v3); // pass copy of v3
printf("x= %0.3f, y= %0.3f, z= %0.3f\n", v3.x, v3.y, v3.z);
return 0;
}
Title: Re: need help with mean and standard deviation
Post by: guga on October 24, 2013, 03:16:20 PM
Ok, guys...both functions are fully working (RGB2Lab and Lab2RGB)

What was missing on the original code is a division of the Red, Green and Blue with 255

I´ll make some other tests and try to implement other matrices for HDTV etc before release them here

Now, it is needed to understand the other functions related to the article posted.