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

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

下面给出源码:

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


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

原文地址:https://www.cnblogs.com/ywsoftware/p/4511787.html