4/26/2012

(TIP) Be careful when you use this type "unsigned long long type"


Firstly, refer to this webpage(http://stackoverflow.com/questions/2844/how-do-you-printf-an-unsigned-long-long-int)


unsigned long long -> %llu

When you use this type -> unsigned long long llType;
You have to be careful when you use :printf, fprintf, sprintf...
you have to use "%llu".
ex) printf("%llu", llType);

Or, You will meet this problem.

Source:
unsigned long long int num = 285212672; 
int normalInt = 5;      
printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);

Output : 
My number is 8 bytes wide and its value is 285212672l. A normal number is 0.




I takes about 2hours to solve this problem...





(TIP) C++ Make folder (CreateDirectory)


The method to create folder is esay.
You can do this jop using window api.

this is sample code for making folder

//make path string 
char buf[MAX_PATH];
DWORD dir = GetCurrentDirectory(MAX_PATH, buf);
CString directory = buf;
directory += "\\New_Folder";

//make folder
if(!CreateDirectory(directory,NULL))
//check error
         switch (GetLastError()) {
           case ERROR_ALREADY_EXISTS: //Folder exists
 break;
     default:
 ;
}
//end ^^

4/25/2012

Convert from FlyCapture(Point grey) to OpenCV (Source code)

Convert from FlyCapture(Point grey) to OpenCV (Source code)

My environment :
FlyCapture 2.x
OpenCV 2.3
VS 2010


This source code is referred to this site (in korean).

*You have to set to use flycapture lib.

-Firstly, set "include, lib" directory on your vs studio.
I have set this path to my vs option.
"C:\Program Files (x86)\Point Grey Research\FlyCapture2\include"
"C:\Program Files (x86)\Point Grey Research\FlyCapture2\lib"

-Second, set Additional Dependencies on project property.
Add these libs
"flycapture2.lib FlyCapture2GUI.lib"

-Third, include this header file on your source code.
#include "FlyCapture2.h"
#include "FlyCapture2GUI.h"


Below source code is core part to convert flycaptuer to opencv.
This part is included in the linked surce code.
In detail, please refer to my source code.

Thank you.

[source code]




// Camera GUID acquisition 카메라 GUID νšλ“
 m_error = m_BusManager.GetCameraFromIndex(0,&m_Guid);
 // Camera connection 카메라 μ—°κ²°
 m_error = m_Cam.Connect(&m_Guid);
 // Grap start μ˜μƒ νšλ“ μ‹œμž‘
 m_error = m_Cam.StartCapture();

 m_pDC=GetDC();
 m_pDC->SetStretchBltMode(COLORONCOLOR);
 m_bitmapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
 m_bitmapInfo.bmiHeader.biPlanes=1;
 m_bitmapInfo.bmiHeader.biCompression=BI_RGB;
 m_bitmapInfo.bmiHeader.biXPelsPerMeter=100;
 m_bitmapInfo.bmiHeader.biYPelsPerMeter=100;
 m_bitmapInfo.bmiHeader.biClrUsed=0;
 m_bitmapInfo.bmiHeader.biClrImportant=0;
 m_bitmapInfo.bmiHeader.biBitCount=24;
 m_bitmapInfo.bmiHeader.biSizeImage=0;


 while(m_ThreadContinue)
 {
  m_error = m_Cam.RetrieveBuffer(&m_Image);

  // convert to RGB type νšλ“ν•œμ˜μƒRGB ν˜•νƒœλ‘œλ³€ν™˜
  m_error = m_Image.Convert(PIXEL_FORMAT_BGR, &m_ImageColor);

  if( CvImg == NULL)
   CvImg = cvCreateImage(cvSize(m_ImageColor.GetCols(),m_ImageColor.GetRows()),8,3);
  memcpy(CvImg->imageDataOrigin,m_ImageColor.GetData() ,m_ImageColor.GetCols()*m_ImageColor.GetRows()*3);
  
  /*
  //Simple Processing
  for(int i=0; iheight; ++i)
  {
   for(int j=0; jwidth; ++j)
   {
    CvImg->imageData[i*CvImg->widthStep+j*3+2] = 255 - CvImg->imageData[i*CvImg->widthStep+j*3+2];
    CvImg->imageData[i*CvImg->widthStep+j*3+1] = 255 - CvImg->imageData[i*CvImg->widthStep+j*3+1];
    CvImg->imageData[i*CvImg->widthStep+j*3+0] = 255 - CvImg->imageData[i*CvImg->widthStep+j*3+0];
   }
  }
  */

  m_bitmapInfo.bmiHeader.biWidth=m_Image.GetCols();
  m_bitmapInfo.bmiHeader.biHeight=-m_Image.GetRows();  
  StretchDIBits(m_pDC->GetSafeHdc(),0,0,320,240,0,0,m_ImageColor.GetCols(),m_ImageColor.GetRows(),CvImg->imageDataOrigin,&m_bitmapInfo,DIB_RGB_COLORS,SRCCOPY);
 }

 
 ReleaseDC(m_pDC);
 SetEvent(m_heventThreadDone);




