2/26/2013

string hex to decimal


#include < stdlib.h >
#include < stdio.h >

void main()
{
char s[]="17D";
unsigned long x;
x = strtoul(s, 0, 16);
printf("%d\n", x);

}

2/23/2013

The Thing to be careful, when you use cvSetImageROI


cvSetImage is to set region of interest by a rect size.
If you program as follows:

cvSetImageROI(image, cvRect(10,10,300,300) ); //cvRect(Left, Top, Width, Height)

Then, The region set by (10,10,300,300) in the image will be prcessed only by functions.
so..

cvSetImageROI(image, cvRect(10,10,300,300) ); //cvRect(Left, Top, Width, Height)
cvSetZero(image);
cvResetImageROI(image);

The region (10,10,300,300) in the image is only set by zero.
and ROI is released by cvResetImageROI function.

But there is one thing to be careful.

//Origin image size is 1024, 768
cvSetIamgeROI(image, cvRect(10, 10, 300, 300) );
image->width; -> ?
image->height; -> ?

What do you think about the value?
the answer is 1024, 768
so if you program as follows:(This is not correct source!!)
cvSetImageROI(image, cvRect(10,10,300,300));
for(int h=0; h< image->height; ++h)
{
     for(int w=0; w< image->width; ++w)
     {
          unsigned char R,G,B;
          B = (unsigned char)image->imageData[h*image->widthStep+w*3+0];
          G = (unsigned char)image->imageData[h*image->widthStep+w*3+1];
          R = (unsigned char)image->imageData[h*image->widthStep+w*3+2];

          image->imageData[h*image->widthStep+w*3+0] =  (unsigned char)(255-B);
          image->imageData[h*image->widthStep+w*3+1] =  (unsigned char)(255-G);
          image->imageData[h*image->widthStep+w*3+2] =  (unsigned char)(255-R);
     }
}
cvResetImageROI(image);

The color of whole area of image is inverted.
So If you apply invert processing to the ROI, you have to program as follows:(This is correct source)
for(int h=image2->roi->yOffset; h< image2->roi->yOffset+image2->roi->height; ++h)
{
     for(int w=image2->roi->xOffset; w< image2->roi->xOffset+ image2->roi->width; ++w)
     {
          unsigned char R,G,B;
          B = (unsigned char)image2->imageData[h*image2->widthStep+w*3+0];
          G = (unsigned char)image2->imageData[h*image2->widthStep+w*3+1];
          R = (unsigned char)image2->imageData[h*image2->widthStep+w*3+2];

          image->imageData[h*image->widthStep+w*3+0] =  (unsigned char)(255-B);
          image->imageData[h*image->widthStep+w*3+1] =  (unsigned char)(255-G);
          image->imageData[h*image->widthStep+w*3+2] =  (unsigned char)(255-R);
     }
}

Thank you.
Be careful when you use cvSetImageROI~

2/19/2013

Mahalanobis distance source code between 2D normal distributions

Mahalanobis distance source code between 2D normal distributions

The formula to calculate malanobis distance from 2 distributions is follows:




This is source code

struct mean_cov{
    float mean11;
    float mean21;

    float cov11;
    float cov12;
    float cov21;
    float cov22;
};


float Dist_2Distribution( mean_cov mc1, mean_cov mc2 )
{

    float a,b,c,d;

    float A = (mc1.mean11 - mc2.mean11);
    float B = (mc1.mean21 - mc2.mean21);

    float e = 1/((mc1.cov11 + mc2.cov11)*(mc1.cov22 + mc2.cov22) - (mc1.cov12 + mc2.cov12)*(mc1.cov21 + mc2.cov21));

    a = e * (mc1.cov22 + mc2.cov22);
    b = e * -(mc1.cov12 + mc2.cov12);
    c = e * -(mc1.cov21 + mc2.cov21);
    d = e * (mc1.cov11 + mc2.cov11);

    

    return ((a*A + c*B)*A + (b*A + d*B)*B);
}

Mean, Covariance calculate source code from 2D data

The function "Get_M_C" calculates mean, covriance value of 2D data.
Input value of the function is 2D vector.
Ouput is struct that include 6 elements is consisted by 2x1 means and 2x2 covariance.


struct mean_cov{
 float mean11;
 float mean21;

 float cov11;
 float cov12;
 float cov21;
 float cov22;
};



