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

简介: <p><br></p> <p>预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的“预处理”过程,其主要功能包括‘去模糊’、‘获得灰度图’、'应用Canny算子‘等可选模块。</p> <p><br></p> <p>下面给出源码:</p> <p><br></p> <p></p><pre name="code" class="cpp


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


下面给出源码:


#include "PreProcessor.h"

namespace bgslibrary
{
  PreProcessor::PreProcessor() : firstTime(true), equalizeHist(false), gaussianBlur(false)
  {
    std::cout << "PreProcessor()" << std::endl;
  }

  PreProcessor::~PreProcessor()
  {
    std::cout << "~PreProcessor()" << std::endl;
  }

  void PreProcessor::setEqualizeHist(bool value)
  {
    equalizeHist = value;
  }

  void PreProcessor::setGaussianBlur(bool value)
  {
    gaussianBlur = value;
  }

  cv::Mat PreProcessor::getGrayScale()
  {
    return img_gray.clone();
  }

  void PreProcessor::process(const cv::Mat &img_input, cv::Mat &img_output)
  {
    if (img_input.empty())
      return;

    loadConfig();

    if (firstTime)
      saveConfig();

    img_input.copyTo(img_output);

    // Converts image from one color space to another
    // http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html#cv-cvtcolor
    cv::cvtColor(img_input, img_gray, CV_BGR2GRAY);
    //img_gray.copyTo(img_output);

    // Equalizes the histogram of a grayscale image
    // http://opencv.willowgarage.com/documentation/cpp/histograms.html#cv-equalizehist
    if (equalizeHist)
      cv::equalizeHist(img_output, img_output);

    // Smoothes image using a Gaussian filter
    // http://opencv.willowgarage.com/documentation/cpp/imgproc_image_filtering.html#GaussianBlur
    if (gaussianBlur)
      cv::GaussianBlur(img_output, img_output, cv::Size(7, 7), 1.5);

    if (enableShow)
      cv::imshow("Pre Processor", img_output);

    firstTime = false;
  }

  void PreProcessor::rotate(const cv::Mat &img_input, cv::Mat &img_output, float angle)
  {
    IplImage* image = new IplImage(img_input);

    //IplImage *rotatedImage = cvCreateImage(cvSize(480,320), IPL_DEPTH_8U, image->nChannels);
    //IplImage *rotatedImage = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, image->nChannels);
    IplImage* rotatedImage = cvCreateImage(cvSize(image->height, image->width), IPL_DEPTH_8U, image->nChannels);

    CvPoint2D32f center;
    //center.x = 160;
    //center.y = 160;
    center.x = (image->height / 2);
    center.y = (image->width / 2);

    CvMat* mapMatrix = cvCreateMat(2, 3, CV_32FC1);

    cv2DRotationMatrix(center, angle, 1.0, mapMatrix);
    cvWarpAffine(image, rotatedImage, mapMatrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(0));

    cv::Mat img_rot(rotatedImage);
    img_rot.copyTo(img_output);

    cvReleaseImage(&image);
    cvReleaseImage(&rotatedImage);
    cvReleaseMat(&mapMatrix);
  }

  void PreProcessor::applyCanny(const cv::Mat &img_input, cv::Mat &img_output)
  {
    if (img_input.empty())
      return;

    //------------------------------------------------------------------
    // Canny
    // Finds edges in an image using Canny algorithm.
    // http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html#cv-canny
    //------------------------------------------------------------------

    cv::Mat img_canny;
    cv::Canny(
      img_input, // image ?Single-channel 8-bit input image
      img_canny,  // edges ?The output edge map. It will have the same size and the same type as image
      100,       // threshold1 ?The first threshold for the hysteresis procedure
      200);      // threshold2 ?The second threshold for the hysteresis procedure
    cv::threshold(img_canny, img_canny, 128, 255, cv::THRESH_BINARY_INV);

    img_canny.copyTo(img_output);
  }

  void PreProcessor::saveConfig()
  {
    CvFileStorage* fs = cvOpenFileStorage("./config/PreProcessor.xml", 0, CV_STORAGE_WRITE);

    cvWriteInt(fs, "equalizeHist", equalizeHist);
    cvWriteInt(fs, "gaussianBlur", gaussianBlur);
    cvWriteInt(fs, "enableShow", enableShow);

    cvReleaseFileStorage(&fs);
  }

  void PreProcessor::loadConfig()
  {
    CvFileStorage* fs = cvOpenFileStorage("./config/PreProcessor.xml", 0, CV_STORAGE_READ);

    equalizeHist = cvReadIntByName(fs, 0, "equalizeHist", false);
    gaussianBlur = cvReadIntByName(fs, 0, "gaussianBlur", false);
    enableShow = cvReadIntByName(fs, 0, "enableShow", true);

    cvReleaseFileStorage(&fs);
  }
}

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





目录
相关文章
|
2月前
|
编译器 程序员 Linux
C++系列九:预处理功能
C++系列九:预处理功能
|
5月前
|
计算机视觉 Python
OpenCV膨胀操作讲解及将图像加工成近视眼效果实战(附Python源码)
OpenCV膨胀操作讲解及将图像加工成近视眼效果实战(附Python源码)
32 0
|
6月前
|
安全 编译器 C语言
详解预处理(1)
详解预处理(1)
54 1
|
6月前
|
编译器 Linux C语言
详解预处理(2)
详解预处理(2)
42 0
|
7月前
|
机器学习/深度学习 算法 PyTorch
OpenCV-图像着色(采用DNN模块导入深度学习模型)
OpenCV-图像着色(采用DNN模块导入深度学习模型)
|
9月前
预处理的学习
预处理的学习
33 0
|
9月前
|
编译器 程序员 C语言
预处理详解
预处理详解
|
11月前
|
前端开发 JavaScript
Less预处理——混合方法
Less预处理——混合方法
|
11月前
|
前端开发
Less预处理——初识Less
Less预处理——初识Less
|
机器学习/深度学习 自然语言处理 算法
5个Python库可以帮你轻松的进行自然语言预处理
5个Python库可以帮你轻松的进行自然语言预处理
167 0