4/02/2012

The source code to get joystick input value(C++,MFC)

My Environment : MS VS 2008 & MFC(Dialog Based)
Joy Stick : Logitech Extreme 3D pro (XBox Type)
Cteated Date : 2012. 03
[source code]





We use dxShow to get joystick input value.
So we need these files "mmsystem.h", "winmm.lib", "winmm.dll". But normally, there are already in your computer.

The method to get value of joystick is very easy.
First, to confirm whether the joystick is connected well or not. And calibrate range by control pannel in window.

Second, cording~
#include "mmsystem.h"

JOYINFOEX joyInfoEx;
 joyInfoEx.dwSize = sizeof(joyInfoEx);
 joyGetDevCaps(JOYSTICKID1, &joyCaps, sizeof(joyCaps));
 JoyPresent = (joyGetPosEx(JOYSTICKID1, &joyInfoEx) == JOYERR_NOERROR);

JOYINFOEX joyInfoEx;
 if (JoyPresent)
 {
  joyInfoEx.dwSize = sizeof(joyInfoEx);
  joyInfoEx.dwFlags = JOY_RETURNALL;
  joyGetPosEx(JOYSTICKID1, &joyInfoEx);
 }

//And you can confirm button click and stick moving value

 if (joyInfoEx.dwButtons & 0x1) //then do whatever
 {
  str.Format("button1");
  m_ListBox2.AddString(str);
 }

 if (joyInfoEx.dwButtons & 0x2) //then do whatever
 {
  str.Format("button2");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x4) //then do whatever
 {
  str.Format("button3");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x8) //then do whatever
 {
  str.Format("button4");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x10) //then do whatever
 {
  str.Format("button5");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x20) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x40) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }
 if (joyInfoEx.dwButtons & 0x80) //then do whatever
 {
  str.Format("button6");
  m_ListBox2.AddString(str);
 }


if ( joyInfoEx.dwPOV == 0 )
 {
  str.Format("Up");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 4500){
  str.Format("Up+Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 9000){
  str.Format("Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 13500){
  str.Format("Down+Right");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 18000){
  str.Format("Down");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 22500){
  str.Format("Down+Left");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 27000){
  str.Format("Left");
  m_ListBox2.AddString(str);
 }else if( joyInfoEx.dwPOV == 31500){
  str.Format("Up+Left");
  m_ListBox2.AddString(str);
 }if ( joyInfoEx.dwPOV & 0x1 ){
  str.Format("No Input");
  m_ListBox2.AddString(str);
 }

str.Format("X:%d Y:%d Z:%d", joyInfoEx.dwXpos, joyInfoEx.dwYpos, joyInfoEx.dwZpos);  m_ListBox.AddString(str); str.Format("R:%d U:%d V:%d", joyInfoEx.dwRpos, joyInfoEx.dwUpos, joyInfoEx.dwVpos);  m_ListBox3.AddString(str);


Thank you~
Everybody Fighting !!!

(TIP) ListBox contents count limit to prevent memory overflow and to show the latest list

(TIP) ListBox contents count limit to prevent memory overflow and to show the latest list

int nCount = m_ListBox1.GetCount();
//Contents count limit
if(nCount > 1000)
     m_ListBox1.ResetContent();

//To view the last content
if ( nCount > 0 )
     m_ListBox1.SetCurSel( nCount-1 );