mean_cov Get_M_C(vector< pair > &vecMC ){

 mean_cov smc;
 memset(&smc, 0, sizeof(float) * 6 );
 
 //mean first, second
 float sum_1=0;
 float sum_2=0;
 float sum_3=0;

 //mean
 int i;
 for(i=0; i < vecMC.size(); ++i)
 {
  sum_1 = sum_1 + vecMC[i].first;
  sum_2 = sum_2 + vecMC[i].second;  
 }
 smc.mean11 = sum_1 / vecMC.size();
 smc.mean21 = sum_2 / vecMC.size();

 sum_1 = 0;
 sum_2 = 0;
 sum_3 = 0;
 for(i=0; i < vecMC.size(); ++i)
 {
  sum_1 = sum_1 + ( (vecMC[i].first - smc.mean11) * (vecMC[i].first - smc.mean11) );
  sum_3 = sum_3 + ( (vecMC[i].second - smc.mean21) * (vecMC[i].second - smc.mean21) );
  sum_2 = sum_2 + ( (vecMC[i].first - smc.mean11) * (vecMC[i].second - smc.mean21) );
 }
 smc.cov11 = sum_1 / (vecMC.size()-1);
 smc.cov22 = sum_3 / (vecMC.size()-1);
 smc.cov21 = sum_2 / (vecMC.size()-1);
 smc.cov12 = sum_2 / (vecMC.size()-1);

 //cov 11
 //cov 12
 //cov 22
 //cov 21 is same with cov 12
 printf("mean11 = %lf \n", smc.mean11 );
 printf("mean21 = %lf \n", smc.mean21 );
 printf("cov11 = %lf \n", smc.cov11 );
 printf("cov12 = %lf \n", smc.cov12 );
 printf("cov21 = %lf \n", smc.cov21 );
 printf("cov22 = %lf \n", smc.cov22 );

 return smc;
}





This source code is example for using the function Get_M_C
vector< pair < float, float > > m;
mean_cov SampleMC;

for(int h=0; h< img1->height; ++h)
{
     for(int w=0; w< img1->width; ++w)
    {
        unsigned char R,G,B;
        B = (unsigned char)img1->imageData[h*img1->widthStep+w*3+0];
        G = (unsigned char)img1->imageData[h*img1->widthStep+w*3+1];
        R = (unsigned char)img1->imageData[h*img1->widthStep+w*3+2];
        float Cb,Cr;
        GetCbCrFromRGB(R, G, B, &Cb, &Cr);

        m.push_back( make_pair(Cb, Cr) );
    }
}


SampleMC = Get_M_C( m );




2/15/2013

One mouse(keyboard) and Two(three) Computers

If you want to handle two or three computer by one mouse and keyboard, this tool is very useful.
The synergy tool supports this work.

The web site is http://synergy-foss.org
The principle is using server and client of TCP/IP.

This tool is freeware and easy to use.
And it also can use on the mac os.

Thank you.




Covariance matrix example

The definition of covariance is

Cov(x, y) = E[ (X-E(X)) * (Y-E[Y]) ]

where E is abbreviation of expectation. It is same with Mean.
so..
X = [1 2 3 4 5];
E(X) -> 3 or 3.75
3 is the result of "sum(X)/5"
3.75 is the result of "sum(X)/(5-1)"

In the statistics, mean is divided by N-1 to avoid outlier data affection.


*Example of Covariance

X = [ 2 3 4 2 1 4]
Y = [ 2 4 2 1 6 8]

meanX = sum(X) / 6 -> 2.667
(X - meanX) -> [-0.6667    0.3333    1.3333   -0.6667   -1.6667    1.3333]
(X - meanX) * (X -meanX) -> [ 0.4444    0.1111    1.7778    0.4444    2.7778    1.7778]

cov(x, x) -> sum( ( (X - meanX) * (X -meanX) ) ) / (N-1)
              -> 1.4667

cov of X, Y is like that
-> cov(x,x)   cov(x,y)
     cov(x,y)   cov(y,y)

-> 1.4667  0.5333
     0.5333  7.3667


in the matlab...














2/06/2013

HMM forward algorithm - c source code

This is HMM forward algorithm source code.
I made this code today during about half the day.

I wouldn't introduce HMM and forward algorithm on this page. Because there are so many good material in the web.

The source code need Txt file included A, B, pi and observation values.
After load the txt file, the code return likelihood value.

Refer tp the real excute screen.

txt file include...
number of state, number of observation, number of observation sequence
state matrix by 1 line
observation matrix by 1 line
observation sequence

