背景建模技术(七):预处理(PreProcessor)模块

时间:2023-03-09 15:41:35
背景建模技术(七):预处理(PreProcessor)模块

预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的“预处理”过程,其主要功能包括‘去模糊’、‘获得灰度图’、'应用Canny算子‘等可选模块。

下面给出源码:

  1. #include "PreProcessor.h"
  2. namespace bgslibrary
  3. {
  4. PreProcessor::PreProcessor() : firstTime(true), equalizeHist(false), gaussianBlur(false)
  5. {
  6. std::cout << "PreProcessor()" << std::endl;
  7. }
  8. PreProcessor::~PreProcessor()
  9. {
  10. std::cout << "~PreProcessor()" << std::endl;
  11. }
  12. void PreProcessor::setEqualizeHist(bool value)
  13. {
  14. equalizeHist = value;
  15. }
  16. void PreProcessor::setGaussianBlur(bool value)
  17. {
  18. gaussianBlur = value;
  19. }
  20. cv::Mat PreProcessor::getGrayScale()
  21. {
  22. return img_gray.clone();
  23. }
  24. void PreProcessor::process(const cv::Mat &img_input, cv::Mat &img_output)
  25. {
  26. if (img_input.empty())
  27. return;
  28. loadConfig();
  29. if (firstTime)
  30. saveConfig();
  31. img_input.copyTo(img_output);
  32. // Converts image from one color space to another
  33. // http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html#cv-cvtcolor
  34. cv::cvtColor(img_input, img_gray, CV_BGR2GRAY);
  35. //img_gray.copyTo(img_output);
  36. // Equalizes the histogram of a grayscale image
  37. // http://opencv.willowgarage.com/documentation/cpp/histograms.html#cv-equalizehist
  38. if (equalizeHist)
  39. cv::equalizeHist(img_output, img_output);
  40. // Smoothes image using a Gaussian filter
  41. // http://opencv.willowgarage.com/documentation/cpp/imgproc_image_filtering.html#GaussianBlur
  42. if (gaussianBlur)
  43. cv::GaussianBlur(img_output, img_output, cv::Size(7, 7), 1.5);
  44. if (enableShow)
  45. cv::imshow("Pre Processor", img_output);
  46. firstTime = false;
  47. }
  48. void PreProcessor::rotate(const cv::Mat &img_input, cv::Mat &img_output, float angle)
  49. {
  50. IplImage* image = new IplImage(img_input);
  51. //IplImage *rotatedImage = cvCreateImage(cvSize(480,320), IPL_DEPTH_8U, image->nChannels);
  52. //IplImage *rotatedImage = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, image->nChannels);
  53. IplImage* rotatedImage = cvCreateImage(cvSize(image->height, image->width), IPL_DEPTH_8U, image->nChannels);
  54. CvPoint2D32f center;
  55. //center.x = 160;
  56. //center.y = 160;
  57. center.x = (image->height / 2);
  58. center.y = (image->width / 2);
  59. CvMat* mapMatrix = cvCreateMat(2, 3, CV_32FC1);
  60. cv2DRotationMatrix(center, angle, 1.0, mapMatrix);
  61. cvWarpAffine(image, rotatedImage, mapMatrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
  62. cv::Mat img_rot(rotatedImage);
  63. img_rot.copyTo(img_output);
  64. cvReleaseImage(&image);
  65. cvReleaseImage(&rotatedImage);
  66. cvReleaseMat(&mapMatrix);
  67. }
  68. void PreProcessor::applyCanny(const cv::Mat &img_input, cv::Mat &img_output)
  69. {
  70. if (img_input.empty())
  71. return;
  72. //------------------------------------------------------------------
  73. // Canny
  74. // Finds edges in an image using Canny algorithm.
  75. // http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html#cv-canny
  76. //------------------------------------------------------------------
  77. cv::Mat img_canny;
  78. cv::Canny(
  79. img_input, // image ?Single-channel 8-bit input image
  80. img_canny,  // edges ?The output edge map. It will have the same size and the same type as image
  81. 100,       // threshold1 ?The first threshold for the hysteresis procedure
  82. 200);      // threshold2 ?The second threshold for the hysteresis procedure
  83. cv::threshold(img_canny, img_canny, 128, 255, cv::THRESH_BINARY_INV);
  84. img_canny.copyTo(img_output);
  85. }
  86. void PreProcessor::saveConfig()
  87. {
  88. CvFileStorage* fs = cvOpenFileStorage("./config/PreProcessor.xml", 0, CV_STORAGE_WRITE);
  89. cvWriteInt(fs, "equalizeHist", equalizeHist);
  90. cvWriteInt(fs, "gaussianBlur", gaussianBlur);
  91. cvWriteInt(fs, "enableShow", enableShow);
  92. cvReleaseFileStorage(&fs);
  93. }
  94. void PreProcessor::loadConfig()
  95. {
  96. CvFileStorage* fs = cvOpenFileStorage("./config/PreProcessor.xml", 0, CV_STORAGE_READ);
  97. equalizeHist = cvReadIntByName(fs, 0, "equalizeHist", false);
  98. gaussianBlur = cvReadIntByName(fs, 0, "gaussianBlur", false);
  99. enableShow = cvReadIntByName(fs, 0, "enableShow", true);
  100. cvReleaseFileStorage(&fs);
  101. }
  102. }

最后给出此模块的流程框架图供大家参考:

背景建模技术(七):预处理(PreProcessor)模块