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

12/09/2012

Canny Edge - OpenCV Sample Code(example source)

// Origin Image
IplImage *pImage = cvLoadImage(“-”);

// Gray Image
IplImage *pGray = cvCreateImage( cvGetSize(pImage), IPL_DEPTH_8U, 1 );

// RGB to Gray
cvCvtColor(pImage, pGray, CV_RGB2GRAY);

// Edge Image
IplImage *pEdge = cvCreateImage( cvGetSize(pImage), IPL_DEPTH_8U, 1 );

// Canny Function
//Th1 is maximum value to Start tracking edge point
//If pixel point value is bigger than Th1, Edge connecting is started.

//Th2 is minimum value to stop tracking edge point
//If pixel point value is smaller than Th2, Edge connecting is ended.

cvCanny( pGray, pEdge, th1, th2 );

th1 = 10, th2=100

th1=200, th2=100










11/06/2012

YUV color format (444, 422, 411) - simple explanation

In the YUV color format, Y is bright information, U is blue color area, V is red color area.
Show the below picture. The picture is u-v color map

 
 
There are 3 type color format of YUV.
The types are 444, 422, 411.
 
 
444 is one pixel is 24 bits. 
 
 
 
 
422 is one pixel is 16 bits.
And U0, V0 is shared by Y0, Y1.



411 is one pixel is 12 bits.
 
 
 
 
This source code is convert code from YUV422 to RGB.
 

/ 


void ConvertYUV422toRGB(unsigned char** pSrc, unsigned char** pDst,int width,int height){
int i,j;
unsigned char* dst=*pDst;
unsigned char* src=*pSrc;
int Y1,Y2,U,V;
int R,G,B;

for(i=0;i2)
{
Y1=src[i*2+1];
Y2=src[i*2+3];
U=src[i*2];
V=src[i*2+2];

dst[i*3+2]=(unsigned char)((float)Y1+(1.4075f*(float)(V-128)));
dst[i*3+1]=(unsigned char)((float)Y1+(0.3455f*(float)(U-128)-(0.7169f*(float)(V-128))));
dst[i*3]=(unsigned char)((float)Y1+(1.7790f*(float)(U-128)));
dst[i*3+5]=(unsigned char)((float)Y2+(1.4075f*(float)(V-128)));
dst[i*3+4]=(unsigned char)((float)Y2+(0.3455f*(float)(U-128)-(0.7169f*(float)(V-128))));
dst[i*3+3]=(unsigned char)((float)Y2+(1.7790f*(float)(U-128)));
}
}

 


And YUV is converted by below fomulation.

R = Y + 1.4075(V-128)
G = Y - 0.3455(U-128) - 0.7169(V-128)
B = Y + 1.7790(U-128)
 
 
 
Thanks you.
 


sorry last source code is broken.
becasue html tag..
So I upload new version convert source code.
note!!! check Y position, the position is different as machine environment..
In the source code, stride means real width size.
YUV422 to RGB
--
//yuv to rgb
 int Y,U,V;   
 int R1,G1,B1;
 for(int ih=0; ihm_height; ++ih)
 {   
  //int iww=0;
  for(int iw=0; iwm_width; iw++)
  {
  
   if(iw %2 == 0)
   {
    Y = obj->m_yuv[ih*obj->m_stride + iw*2 + 0];
    U = obj->m_yuv[ih*obj->m_stride + iw*2 + 1];
    V = obj->m_yuv[ih*obj->m_stride + iw*2 + 3];
   }else{
    Y = obj->m_yuv[ih*obj->m_stride + iw*2 + 0];
    U = obj->m_yuv[ih*obj->m_stride + iw*2 -1];
    V = obj->m_yuv[ih*obj->m_stride + iw*2 + 1];
   }
   
   R1 = Y + 1.4075*(V-128);
   G1 = Y - 0.3455*(U-128) - 0.7169*(V-128);
   B1 = Y + 1.7790*(U-128);

   o_Image2.at(ih, iw)[0] = BYTE(MMIN(255, MMAX(0, B1)));// B1;
   o_Image2.at(ih, iw)[1] = BYTE(MMIN(255, MMAX(0, G1)));// G1;
   o_Image2.at(ih, iw)[2] = BYTE(MMIN(255, MMAX(0, R1)));// R1;
  }
 }
