forked from andrewssobral/bgslibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreProcessor.cpp
149 lines (115 loc) · 4.67 KB
/
PreProcessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
This file is part of BGSLibrary.
BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
#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 = cv::cvarrToMat(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);
}
}