预处理(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)模块的更多相关文章

  1. 背景建模技术(三):背景减法库(BGS Library)的基本框架与入口函数main()的功能

    背景减法库(BGS Library = background subtraction library)包含了37种背景建模算法,也是目前国际上关于背景建模技术研究最全也最权威的资料.本文将更加详细的介 ...

  2. 背景建模技术(二):BgsLibrary的框架、背景建模的37种算法性能分析、背景建模技术的挑战

    背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.ne ...

  3. 背景建模技术(六):帧处理(FrameProcessor)模块

    前面几篇文章简单介绍了BgsLibrary的入口函数.视频分析和视频捕获模块,本文将简单介绍帧处理模块,即对每一帧进行处理的函数,也就是真正调用背景建模算法的接口处. 下面贴出源码供大家分析: #in ...

  4. 背景建模技术(五):视频捕获(VideoCapture)模块

    本次对“视频捕获(VideoCapture)模块”做出分析,给出源代码和对应的程序流程框架. 视频捕获模块的主要功能是设置视频或相机参数,并读取设置配置参数,最后进入帧处理模块的process进程,该 ...

  5. 背景建模技术(四):视频分析(VideoAnalysis)模块

    视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis ...

  6. SPSS Modeler数据挖掘项目实战(数据挖掘、建模技术)

    SPSS Modeler是业界极为著名的数据挖掘软件,其前身为SPSS Clementine.SPSS Modeler内置丰富的数据挖掘模型,以其强大的挖掘功能和友好的操作习惯,深受用户的喜爱和好评, ...

  7. 浅析软件工程中的UML建模技术

    一.基本信息 标题:浅析软件工程中的UML建模技术 时间:2018 出版源:电子世界 领域分类:软件工程:UML建模技术:需求分析 二.研究背景 问题定义:软件工程中UML建模技术的研究 难点:明确软 ...

  8. NoSQL 数据建模技术(转)

    本文转载自:http://coolshell.cn/articles/7270.html ================================================ 全文译自墙外 ...

  9. NoSQL数据建模技术

    原文来自“NoSQL Data Modeling Techniques”,由酷壳网陈皓编译<NoSQL数据建模技术>.这篇文章看完之后,你可能会对NoSQL的数据结构会有些感觉.我的感觉是 ...

随机推荐

  1. TPO-12 C2 A problem of the TA's payroll

    TPO-12 C2 A problem of the TA's payroll payroll n. 工资单:在册职工人数:工资名单: paycheck n. 付薪水的支票,薪水 paperwork ...

  2. 第二章 IP协议详解

    第二章 IP协议详解 2.1 IP服务的特点 它为上层协议提供了无状态,无连接,不可靠的服务 名称 简介 优点 缺点 对付缺点的方法 无状态 IP通信双方不同步传输数据的状态信息 无须为保持通信的状态 ...

  3. 现实世界中的 Python

    Python 有多稳定? 非常稳定. 自 1991 年起大约每隔 6 到 18 个月就会推出新的稳定发布版,这种状态看来还将持续下去. 目前主要发布版本的间隔通常为 18 个月左右. 开发者也会推出旧 ...

  4. Java注解的基本原理

    注解的本质就是一个继承了Annotation接口的接口,一个注解准确意义上来说,只不过是一种特殊注释而已,如果没有解析他的代码,他可能连注释都不如. 解析一个类或者方法的注解往往有两种形式,一种是编译 ...

  5. 在Arch上安装VSCode的方法

    首先去特硬去下载vscode的安装包 mkdir /tmp/vscode cd /tmp/vscode/ wget https://az764295.vo.msecnd.net/public/0.3. ...

  6. 【Linux 运维】 Centos7.x 系统修复模式

    一.linux的运行级别: 运行级别就是来确定系统启动时到底启动那个服务. linux默认有7个运行级别: 0 关机 1 单用户模式,用于系统修复 2 不完全的命令模式,不含NFS服务 3 完全的命令 ...

  7. Python入门(4)

    一.while循环 有时候,你可能需要计算机来帮重复做一件事,这时就需要循环. while condition: statements (else: statements ) 当condition条件 ...

  8. leetcode个人题解——#20 Valid Parentheses

    class Solution { public: bool isValid(string s) { stack<char> brackts; ; i < s.size(); i++) ...

  9. Python3 Tkinter-Menu

    1.创建 from tkinter import * root=Tk() menubar=Menu(root) def hello(): print('Hello Menu!') for item i ...

  10. Java中I/O流之Object流

    Java 中的 object 流:直接将 Object 对象写入或读出 1. serializable 接口:序列化,可以被序列化的,若确实需要将某个类的对象写在硬盘上或网络上,想把他们序列化成一个字 ...