--
RGB to YUV422
--
int Y,U,V;   
   int R1,G1,B1;
   for(int ih=0; ih< obj->m_height; ++ih)
   {   
    //int iww=0;
    for(int iw=0; iw< obj->m_width; iw++)
    {

     B1 = o_Image2.at< cv::Vec3b>(ih, iw)[0];// B1;
     G1 = o_Image2.at< cv::Vec3b>(ih, iw)[1];// G1;
     R1 = o_Image2.at< cv::Vec3b>(ih, iw)[2];// R1;

     Y = (0.257*R1) + (0.504*G1) + (0.098*B1) +16;
     U = -(0.148*R1) - (0.291*G1) + (0.439*B1) + 128;
     V = (0.439*R1 ) - (0.368*G1) - (0.071*B1) + 128;
     Y = MMIN(255, MMAX(0, Y));
     U = MMIN(255, MMAX(0, U));
     V = MMIN(255, MMAX(0, V));

     if(iw %2 == 0)
     {
      obj->m_yuv[ih*obj->m_stride + iw*2 + 0] = Y;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 1] = U;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 3] = V;
     }else{
      obj->m_yuv[ih*obj->m_stride + iw*2 + 0] = Y;
      obj->m_yuv[ih*obj->m_stride + iw*2 -1] = U;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 1] = V;
     }     
    }
--
 


11/01/2012

clapack example source and method to use


clapack is math library to solve linear algebra in C language.
Originally, it is library to use in fortran language.
but clapack is made to use in C language.

