2/19/2013

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;
}

1/31/2013

Drag and Save source code using openCV

When we study machine learning, we have to get a lot of image data.
And we should cut off only positive region on the image.

If we have many image to crop positive region, this source code is useful.

---------------------------------------------------------------------------------------------------
The method to use is..
1. input the path to crop image data.
2. number of image data 

On the image,
Click and Mouse drag ( green box line is drawn)
Space bar(save and next image)


----------------------------------------------------------------------------------------------------
environment
vs 2008
OpenCV 2.43

source code

1/25/2013

To save Txt file in the Matlab (dlmwrite function)

When you want to save vector values to Txt file, use dlmwrite function.
more detail information find in the matlab help file. (help dlmwrite)

-------------------------------------------------------
 simple example.
>>
>>dlmwrite('./saveV.txt', [1 2 3 4 5], 'delimiter', ' ');


 ->saveV.txt
1 2 3 4 5

--------------------------------------------------------

Thank you. ^^

1/13/2013

Car number plate image data

6 yeasrs ago, Han professor ask me. Don't open this image data.
But because a lot of time has passed.
I think sharing is not illegal. ^^


flate and car #1
flate and car #2
flate and car #3


346 images
< number flate only >