본문 바로가기
코스웨어/14년 스마트컨트롤러

2014 10 07 영상처리 보고

by 알 수 없는 사용자 2014. 10. 7.
728x90
반응형

#include <windows.h>

#include "vfw.h"

#pragma comment(lib, "vfw32.lib")


#define BITMAP_MAXSIZE   (1024*768*3+10)


LRESULT CALLBACK  WndProc(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK  FramInfo(HWND, LPVIDEOHDR);


HINSTANCE       g_hInst;

HWND            HWndMain;

LPCTSTR         lpszClass=TEXT("WiseCat");

HWND            vfw;

BITMAPINFO      Bm;




int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance

           ,LPSTR lpszCmdParam,int nCmdShow)

{

  HWND hWnd;

  MSG Message;

  WNDCLASS WndClass;

  g_hInst = hInstance;


  WndClass.cbClsExtra=0;

  WndClass.cbWndExtra=0;

  WndClass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);

  WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);

  WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

  WndClass.hInstance=hInstance;

  WndClass.lpfnWndProc=WndProc;

  WndClass.lpszClassName=lpszClass;

  WndClass.lpszMenuName=NULL;

  WndClass.style=CS_HREDRAW | CS_VREDRAW;

  RegisterClass(&WndClass);


  hWnd=CreateWindow(  lpszClass

            ,lpszClass

            ,WS_OVERLAPPEDWINDOW

            ,CW_USEDEFAULT

            ,CW_USEDEFAULT

            ,CW_USEDEFAULT,CW_USEDEFAULT

            ,NULL

            ,(HMENU)NULL

            ,hInstance

            ,NULL);        

  ShowWindow(hWnd,nCmdShow);


  while(GetMessage(&Message,NULL,0,0))

  {

    TranslateMessage(&Message);

    DispatchMessage(&Message);

  }


  return (int)Message.wParam;

}


HDC hdc;


///////////////////////////////////////////////////

HDC MemDC, Hdc;

PAINTSTRUCT ps;

HANDLE hFile;

static unsigned char * BMbuf;

static BITMAPFILEHEADER * stpBFH;

static BITMAPINFOHEADER * stpBIH;

//static unsigned int uiX;

//static unsigned int uiPad;

static unsigned char * ucpPixel;

static DWORD dwRead;

HBITMAP OldBitmap;


///////////////////////////////////////////////////


LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)

{

  PAINTSTRUCT ps;


  switch(iMessage)

  {

    case WM_CREATE:

/////////////////////////////////////BITMAP 적재////////////////////////////////////////////////////

      hFile = CreateFile(TEXT("image.bmp"),

         GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 if(hFile == INVALID_HANDLE_VALUE)

{

MessageBox(hWnd,TEXT("smart.bmp 를 찾을수 없습니다"),TEXT("ㅋㅋ실패ㅋㅋㅋ"),MB_OK);

DestroyWindow(hWnd);

}

 

 BMbuf = (unsigned char *)malloc(BITMAP_MAXSIZE);

 if(BMbuf == 0)

{

MessageBox(hWnd,TEXT("동적할당 생성 실패"),TEXT("ㅋㅋ실패ㅋㅋㅋ"),MB_OK);

DestroyWindow(hWnd);

}

 ReadFile(hFile,BMbuf,BITMAP_MAXSIZE,&dwRead,NULL);

 stpBFH = (BITMAPFILEHEADER *)BMbuf;

 stpBIH = ((BITMAPINFOHEADER *)((char *)BMbuf + sizeof(BITMAPFILEHEADER)) );

 CloseHandle(hFile);

 

// uiPad = uiX%4;

/////////////////////////////////////////////////////////////////////////////////////////////////////////

      HWndMain = hWnd;

      vfw = capCreateCaptureWindow(  TEXT("CAM")

                      ,WS_CHILD | WS_VISIBLE

                      ,0

                      ,0

                      ,400

                      ,300

                      ,hWnd

                      ,NULL);


      capDriverConnect(vfw,0);

      capGetVideoFormat(vfw,&Bm,sizeof(Bm));

      Bm.bmiHeader.biWidth  = 320;

      Bm.bmiHeader.biHeight = 240;

      capSetVideoFormat(vfw,&Bm,sizeof(Bm));

      //capDlgVideoFormat(m_capwnd); 

      capSetCallbackOnFrame(vfw, FramInfo);

      capPreviewRate(vfw, 1);

      capPreview(vfw, FALSE);

      return 0;


    case WM_PAINT:

      hdc = BeginPaint(hWnd,&ps);

      EndPaint(hWnd,&ps);

      return 0;


    case WM_DESTROY:

      PostQuitMessage(0);

      return 0;

  }

  return(DefWindowProc(hWnd,iMessage,wParam,lParam));

}


LRESULT CALLBACK FramInfo(HWND hWnd, LPVIDEOHDR lpData)

{

  static int iCntX;

  static int iCntY;

  static int Jump;

  hdc = GetDC(HWndMain);

  StretchDIBits(hdc  , 0

            , 0

            , Bm.bmiHeader.biWidth

            , Bm.bmiHeader.biHeight

            , 0

            , 0

            , Bm.bmiHeader.biWidth

            , Bm.bmiHeader.biHeight

            , lpData->lpData

            , &Bm

            , DIB_RGB_COLORS

            , SRCCOPY);

  Jump= 0;

  ucpPixel = BMbuf + stpBFH->bfOffBits;


  for(iCntY = 0; iCntY < Bm.bmiHeader.biHeight ; ++iCntY)  

  {        

    for(iCntX = 0; iCntX  < Bm.bmiHeader.biWidth  ; ++iCntX, Jump += 3, ucpPixel = ucpPixel+3)

    {

if(lpData->lpData[Jump + 2] > 100)

{

continue;

}

if(lpData->lpData[Jump + 1] > 100)

{

continue;

}


        if(lpData->lpData[Jump + 0] > 50)

        {

//lpData->lpData[Jump]    = 0;  // Blue

//lpData->lpData[Jump + 2]  = 255;  // Red

//lpData->lpData[Jump + 1]  = 0;  // Green

lpData->lpData[Jump + 2] = *(ucpPixel+2);

lpData->lpData[Jump + 1] = *(ucpPixel+1); 

lpData->lpData[Jump + 0] = *(ucpPixel+0); 

        }

  }

//ucpPixel = ucpPixel + uiPad;

  }


  /*

  for(iCntY = 0; iCntY < Bm.bmiHeader.biHeight ; ++iCntY)  

  {        

    for(iCntX = 0; iCntX  < Bm.bmiHeader.biWidth  ; ++iCntX, Jump += 3 )

    {

      if(lpData->lpData[Jump + 2] > 100)

      {

        continue;

      }

      if(lpData->lpData[Jump + 1] > 100)

      {

        continue;

      }

      if(lpData->lpData[Jump + 0] > 50)

      {

        lpData->lpData[Jump]    = 0;  // Blue

        lpData->lpData[Jump + 2]  = 255;  // Red

        lpData->lpData[Jump + 1]  = 0;  // Green

      }

}

  }*/

  StretchDIBits(hdc  , Bm.bmiHeader.biWidth +20

            , 0

            , Bm.bmiHeader.biWidth

            , Bm.bmiHeader.biHeight

            , 0

            , 0

            , Bm.bmiHeader.biWidth

            , Bm.bmiHeader.biHeight

            , lpData->lpData

            , &Bm

            , DIB_RGB_COLORS

            , SRCCOPY);


  ReleaseDC(HWndMain, hdc);


  return 0;

}

결과 :






728x90