1. You can see the method to use and file download on this site(http://icl.cs.utk.edu/lapack-for-windows/index.html)

Or

2. You can download pre-bulit clapack lib in here. I am using this lib now.
It might not run well, if the environment of your computer was different with me.
My environment is window 7 32bit, vs 2008.
The method to use is easy.
- Copy lib, header files in your project (The compressed file includes 'blas.lib, blasd.lib, lapack.lib, lapackd.lib, libf2c.lib, libf2cd.lib, clapack.h, f2c.h'.)
- Set Additional Dependencies on project property.
Add these libs "blas.lib lapack.lib libf2c.lib"
- include this header file on your source code.
#include "f2c.h"
#include "clapack.h"
- use functions of lapack.


this is example source code.
/*
    DGESV Example.
    ==============
  
    The program computes the solution to the system of linear
    equations with a square matrix A and multiple
    right-hand sides B, where A is the coefficient matrix:
  
      6.80  -6.05  -0.45   8.32  -9.67
     -2.11  -3.30   2.58   2.71  -5.14
      5.66   5.36  -2.70   4.35  -7.26
      5.97  -4.44   0.27  -7.17   6.08
      8.23   1.08   9.04   2.14  -6.87
 
   and B is the right-hand side matrix:
  
      4.02  -1.56   9.81
      6.19   4.00  -4.09
     -8.22  -8.67  -4.57
     -7.57   1.75  -8.61
     -3.03   2.86   8.99
  
    Description.
    ============
  
    The routine solves for X the system of linear equations A*X = B,
    where A is an n-by-n matrix, the columns of matrix B are individual
    right-hand sides, and the columns of X are the corresponding
    solutions.
 
   The LU decomposition with partial pivoting and row interchanges is
    used to factor A as A = P*L*U, where P is a permutation matrix, L
    is unit lower triangular, and U is upper triangular. The factored
    form of A is then used to solve the system of equations A*X = B.
 
   Example Program Results.
    ========================
  
  DGESV Example Program Results
 
 Solution
   -0.80  -0.39   0.96
   -0.70  -0.55   0.22
    0.59   0.84   1.90
    1.32  -0.10   5.36
    0.57   0.11   4.04
 
 Details of LU factorization
    8.23   1.08   9.04   2.14  -6.87
    0.83  -6.94  -7.92   6.55  -3.99
    0.69  -0.67 -14.18   7.24  -5.19
    0.73   0.75   0.02 -13.82  14.19
   -0.26   0.44  -0.59  -0.34  -3.43
 
 Pivot indices
       5      5      3      4      5
 */

#include 
#include 

#include "f2c.h"
#include "clapack.h"


 extern void print_matrix( char* desc, int m, int n, double* a, int lda );
 extern void print_int_vector( char* desc, int n, int* a );
 
/* Parameters */
 #define N 5
 #define NRHS 3
 #define LDA N
 #define LDB N
 
/* Main program */
 int main() {
         /* Locals */
         integer n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
         /* Local arrays */
         integer ipiv[N];
         double a[LDA*N] = {
             6.80, -2.11,  5.66,  5.97,  8.23,
            -6.05, -3.30,  5.36, -4.44,  1.08,
            -0.45,  2.58, -2.70,  0.27,  9.04,
             8.32,  2.71,  4.35, -7.17,  2.14,
            -9.67, -5.14, -7.26,  6.08, -6.87
         };
         double b[LDB*NRHS] = {
             4.02,  6.19, -8.22, -7.57, -3.03,
            -1.56,  4.00, -8.67,  1.75,  2.86,
             9.81, -4.09, -4.57, -8.61,  8.99
         };
         /* Executable statements */
         printf( " DGESV Example Program Results\n" );
         /* Solve the equations A*X = B */
         dgesv_( &n, &nrhs, a, &lda, ipiv, b, &ldb, &info );
         /* Check for the exact singularity */
         if( info > 0 ) {
                 printf( "The diagonal element of the triangular factor of A,\n" );
                 printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
                 printf( "the solution could not be computed.\n" );
                 exit( 1 );
         }
         /* Print solution */
         print_matrix( "Solution", n, nrhs, b, ldb );
         /* Print details of LU factorization */
         print_matrix( "Details of LU factorization", n, n, a, lda );
         /* Print pivot indices */
         print_int_vector( "Pivot indices", n, (int*)ipiv );
         exit( 0 );
 } /* End of DGESV Example */
 
/* Auxiliary routine: printing a matrix */
 void print_matrix( char* desc, int m, int n, double* a, int lda ) {
         int i, j;
         printf( "\n %s\n", desc );
         for( i = 0; i < m; i++ ) {
                 for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );
                 printf( "\n" );
         }
 }
 
/* Auxiliary routine: printing a vector of integers */
 void print_int_vector( char* desc, int n, int* a ) {
         int j;
         printf( "\n %s\n", desc );
         for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
         printf( "\n" );


 }





This site is useful to know what functions are exist. but there is not source code. http://www.calerga.com/doc/LME_lapk.htm This site is very useful, there are descriptions and  example source codes. http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/index.htm#dgesv.htm












10/25/2012

Inner product(the angle between the two lines) - function source code




If there are 3 point(c, p1, p2), as shown in the above figure.
This function got the angle between the two lines using inner product.
Even if the name changed from (p1, p2) to (p2, p1),  the function get the narrow angle between the two lines.

I hope this code useful to you.
thank you.


float innerAngle(float px1, float py1, float px2, float py2, float cx1, float cy1)
{

 float dist1 = sqrt(  (px1-cx1)*(px1-cx1) + (py1-cy1)*(py1-cy1) );
 float dist2 = sqrt(  (px2-cx1)*(px2-cx1) + (py2-cy1)*(py2-cy1) );

 float Ax, Ay;
 float Bx, By;
 float Cx, Cy;

 //find closest point to C
 //printf("dist = %lf %lf\n", dist1, dist2);

 Cx = cx1;
 Cy = cy1;
 if(dist1 < dist2)
 {  
  Bx = px1;
  By = py1;  
  Ax = px2;
  Ay = py2;


 }else{
  Bx = px2;
  By = py2;
  Ax = px1;
  Ay = py1;
 }


 float Q1 = Cx - Ax;
 float Q2 = Cy - Ay;
 float P1 = Bx - Ax;
 float P2 = By - Ay;  


 float A = acos( (P1*Q1 + P2*Q2) / ( sqrt(P1*P1+P2*P2) * sqrt(Q1*Q1+Q2*Q2) ) );

 A = A*180/PI;

 return A;
}