This picture describes the result of forward algorithm.

C source code






#include < stdio.h>


double HMM_forwardAlgorithm(double* &pi, double** &A, double ** &B, int* &obS, int sN, int oN, int obs_N);

void main()
{
 ///////////////////////////////////////////////
 //Input HMM ramda
 int stateN, observeN, ob_seq_N;
 double *pi;
 double **A;
 double **B;
 int *obS;
 ////////////////////////////////////////////////

 FILE * fp;
 fp = fopen("HMM_Ramda2.txt", "r");
 fscanf(fp,"%d %d %d",&stateN, &observeN, &ob_seq_N);
 /////////////////////////////////////////////////
 //alloc buffer
 pi = new double[stateN];
 A = new double*[stateN];
 B = new double*[stateN];
 obS = new int[ob_seq_N];

 for(int i=0; i< stateN; ++i)
 {
  A[i] = new double[stateN];
  B[i] = new double[observeN];
 }
 /////////////////////////////////////////////////
 //read Data 
 //pi
 for(int i=0; i< stateN; ++i)
  fscanf(fp, "%lf", &(pi[i]) );
 //A
 for(int i=0; i< stateN; ++i)
  for(int j=0; j< stateN; ++j)
   fscanf(fp, "%lf", &(A[i][j]) );
 //B
 for(int i=0; i< stateN; ++i)
  for(int j=0; j< observeN; ++j)
   fscanf(fp, "%lf", &(B[i][j]) );

 //observe sequence
 for(int i=0; i< ob_seq_N; ++i)
  fscanf(fp, "%d", &(obS[i]));

 ///////////////////////////////////////////////////
 HMM_forwardAlgorithm(pi, A, B, obS, stateN, observeN, ob_seq_N);
 

 ////////////////////////////////////////////////
 fclose(fp);

 for(int i=0; i< stateN; ++i)
 {
  delete[] A[i];
  delete[] B[i];
 }
 delete[] A;
 delete[] B;
 delete[] pi;
 delete[] obS; 

}



double HMM_forwardAlgorithm(double* &pi, double** &A, double ** &B, int* &obS, int sN, int oN, int obs_N)
{
 int i,j,t;
 //make forward matrix
 double** fMtx = new double *[sN];
 for(i=0; i< sN; ++i)
  fMtx[i] = new double[obs_N];

 //first step
 for(i=0; i< sN; ++i)
 {
  fMtx[i][0] = pi[i] * B[i][ obS[0]-1 ]; 
 }

 //routine
 double sum;
 for(t=1; t< obs_N; ++t)
 {  
  for(j=0; j< sN; ++j)
  {   
   sum=0;
   for(i=0; i< sN; ++i)
   {
    sum += (fMtx[i][t-1]*A[i][j]);    
   }
   fMtx[j][t] = sum * B[j][ obS[t]-1];
  }
 }


 //final
 sum=0; 
 for(j=0; j< sN; ++j)
 {
  sum += (fMtx[j][obs_N-1]);
 } 

 ////////////////////////////////////////////////////////
 //report
 printf("state N=%d / Observe N=%d\n\n", sN, oN);

 printf("Initial Matrix\n");
 for(int i=0; i< sN; ++i)
  printf("%lf ", pi[i] );
 printf("\n\n");

 printf("A matrix\n");
 for(int i=0; i< sN; ++i)
 {
  for(int j=0; j< sN; ++j)
  {
   printf("%lf ", A[i][j]);
  }
  printf("\n");
 }
 printf("\n");
 printf("B matrix\n");
 for(int i=0; i< sN; ++i)
 {
  for(int j=0; j< oN; ++j)
  {
   printf("%lf ", B[i][j]);
  }
  printf("\n");
 }
 printf("\n");
 printf("Observation sequence\n");
 for(int i=0; i< obs_N; ++i)
  printf("%d ", obS[i]);

 printf("\n\n");


 printf("** Trellis matrix **\n");
 for(j=0; j< sN; ++j)
 {
  for(t=0; t< obs_N; ++t)
  {
   printf("%lf  ", fMtx[j][t]);

   if(t!=obs_N-1)
    printf("->  ");
  }
  printf("\n");
 }


 printf("\nLikelihood = %lf \n", sum);

 for(i=0; i< sN; ++i)
  delete[] fMtx[i];
 delete[] fMtx;

 return sum;
}