forked from stupel/Preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinarization.cpp
executable file
·81 lines (67 loc) · 2.77 KB
/
binarization.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
#include "binarization.h"
Binarization::Binarization(QObject *parent) : QObject(parent)
{
}
void Binarization::setParams(const cv::Mat &imgEnhanced, const BINARIZATION_PARAMS &binarizationParams)
{
this->imgEnhanced = imgEnhanced;
this->binarization = binarizationParams;
}
void Binarization::binarizeGaussianBlur()
{
this->imgBinarized = cv::Mat(this->imgEnhanced.rows, this->imgEnhanced.cols, this->imgEnhanced.type());
cv::GaussianBlur(this->imgEnhanced, this->imgBinarized, cv::Size(3,3), 1);
cv::threshold(this->imgBinarized, this->imgBinarized, 0, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);
if (*this->binarization.useQualityMap || *this->binarization.useMask) this->deleteBackground();
}
void Binarization::binarizeAdaptive()
{
this->imgBinarized = cv::Mat(this->imgEnhanced.rows, this->imgEnhanced.cols, this->imgEnhanced.type());
cv::adaptiveThreshold(this->imgEnhanced, this->imgBinarized, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 21, 10);
if (*this->binarization.useQualityMap || *this->binarization.useMask) this->deleteBackground();
}
void Binarization::deleteBackground()
{
if (*this->binarization.useMask) {
for (int x = 0; x < this->imgBinarized.cols; x++) {
for (int y = 0; y < this->imgBinarized.rows; y++) {
if (this->binarization.imgMask->at<uchar>(y, x) == 0) this->imgBinarized.at<uchar>(y, x) = 255;
}
}
}
else{
for (int x = 0; x < this->imgBinarized.cols; x++) {
for (int y = 0; y < this->imgBinarized.rows; y++) {
if (this->binarization.imgQualityMap->at<uchar>(y, x) == 0) this->imgBinarized.at<uchar>(y, x) = 255;
}
}
}
}
void Binarization::removeHoles(double holeSize)
{
// invertujem obraz
cv::bitwise_not(this->imgBinarized, this->imgBinarized);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
// najdem uplne vsetky diery
cv::findContours(this->imgBinarized, contours, hierarchy,cv::RETR_CCOMP,cv::CHAIN_APPROX_SIMPLE);
// odstranujem vsetky velke diery, zostanu len male diery - potne pory a nedokonalosti
for (std::vector<std::vector<cv::Point> >::iterator it = contours.begin(); it!=contours.end(); )
{
if (it->size() > holeSize)
it=contours.erase(it);
else
++it;
}
// vyplnim diery bielou farbou
for(int ii=0; ii<contours.size(); ii++)
{
cv::drawContours(this->imgBinarized, contours, ii, cv::Scalar(255), -1, cv::LINE_8);
}
// invertujem obraz
cv::bitwise_not(this->imgBinarized, this->imgBinarized);
}
cv::Mat Binarization::getImgBinarized() const
{
return imgBinarized;
}