10/23/2012

Optical Flow sample source code using OpenCV



Optical Flow sample source code using OpenCV.
It programed based on http://feelmare.blogspot.kr/2012/10/make-2-frame-having-time-interval-in.html source code for time difference frame.

#define MAX_COUNT 250   
#define DELAY_T 20   
#define PI 3.1415   


void main()   
{   

 //////////////////////////////////////////////////////////////////////////   
 //image class         
 IplImage* image = 0;   

 //T, T-1 image   
 IplImage* current_Img = 0;   
 IplImage* Old_Img = 0;   

 //Optical Image   
 IplImage * imgA=0;   
 IplImage * imgB=0;   


 //Video Load   
 CvCapture * capture = cvCreateFileCapture("1.avi"); //cvCaptureFromCAM(0); //cvCreateFileCapture("1.avi");   

 //Window   
 cvNamedWindow( "Origin" );   
 //////////////////////////////////////////////////////////////////////////   


 //////////////////////////////////////////////////////////////////////////    
 //Optical Flow Variables    
 IplImage * eig_image=0;
 IplImage * tmp_image=0;
 int corner_count = MAX_COUNT;   
 CvPoint2D32f* cornersA = new CvPoint2D32f[ MAX_COUNT ];   
 CvPoint2D32f * cornersB = new CvPoint2D32f[ MAX_COUNT ];   

 CvSize img_sz;   
 int win_size=20;   

 IplImage* pyrA=0;   
 IplImage* pyrB=0;   

 char features_found[ MAX_COUNT ];   
 float feature_errors[ MAX_COUNT ];   
 //////////////////////////////////////////////////////////////////////////   


 //////////////////////////////////////////////////////////////////////////   
 //Variables for time different video   
 int one_zero=0;   
 int t_delay=0;   



 //Routine Start   
 while(1) {      


  //capture a frame form cam      
  if( cvGrabFrame( capture ) == 0 )   
   break;   

  //Image Create   
  if(Old_Img == 0)      
  {      
   image = cvRetrieveFrame( capture );   
   current_Img = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);      
   Old_Img  = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);

   

  }   



  if(one_zero == 0 )   
  {   
   if(eig_image == 0)
   {
    eig_image = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
    tmp_image = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
   }

   //copy to image class   
   memcpy(Old_Img->imageData, current_Img->imageData, sizeof(char)*image->imageSize );   
   image = cvRetrieveFrame( capture );   
   memcpy(current_Img->imageData, image->imageData, sizeof(char)*image->imageSize );   

   //////////////////////////////////////////////////////////////////////////   
   //Create image for Optical flow   
   if(imgA == 0)   
   {   
    imgA = cvCreateImage( cvSize(image->width, image->height), IPL_DEPTH_8U, 1);   
    imgB = cvCreateImage( cvSize(image->width, image->height), IPL_DEPTH_8U, 1);       
   }      

   //RGB to Gray for Optical Flow   
   cvCvtColor(current_Img, imgA, CV_BGR2GRAY);   
   cvCvtColor(Old_Img, imgB, CV_BGR2GRAY);      

   //   
   cvGoodFeaturesToTrack(imgA, eig_image, tmp_image, cornersA, &corner_count, 0.01, 5.0, 0, 3, 0, 0.04);   
   cvFindCornerSubPix(imgA, cornersA, corner_count, cvSize(win_size, win_size), cvSize(-1, -1), cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03));      


   CvSize pyr_sz = cvSize( imgA->width+8, imgB->height/3 );   
   if( pyrA == 0)   
   {    
    pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1);   
    pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1);   
   }   

   cvCalcOpticalFlowPyrLK( imgA, imgB, pyrA, pyrB, cornersA, cornersB, corner_count, cvSize(win_size, win_size), 5, features_found, feature_errors, cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3), 0);   

   /////////////////////////////////////////////////////////////////////////      

   for(int i=0; i< corner_count; ++i)
   {

    if( features_found[i] == 0 || feature_errors[i] > MAX_COUNT )
     continue;   



    //////////////////////////////////////////////////////////////////////////       
    //Vector Length   
    float fVecLength = sqrt((float)((cornersA[i].x-cornersB[i].x)*(cornersA[i].x-cornersB[i].x)+(cornersA[i].y-cornersB[i].y)*(cornersA[i].y-cornersB[i].y)));   
    //Vector Angle   
    float fVecSetha  = fabs( atan2((float)(cornersB[i].y-cornersA[i].y), (float)(cornersB[i].x-cornersA[i].x)) * 180/PI );   

    cvLine( image, cvPoint(cornersA[i].x, cornersA[i].y), cvPoint(cornersB[i].x, cornersA[i].y), CV_RGB(0, 255, 0), 2);     

    printf("[%d] - Sheta:%lf, Length:%lf\n",i , fVecSetha, fVecLength);   
   }


   //////////////////////////////////////////////////////////////////////////       

  }   
  cvShowImage( "Origin", image );   

  //////////////////////////////////////////////////////////////////////////   

  //time delay   
