2012년 8월 12일 일요일

Opening image/avi/webcam on MFC dialog window using OpenCV2.3

In the previous post, I opened image/avi/webcam on console window with OpenCV library. Sometimes, console window could be inconvenient for user, because it bases on DOS command. MFC that is a kind of object-based programming tool provides class libraries for  development of window application program. Understanding MFC is not easy for developer, but  for user, it provide good UI(user interface).
Consequently, studying MFC deserve it..
From now on, I will study both MFC and Window API programming with OpenCV2.3.
Anyway this post is about opening image/avi/webcam on MFC dialog window.


Before start, run the Visual studio2008 and make new project. Select MFC Application and dialog based like images shown below.



What a weird configuration!
Don't be confused. 

First, let's set up OpenCV library on it.

And include cv.h and highgui.h in C****Dlg.h

Second, go to resource view and make picture controls and buttons like below.

Add each picture control to variable and name its ID.




Now, a function is needed for convert image to bitmap because MFC support only bmp files. Until OpenCV 2.2, a class named CvvImage is provided and that class provides useful function for using image file in MFC, but from ver2.3, CvvImage class is not provided. So, Some functions that can change any image to bitmap file is needed. 


It is a little bit hard opening image on MFC without CvvImage, but fortunately I found a function in OpenCV Korea cafe.

The function is as follows:
 -------------------------------------------------------------------------------------
void DisplayIplImg(IplImage* pImgIpl, CDC* pDC, CRect rect)
{
 BITMAPINFO bitmapInfo;
 bitmapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
 bitmapInfo.bmiHeader.biPlanes=1;
 bitmapInfo.bmiHeader.biCompression=BI_RGB;
 bitmapInfo.bmiHeader.biXPelsPerMeter=100;
 bitmapInfo.bmiHeader.biYPelsPerMeter=100;
 bitmapInfo.bmiHeader.biClrUsed=0;
 bitmapInfo.bmiHeader.biClrImportant=0;
 bitmapInfo.bmiHeader.biSizeImage=0;
 bitmapInfo.bmiHeader.biWidth=pImgIpl->width;
 bitmapInfo.bmiHeader.biHeight=-pImgIpl->height;
 IplImage* tempImage;
 if(pImgIpl->nChannels == 3)
 {
  tempImage = (IplImage*)cvClone(pImgIpl);
  bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;
 }
 else if(pImgIpl->nChannels == 1)
 {
  tempImage =  cvCreateImage(cvGetSize(pImgIpl), IPL_DEPTH_8U, 3);
  cvCvtColor(pImgIpl, tempImage, CV_GRAY2BGR);
  bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;
 }
 pDC->SetStretchBltMode(COLORONCOLOR);
 ::StretchDIBits(pDC->GetSafeHdc(), rect.left, rect.top, rect.right, rect.bottom,
  0, 0, tempImage->width, tempImage->height, tempImage->imageData, &bitmapInfo,
  DIB_RGB_COLORS, SRCCOPY);
 cvReleaseImage(&tempImage);
}
--------------------------------------------------------------------------------

Among 3 variables used in the function, pImgIpl means point variable of image loaded by OpenCV,  pDC means structure information where to open image, and rect means size of where to open image. pDC and rect should be declared by CDC class and CRect class variables before using. 
  
Let's declare variables.



IplImage* image;
CDC* pDC1;
CRect rect1;
These variables are for load image on picture control. 
And then go to OnInitDialog function that is pre-process as soon as run this project.
Type like below.

--------------------------------------------------------
 image = cvLoadImage( "test.jpg" );
 pDC1 = m_pic.GetDC();
 m_pic.GetClientRect(&rect1);
---------------------------------------------------------



Now, add function to display image.



Name of Function is DisplayIplImg, and input each parameter on it
-----------------------------------------------------------------------
void DisplayIplImg(IplImage* pImgIpl, CDC* pDC, CRect rect)


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

And then go to where the function is declared, copy and paste codes of function


Third, move on to dialog window and double click Load Image button. In there you can make some event.Input below codes in the event function.

-------------------------------------------------------------------
DisplayIplImg(image, pDC1,rect1);
--------------------------------------------------------------------




