Code examples

High Level Code generation in C, C++, Visual C++
1 the simplest webcam program with MFC C/C++


Create Your own MFC Dialog Project
Insert a few lines in the OnInitDialog

or download the source code

to read an image from the webcam initialize a window handle with capCreateCaptureWindow
and connect this handle with the installed webcam driver.
The first image is now in your window

BOOL CCam01Dlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 SetIcon(m_hIcon, TRUE);
 SetIcon(m_hIcon, FALSE);

 // TODO:

 hMyWindow = capCreateCaptureWindow("handle", WS_CHILD | WS_VISIBLE,
 0, 0, ImageX, ImageY,
 this->m_hWnd, 1);

 // connect the driver with the webcam
    bool xx = capDriverConnect(hMyWindow,0);
    if(xx == false)
        {
                AfxMessageBox("Webcam not found ",0,0);
                return false;
        }
        return TRUE;
}

 2 use the timer to grab the frame from webcam

BOOL Ccam02Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
// TODO:

hMyWindow = capCreateCaptureWindow("handle", WS_CHILD | WS_VISIBLE,
0, 0, ImageX, ImageY, this->m_hWnd, 1);

// connect the driver with the webcam
bool xx = capDriverConnect(hMyWindow,0);
        if(xx == false)
        {
        AfxMessageBox("Webcam not found ",0,0);
        return false;
        }
   //----------- Set the Timer to grab new images -----------
        SetTimer(1,40,0);

        return TRUE;
}


void Ccam02Dlg::OnTimer(UINT_PTR nIDEvent)
{
    if(nIDEvent == 1)
        {
    capGrabFrame(hMyWindow);
        }
        CDialog::OnTimer(nIDEvent);
}
 3 create another dialog window to view the image

CWin2Dlg  dlgWin2;   // like a pointer to CWin2Dlg class

  //==== create and shows the second dialog window =========
        dlgWin2.Create(IDD_WIN2,this);
        dlgWin2.ShowWindow(SW_SHOW);


void Ccam03Dlg::OnTimer(UINT_PTR nIDEvent)
{
    if(nIDEvent == 1)
        {
    capGrabFrame(hMyWindow);

    if(iBitmapFlag==0)  // create a Bitmap only once
    {
    capture_map.CreateCompatibleBitmap( dc , ImageX, ImageY );
    iBitmapFlag=1;
    }

    capture_dc.CreateCompatibleDC( dc );
    capture_dc.SelectObject( &capture_map );
    capture_dc.BitBlt( 0, 0, ImageX, ImageY, dc, 0, 0, SRCCOPY );  //copy image to bitmap

    //======= copy the image from bitmap to memory (arrImage1)===============
    LPBYTE lpBits = arrImage1;   // pointer
    int xx,err;
    xx = GetDIBits(capture_dc, capture_map, 0, ImageY, lpBits, &bmpinfo, DIB_RGB_COLORS);
    if(xx == 0)   err = GetLastError(); // only for debug

  //*********** copy data from arrImage1 to arrImage2 *********************
        int ii=0;
        int iimax = sizeof(arrImage1);
    while( ii < iimax)
        {
                arrImage2[ii] = arrImage1[ii];
         ii++;
        }
        dlgWin2.InvalidateRect(RectWin2,false); // repaint WIN2  with arrImage2

        }
        CDialog::OnTimer(nIDEvent);
}
 4 mirror the image in the second dialog window

//*********** copy data from arrImage1 to arrImage2 *********************
        int iLine,iCol,index1,index2;
        int ii=0;
        int iimax = sizeof(arrImage1);

        if(iFlagLeftRight == 0)
        {
                while( ii < iimax)
                {
                arrImage2[ii] = arrImage1[ii];
        ii++;
                }
        }
        if(iFlagLeftRight == 1)
        {
     iLine=0;
         while(iLine < ImageY)
         {
      iCol=0;
                index1 = iLine * ImageX * 4;
                index2 = index1 + ImageX * 4 - 4;

          while(iCol < ImageX)
          {
                arrImage2[index2]   = arrImage1[index1++];
                arrImage2[index2+1] = arrImage1[index1++];
                arrImage2[index2+2] = arrImage1[index1++];
                arrImage2[index2+3] = arrImage1[index1++];
        index2 -= 4;
                iCol++;
          }
          iLine++;
         }

        }

 5 rotate the image 0 90 180 or 270 degree


To rotate the image we copy the data from arrImage1 to arrImage2
changing lines and colummns in the right manner.

 0 degree: no rotation, lines   starts from zero to linemax
                         columms starts from zero to columnmax

 90 degree: columms starts from columnmax to zero
            lines   starts from zero to linemax

180 degree: lines   starts from linemax to zero
            columms starts from columnmax to zero

270 degree: columms starts from zero to columnmax
            lines   starts from linemax to zero

 6 toggle from color to grayscale


every pixel of image is composed with four bytes
first byte blue, second green, third red, fourth byte is not used
For the gray value we take 11% blue, 59% green and 30% red

      grayblue  = arrImage1[index1];     // add 11%
      grayblue *= 28;
      graygreen = arrImage1[index1+1];   // add 59%
      graygreen *= 151;
      grayred   = arrImage1[index1+2];   // add 30%
      grayred  *= 77;
      grayall = grayblue + graygreen + grayred;
      grayall /= 256;

!! multiply with 28 and to divide with 256 ist the same
   as multiply with 11 and to divide with 100
 7 motion detection: creat a reference image


To detect motions in an image we need a reference image.
The original or new image subtracted from the reference image is the motion value.
This operation is made with every pixel inside a defined rectangle and the sum of
all pixels gives us the motion value.
The difference is always absolute.



The reference image is a low pass filtered image.
The value of every pixel is calculated as follow:

PixelFilteredNew = PixelFilteredOld - PixelFilteredOld/8 + PixelNewValue/8;

The value 8 is the filter level.

8  motion detection: draw rectangles and calculate the motion inside


inside a given rectangle we made the difference (absolute mode)

of every
pixel from the reference image and the new image.
9  motion detection: draw rectangles and calculate the motion inside

inside a given rectangle we made the difference (absolute mode)
of every
pixel from the reference image and the new image.

A blue rectangle is illuminated if the motion value rises a given level.
10  motion detection with color pixels
The motion can be detected with different color pixels.