|
Here I will add code that will load a bitmap file into an IplImage, as a step before using the OpenCV library to do some image processing.
Now right click on "MyApplication classes" in the CVB and then on "New Class" to create a class to do some image processing with the library, make it a "Generic Class" and name it "MyIplClass"
Click on the thumbnail to view the New class Dialog Box.
This creates and adds two new files to the project work space, "MyIplClass.ccp" and "MyIplClass.h" can be seen in the FVB, with constructor MyIplClass() destructor ~MyIplClass() in the CVB.
Double click on "MyApplication.h" and add the line:
- - - Start cut'n paste - - -
#include "MyIplClass.h"
- - - End cut'n paste - - -
Below the "include resource.h" and move the other include directives to "MyIplClass.h" (double click on FVB to get there) just before the class declaration also do a include of ipl.h:
- - - Start cut'n paste - - -
#include "cv.h"
#include "HighGUI.h"
#define IMAGE_WIDTH 128
#define IMAGE_HEIGHT 128
- - - End cut'n paste - - -
Right click on "MyIplClass" in the CVB and add a public member variable "IplImage*" called "m_Ipl". Double click on the constructor "MyIplClas()" to get to the code and add the following lines:
- - - Start cut'n paste - - -
     // Create the IPL image
     m_Ipl = cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
     // Use this line if the bitmap is 24 bit
     //m_Ipl = cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), IPL_DEPTH_8U, 3 );
- - - End cut'n paste - - -
And double click on the destructor ~MyIplClass() in the CVB and add the code:
- - - Start cut'n paste - - -
     cvReleaseImage(&m_Ipl);
- - - End cut'n paste - - -
Right click on "MyIplClass" in the CVB and add a member function called GetIplData(IplImage* ipl) and of type void. Double click on the new function and add the line:
- - - Start cut'n paste - - -
     memcpy(ipl->imageData, m_Ipl->imageData, m_Ipl->imageSize);
- - - End cut'n paste - - -
Right click on "MyIplClass" in the CVB and add a member function called LoadBMP(CString FileName) of type void with the lines:
- - - Start cut'n paste - - -
     // Do it the easy way with HighGUI BMP, DIB,JPEG, JPG, JPE,PNG,PBM, PGM, PPM, SR, RAS, TIFF, TIF
     // m_Ipl = cvLoadImage( FileName, 0);
     // Or the hard way:
     BOOL ok = TRUE;
     CFile SrcFile;
     char *ReadBuffer; // Pointer to read buffer
     char *OutputBuffer; // Pointer to output buffer
     BITMAPFILEHEADER BmpFileHeader;
     unsigned int BmpHeadSize;
     char *BmpHeadPtr;
     BITMAPINFOHEADER *BmpInfoPtr;
     int i;
     if ( !SrcFile.Open( FileName, ( CFile::modeRead | CFile::typeBinary), NULL ) )
     {
          AfxMessageBox("Could not open file");
     }
     // Read the BMP File header
     ok = ok && ( SrcFile.Read( &BmpFileHeader, sizeof( BmpFileHeader )) == sizeof( BmpFileHeader ));
     // Test settings in BMP file header.
     ok = ok && ( BmpFileHeader.bfType == 'MB' ); /* Bitmap file */
     // Find the size of the rest of the BMP header
     BmpHeadSize = BmpFileHeader.bfOffBits - sizeof( BmpFileHeader );
     // Allocate space for the rest of the BMP header
     BmpHeadPtr = new char[ BmpHeadSize ];
     ok = ok && ( BmpHeadPtr != NULL );
     // Read the rest of the BMP header - Info part and color table part
     ok = ok && ( SrcFile.Read( BmpHeadPtr, BmpHeadSize ) == BmpHeadSize );
     // Make a cast to the type we think is the Info part of the header
     BmpInfoPtr = ( BITMAPINFOHEADER* ) BmpHeadPtr;
     // Test settings in BMP info header.
     ok = ok && ( BmpInfoPtr->biSize == sizeof( BITMAPINFOHEADER ) );
     ok = ok && ( BmpInfoPtr->biWidth == IMAGE_WIDTH );
     ok = ok && ( BmpInfoPtr->biHeight == IMAGE_HEIGHT );
     ok = ok && ( BmpInfoPtr->biPlanes == 1 );
     ok = ok && ( BmpInfoPtr->biBitCount == 8 );
     // Use this line if the bitmap is 24 bit
     // ok = ok && ( BmpInfoPtr->biBitCount == 24 );
     ok = ok && ( BmpInfoPtr->biCompression == BI_RGB );
     ok = ok && ( BmpInfoPtr->biSizeImage == IMAGE_HEIGHT * IMAGE_WIDTH );
     // Use this line if the bitmap is 24 bit
     //ok = ok && ( BmpInfoPtr->biSizeImage == IMAGE_HEIGHT * IMAGE_WIDTH * 3 );
     if (ok)
     {
         // Allocate a buffer for destination data
         ReadBuffer = new char[ BmpInfoPtr->biSizeImage ];
         // Read the BMP data from the source file
         SrcFile.Read( ReadBuffer, BmpInfoPtr->biSizeImage );
         // Allocate a buffer for upside down corrected data
         OutputBuffer = new char[ BmpInfoPtr->biSizeImage ];
         for(i = 0; i < IMAGE_HEIGHT; i++)
         {
             memcpy(&OutputBuffer[(IMAGE_HEIGHT - i - 1) * IMAGE_WIDTH], &ReadBuffer[i * IMAGE_WIDTH], IMAGE_WIDTH);
             // Use this line if the bitmap is 24 bit
             // memcpy(&OutputBuffer[(IMAGE_HEIGHT - i - 1) * IMAGE_WIDTH*3], &ReadBuffer[i * IMAGE_WIDTH*3], IMAGE_WIDTH*3);
         }
         // Now copy the read data to the IPL structure
         memcpy(m_Ipl->imageData, OutputBuffer, BmpInfoPtr->biSizeImage );
         delete OutputBuffer;
         delete ReadBuffer;
     }
     else
     {
         AfxMessageBox("Wrong image size");
     }
     delete BmpHeadPtr;
     SrcFile.Close();
- - - End cut'n paste - - -
Now click on the RVB (Resource View Browser), then on "MyApplication resources" then double click on "ID_MYAPPLICATION_DIALOG" this brings you to the Dialog Editor Window.
Drag a button from the tool line to a corner of your dialog, right click on the button and then click on the "Properties"to be able to edit the properties for this control. In the "ID:" field write "IDC_PROCESS" and in the "Caption" field write "Process" close the properties window.
Double click on the created button to make a function named "OnProcess" and get to the code part of this function. Add the lines:
- - - Start cut'n paste - - -
     MyIplClass *Img= new MyIplClass;
     CString filename;
     // This file should be part of the OpenCV kit.
     filename = "C:\\Program Files\\Intel\\opencv\\tests\\cv\\testdata\\snakes\\ring.bmp";
     Img->LoadBMP(filename);
     Img->GetIplData( TheImage );
     delete Img;
     RedrawWindow(NULL, NULL, RDW_INVALIDATE ); //Force update of dialog;
- - - End cut'n paste - - -
Thats it hit F5 to build and run, when the "Process" button is pressed the ring appears.
Press on the link to use the openCV on the bitmap file.
^ back to top ^
Last updated
May 3 - 2004: My counter from freewebcounter.com created popups, so a new counter is used.
June 18 2003 : Use of HighGUI in bmp load added.
May 5 2003 : Added a delete in the function OnProcess() to avoid memory leaks.
|