1/12/2013

CvFilter2D example source code, various Filter masks

double K[] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };

float t=0;
for(int i=0; i< (3*3); ++i)
     t = t + K[i];
for(int i=0; i< (3*3); ++i)
     K[i] = K[i] / t;

CvMat Kernel=cvMat(3, 3, CV_64FC1, K); 
cvFilter2D(InputImg,OutputImg, &Kernel);


various type of filter mask
 
Gaussian Filter 
Average Filter
High pass Filter
Horizontal Prewitt Filter
Vertical Prewitt Filter
Horizontal Sobel Filter
Vertical Sobel Filter
Laplacian Filter
Sharpen Filter
 
Sharpen Low Filter

1/10/2013

C file Write, Read by Binary - example source code

C file Write, Read by Binary - example source code


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


void main()
{


 //////////////////////////////////////////////////////////////////////////
 //File Write by binary
 srand( time(0));
 float feature[10];
 for(int i=0; i<10; ++i) 
  feature[i] = rand()/100; 

 //data confirm
 for(int i=0; i< 10; ++i) 
  printf("%lf\n", feature[i] );

 FILE * fp;
 fp = fopen("output.txt", "wb");
 fwrite(feature, sizeof(float), 10, fp);
 fclose(fp);

 printf("//////////////////////////////////////////////////////////////////////////\n");


 //////////////////////////////////////////////////////////////////////////
 //File Read by binary
 float feature_r[10];
 FILE* fp2;
 fp2 = fopen("output.txt", "rb");
 fread(feature_r, sizeof(float), 10, fp2);
 fclose(fp2);

 //data confirm
 for(int i=0; i< 10; ++i) 
  printf("%lf\n", feature_r[i] );
 //////////////////////////////////////////////////////////////////////////


}




881A sonar sensor - data acquisition source code

881A sonar sensor - data acquisition source code





source code >

This code is also made by VS 2008.
This source code included 'interface_881A' class.
You can get the data easily using the class.





//port open
CString strPort = "COM5";
int baudrate = 115200;
int data = 8;
int parity = 0;
int stop = 0;//1;
C881A.OpenPort2(strPort,baudrate,data,parity,stop);


//request data
C881A.SendData();


//data acquisition part
//The data is saved in the Command_881A_GetData struct
CString str;
 str.Format("%s", C881A.ReceiveData.DataHeader.c_str());
 m_List.AddString(str);

 str.Format("Head ID : %d", C881A.ReceiveData.HeadID);
 m_List.AddString(str);

 str.Format("Head Position : %lf", C881A.ReceiveData.HeadPosition);
 m_List.AddString(str);

 str.Format("Head direction : %d", C881A.ReceiveData.Direction);
 m_List.AddString(str);


 str.Format("Range : %d", C881A.ReceiveData.Range);
 m_List.AddString(str);

 str.Format("Profile Range : %d", C881A.ReceiveData.ProfileRange);
 m_List.AddString(str);

 str.Format("Data Byte : %d", C881A.ReceiveData.DataBytes);
 m_List.AddString(str);
 

 m_List10.ResetContent();
 for(int i=0; i< 500; ++i)
 {
  str.Format("[%d] = %d", i, C881A.ReceiveData.Data[i]);
  m_List10.AddString(str);
 }
 
///////////////////////////


//close port
C881A.ClosePort();



Thank you.


3DM-GX3 IMU sensor - Data acquisition source code

3DM-GX3 IMU sensor - Data acquisition source code


Code is made by Visual studio 2008(C++).


There is "3DM_GX3_interface" class In the source code.
You can get IMU data easily using the class.



//Open Port 
CString strPort = "COM7";
int baudrate = 115200; //19200;
int data = 8;
int parity = 0;
int stop = 0;//1;
C3DM_GX3.OpenPort2(strPort,baudrate,data,parity,stop);


//Request Data to the IMU, This function need one time when start to acquisition.
C3DM_GX3.SendData()

