VideoProcessor.cpp
Upload User: huiyuanjf
Upload Date: 2022-05-15
Package Size: 132k
Code Size: 2k
Category:

OpenCV

Development Platform:

Visual C++

  1. #include "StdAfx.h"
  2. #include "VideoProcessor.h"
  3. using namespace cv;
  4. UINT ProcessFunction(LPVOID lpParam)
  5. {
  6. VideoProcessor *proc = (VideoProcessor*)lpParam;
  7. while (true)
  8. {
  9. proc->Capture();
  10. proc->Process();
  11. //proc->Display();
  12. if (WaitForSingleObject(proc->eventStop, 0) == WAIT_OBJECT_0)
  13. {
  14. break;
  15. }
  16. }
  17. return 0;
  18. }
  19. VideoProcessor::VideoProcessor()
  20. {
  21. isRunning = false;
  22. }
  23. VideoProcessor::~VideoProcessor()
  24. {
  25. Stop();
  26. }
  27. void VideoProcessor::Start(int deviceIndex)
  28. {
  29. if (isRunning == true)
  30. {
  31. return;
  32. }
  33. cap = new VideoCapture(deviceIndex);
  34. if (cap->isOpened() == false)
  35. {
  36. return;
  37. }
  38. eventStop = CreateEvent(NULL, FALSE, NULL,NULL);
  39. procThread = AfxBeginThread(ProcessFunction, this);
  40. isRunning = true;
  41. return;
  42. }
  43. void VideoProcessor::Stop()
  44. {
  45. if (isRunning == false)
  46. {
  47. return;
  48. }
  49. SetEvent(eventStop);
  50. WaitForSingleObject(procThread->m_hThread, INFINITE);
  51. delete cap;
  52. CloseHandle(eventStop);
  53. isRunning = false;
  54. }
  55. bool VideoProcessor::IsRunning()
  56. {
  57. return isRunning;
  58. }
  59. void VideoProcessor::Capture()
  60. {
  61. if (isRunning == false)
  62. {
  63. return;
  64. }
  65. Mat imgOrg;
  66. (*cap) >> imgOrg;
  67. resize(imgOrg, imgSrc, Size(480, 360));
  68. return;
  69. }
  70. void VideoProcessor::Process()
  71. {
  72. if (isRunning == false)
  73. {
  74. return;
  75. }
  76. Mat src = imgSrc;
  77. Mat dst;
  78. dilate(src, dst, Mat());
  79. imgDst = dst;
  80. return;
  81. }
  82. void VideoProcessor::Display()
  83. {
  84. imshow("Image", imgDst);
  85. HWND hWnd= (HWND)cvGetWindowHandle("Image");
  86. SendMessage(hWnd,WM_PAINT,NULL,NULL);
  87. }