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.