Move on to dialog window againg and double click close button. In there, input below codes.
-----------------------------------------------------------------------
cvReleaseImage(&image);
-----------------------------------------------------------------------




Now, Let's code for the video clip and webcam to open on dialog window. Both of two are similar. outline is as follows:

1) Declare variables

2) Get the information of picture control
    start capture from video and save buffer for moment

3) Set the timer and call and display buffer image when timer on





1. Declare variables
use Add->Variables  on c***Dlg class and input below
------------------------------------
IplImage* m_FrameVideo;
IplImage* m_FrameWebcam;
CvCapture* captureAVI;
CvCapture* captureCAM;
CDC* pDC2;
CDC* pDC3;
CRect rect2;
CRect rect3;
-------------------------------------

2. Go to initializing function of the class. You can see it easily as double clicking on C***Dlg class. It is same place where you input codes for opening image.

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

captureAVI = cvCreateFileCapture( "test.avi" );
captureCAM = cvCaptureFromCAM(0);

pDC2 = m_video.GetDC();
m_video.GetClientRect(&rect2);
pDC3 = m_cam.GetDC();
m_cam.GetClientRect(&rect3);
----------------------------------------------------------------------------



3. In dialog window, just double clink on Open VideoClip button. Then input below codes.
    Repeat again on WebCAM button.

-------------------------------------------------------------
//(In Open VideoClip button)
SetTimer(1,30,NULL);
---------------------------------------------------------------
//(In Open WebCAM button)
SetTimer(2,30,NULL);
-----------------------------------------------------------

In SetTimer Function, first parameter means channel of timer, second parameter means how frequent call Ontimer function.



Go to Message Handler and make OnTimer()



  ------------------------------------------------------------------------
//in OnTimer()

        if(nIDEvent == 1)
        {
              
               m_FrameVideo = cvQueryFrame( captureAVI );
               if(!m_FrameVideo) KillTimer(1);
               else DisplayIplImg(m_FrameVideo, pDC2, rect2);
        }
        if(nIDEvent ==2)
        {
               m_FrameWebcam = cvQueryFrame( captureCAM );
               DisplayIplImg(m_FrameWebcam, pDC3, rect3);
        }


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




Finally, go to close button function and release memory.
--------------------------------------------------------
cvReleaseCapture(&captureAVI);
cvReleaseCapture(&captureCAM);

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



Result












2012년 8월 10일 금요일

Easy way to set OpenCV configuration

Every time when you start VC2008 for using OpenCV library, you might feel tired due to routine pre-configuration. some tips are here.

You don't need to set include folder and library folder in 'debug' mode and 'release' mode sperately.





















Like above picture, select "All configuration"  and set each folder.
Furthermore, For dll files that should be input in 'Additional dependencies',  only three dll file is enough, if you are beginner of OpenCV.
Needed dll file for beginner is: 

opencv_core230.lib  opencv_highgui230.lib  opencv_imgproc230.lib


All set up would be done in 'All configuration', you don't need  repeated work.

Opening a image and .avi file and Webcam Video on VC2008 console window using OpenCV2.3


If you don't know how to set OpenCV configuration on Visual Studio 2008, please read previous post. This post continue to follow Open CV study.
I try to open jpg image and avi videoclip in VC2008 console window, and I try also to connect webcam with console window.

At first, type like below. and build and run!

#include <cv.h>
#include <highgui.h>

int main()
{
        IplImage* img = cvLoadImage( "test.jpg" ); 
//"test.jpg" should be in where you are coding; open a image file
        cvNamedWindow( "window1", CV_WINDOW_AUTOSIZE ); //to create window
        cvShowImage( "window1", img ); //poject image file in window made before
        cvWaitKey(0);//wait until next keyboard input

        cvReleaseImage( &img ); //memory release
        cvDestroyWindow( "window1" ); //close window

        return 0;
}



Result

you did one of the three missions!


Second, type this below, and build and run!

#include <cv.h>
#include <highgui.h>