//Get Data loop until end.
C3DM_GX3.ReceiveData.roll/3.14*180 ;
C3DM_GX3.ReceiveData.pitch/3.14*180 ;
C3DM_GX3.ReceiveData.yaw/3.14*180 ;

//Close Port
C3DM_GX3.ClosePort();




"How to change 4byte bit data to float" is used to get the roll, yaw, pitch.
The method is introduced on my blog here.


this code is useful to you.
Thank you.

1/02/2013

Matlab dimension change function -> reshape

Useful function ~ ^^

A = magic(3)

A =
     8     1     6
     3     5     7
     4     9     2

>> B = reshape(A, 1, 3*3)

B =
     8     3     4     1     5     9     6     7     2


Image Normalization using Standard deviation - example source code

You can understand Standard deviation normalization, referenced on this web page.
http://www.d.umn.edu/~deoka001/Normalization.html
This web page introduce that..(wrote by Siddharth Deokar)

-------------------------------------------------------------------------
Normalization by Standard Deviation
We normalize the attribute values by using standard deviation.

For Example:

Consider 5 instances which has attribute A with the follwing values: {-5, 6, 9, 2, 4}

First we calculate the mean as follows:

Mean = (-5+6+9+2+4) / 5 = 3.2

Second, we subtract the mean from all the values and square them:

(-5-3.2)^2 = 67.24
(6-3.2)^2 = 7.84
(9-3.2)^2 = 33.64
(2-3.2)^2 = 1.44
(4-3.2)^2 = 0.64

Then we find the deviation as follows:

Deviation = sqrt ((67.24 + 7.84 + 33.64 + 1.44 + 0.64) / 5) = 4.71

Now we normalize the attribute values:

x => (x - Mean) / Deviation

-5 => (-5 - 3.2) / 4.71 = -1.74
6 => (6 - 3.2) / 4.71 = 0.59
9 => (9 - 3.2) / 4.71 = 1.23
2 => (2 - 3.2) / 4.71 = -0.25
4 => (4 - 3.2) / 4.71 = 0.17

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



This is sample source code:

//

int i,j,z;
 double sum=0; 
 double Mean;
 
 //sum, mean
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {
   sum = sum + Pdata[i][j];
  }
 }

 Mean = sum/(Height*Width);
 
 //Deviation
 double dSum=0;
 double deviation;
 double std_dev; 
 
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {        
   Pdata[i][j] = (Pdata[i][j] - Mean);

   dSum = dSum + (Pdata[i][j]*Pdata[i][j]);
  }
 }
 
 deviation = dSum / (Width*Height);
 std_dev = sqrt(deviation);
 
 //Normalization
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {        
   Pdata[i][j] = (Pdata[i][j] / std_dev);
  }
 }

12/26/2012

My first Arduino art (5led on/off example source)

The advantage of Arduino is beginners can access electrical and electronic experiment easily.
I think the reason is the arduino support abundant examples  and functions.
Nervertheless I don't know ATmega, I could led on/off easily.

This is demo video and picture.
To experiment my case, you need arduino board, resistance 220om and led.

And to setup your arduino is explained very well on the homepage.
http://arduino.cc/en/Guide/HomePage
http://arduino.cc/en/Guide/Windows





Thank you.



source code
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int mode=0;
int order=1;
int count=0;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led-1,OUTPUT); 
  pinMode(led-2,OUTPUT);
  pinMode(led-3,OUTPUT);
  pinMode(led-4,OUTPUT);
      
}

// the loop routine runs over and over again forever:
void loop() { 

  if(mode == 0)
    count = count+1;
  else
    count = count-1;

  for(int i=0; i< 5 i=i+1) {
    if(count%5 == i)
      digitalWrite(led-i, HIGH);   // turn the LED on (HIGH is the voltage level)     
    else
      digitalWrite(led-i, LOW);   // turn the LED on (HIGH is the voltage level)   
  }
  delay(200);

  if(mode == 0)
  {
    if(count == 50)     
      mode=1;
  }else{
    if(count == 0)
      mode=0;   
  } 
}