one_zero++;
  if( (one_zero % DELAY_T ) == 0)   
  {      
   one_zero=0;   
  }   

  //break      
  if( cvWaitKey(10) >= 0 )      
   break;      
 }      

 //release capture point      
 cvReleaseCapture(&capture);   
 //close the window      
 cvDestroyWindow( "Origin" );      

 cvReleaseImage(&Old_Img);    
 //////////////////////////////////////////////////////////////////////////   
 cvReleaseImage(&imgA);   
 cvReleaseImage(&imgB);    
 cvReleaseImage(&eig_image);
 cvReleaseImage(&tmp_image);
 delete cornersA;   
 delete cornersB;    
 cvReleaseImage(&pyrA);   
 cvReleaseImage(&pyrB);   


 //////////////////////////////////////////////////////////////////////////   
}   





Thank you.

10/17/2012

Make 2 frame having time interval in video using OpenCV(Example source code)

This code is example source code that made 2 frame having time interval.
For example : this code made 2 images t time and t-x time in video stream.


This images shows origin image(Left), T time Image(center), T-x time Image(right)



This is example source code
You can control time delay frame using DELAY_T macro variable.

#define DELAY_T 30

void main()
{

  //image class      
    IplImage* image = 0;   
 IplImage* image1 = 0;    
 IplImage* image2 = 0;    

 
    //camera point   
 CvCapture * capture = cvCreateFileCapture("RunF.avi"); //cvCaptureFromCAM(0); //cvCreateFileCapture("1.avi");

 //make window   
 cvNamedWindow("Origin");
    cvNamedWindow( "t");  
 cvNamedWindow( "t-1");   
   
    
 int one_zero=0;
    
    while(1) {   


  //capture a frame form cam   
        if( cvGrabFrame( capture ) == 0)
   break;
  image = cvRetrieveFrame( capture );

  
  if(image2 == 0)   
        {   
   //image = cvRetrieveFrame( capture );
   image1  = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);  
            image2  = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);   
        }


  //show window   
  cvShowImage( "Origin", image);

  if(one_zero == 0 )
  {
   //copy to image class
   memcpy(image2->imageData, image1->imageData, sizeof(char)*image->imageSize );
   //image = cvRetrieveFrame( capture );
   memcpy(image1->imageData, image->imageData, sizeof(char)*image->imageSize );

   
   //show window   
   cvShowImage( "t", image1 );
   cvShowImage( "t-1", image2 );

  }

  //Time Delay
  one_zero++;
  if( (one_zero%DELAY_T) == 0)
  {   
   one_zero=0;
  }

 
        //break   
        if( cvWaitKey(10) >= 0 )   
            break;   
    }   
  
    
    //release capture point   
    cvReleaseCapture( &capture );

    //close the window   
    cvDestroyAllWindows();   

 //release Images
 cvReleaseImage(&image1);
 cvReleaseImage(&image2);

}

Thank you.