int main()
{
        cvNamedWindow( "yadong", 0 ); //open a new window, size is not determined
        cvResizeWindow( "yadong", 200,200); //determine window size
        cvMoveWindow( "yadong", 100,100); //place window,
        CvCapture* capture = cvCreateFileCapture( "test.avi" ); //open avi file and ready to capture.
        IplImage* frame; //this variable is for captured image.
        char key;

        while(1)
        {
               frame = cvQueryFrame( capture );//captured image from video is saved in this pointer for instance.
               if( !frame ) break;
               cvShowImage( "yadong", frame ); //project image into window

               key = cvWaitKey(20); //wait 20ms for check keyboard input
               if( key == 27 ) break; //27 means 'ESC'
        }

        cvReleaseCapture( &capture );
        cvDestroyWindow( "yadong" );
       
        return 0;
}


Result

Second mission completed!


Finally, Let's connect and project webcam video on the console window.


#include <cv.h>
#include <highgui.h>

int main()
{
        IplImage *frame;
        CvCapture *pCamera;
        int key;

        pCamera = cvCaptureFromCAM(0);
       
        cvNamedWindow( "molka", CV_WINDOW_AUTOSIZE);

        while(1)
        {
               frame = cvQueryFrame( pCamera );
               cvShowImage( "molka", frame);
              
               key = cvWaitKey(30);
               if( (char)key==27 ) break;
        }
        cvReleaseCapture(&pCamera);
        cvDestroyWindow("molka");

        return 0;

}


Result




2012년 8월 6일 월요일

Installing OpenCV 2.3 for Visual Studio 2008(win7)

At first, go to below web site and download OpenCV-2.3.0-win-superpack.exe

http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3/





Double click on the downloaded file to unzip OpenCV libraries.
Move the unzipped folder "OpenCV2.3" to "C:\".
In my case, I located "OpenCV2.3" folder on "C:\OpenCV2.3".
(Actually, folder location is not important, but you must remember path of folder.)

And then, Open the Visual Studio 2008 and make new empty project like below.
Choose Win32 Console Application as type. Enter its name and select the path where to create it. Then in the upcoming dialog make sure you create an empty project.






Add New Item in Source files directory. Select C++ file(.cpp) as template.


 Now, let's configure project's properties, go to Projects -> Properties...
There are two configuration mode, a 'Debug' and a 'Release', to configure for OpenCV2.3.
Firstly, select 'Debug' on configuration tab.


1. Configuration Properties->C/C++ ->General directory ->Additional Include Directories

Add additional include directories as follows:

C:\OpenCV2.3\build\include
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2





2. Configuration Properties -> Linker -> General -> Additional Library Directories

Add additional library directories as follows:

C:\OpenCV2.3\build\x86\vc9\lib


3. Configuration Properties -> Linker -> Input -> Additional Dependencies edit

Add libraries as follows:

opencv_calib3d230d.lib
opencv_contrib230d.lib
opencv_core230d.lib
opencv_features2d230d.lib
opencv_flann230d.lib
opencv_gpu230d.lib
opencv_haartraining_engined.lib
opencv_highgui230d.lib
opencv_imgproc230d.lib
opencv_legacy230d.lib
opencv_ml230d.lib
opencv_objdetect230d.lib
opencv_video230d.lib

4. For 'Release' mode

Do like 'Debug' mode, but when you edit Additional  Dependencies, make sure you type libraries' names except 'd'( ex) xxxx_xxx230.lib)


opencv_calib3d230.lib
opencv_contrib230.lib
opencv_core230.lib
opencv_features2d230.lib
opencv_flann230.lib
opencv_gpu230.lib
opencv_haartraining_engine.lib
opencv_highgui230.lib
opencv_imgproc230.lib
opencv_legacy230.lib
opencv_ml230.lib
opencv_objdetect230.lib
opencv_video230.lib




5. Copy dll files onto System32 directory.
Go and copy files in 'C:\OpenCV2.3\build\x86\vc9\bin' and paste into 'C:\Windows\System32'



you did it!

Let's check it works well or not.



#include <cv.h>
#include <highgui.h>

using namespace cv;

int main(void)
{
Mat img;
img = imread("test.jpg");
imshow("test",img);
waitKey(0);
}