-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImgProc.cpp
123 lines (102 loc) · 4.07 KB
/
ImgProc.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
#include "ImgProc.h"
#include "fourier_transform.h"
#include "histogramEqualization.h"
#include "opencv2/imgproc/imgproc.hpp"
void OpenCvImgProc::gaussianFilter(const cv::Mat& src, cv::Mat& dst) const {
cv::GaussianBlur(src, dst, cv::Size(5, 5), 0, 0);
}
void OpenCvImgProc::medianFilter(const cv::Mat& src, cv::Mat& dst) const {
cv::medianBlur(src, dst, 5);
}
void OpenCvImgProc::bilateralFilter(const cv::Mat& src, cv::Mat& dst) const {
cv::bilateralFilter(src, dst, 9, 250, 250);
}
void OpenCvImgProc::lowPassFilter(const cv::Mat& src, cv::Mat& dst) const {
cv::blur(src, dst, cv::Size(5,5));
}
void OpenCvImgProc::lowPassFilterFreq(const cv::Mat& src, cv::Mat& dst) const {
if (src.channels() > 1) {
cv::cvtColor(src, dst, cv::COLOR_BGR2HSV);
cv::Mat HSVChannels[3];
cv::split(dst, HSVChannels);
cv::Mat mask = lowPassMask(getfourierPaddedSize(HSVChannels[2]));
freqFilter(HSVChannels[2], HSVChannels[2], mask);
std::vector<cv::Mat> channels = {HSVChannels[0],HSVChannels[1],HSVChannels[2]};
cv::merge(channels,dst);
cv::cvtColor(dst,dst, cv::COLOR_HSV2BGR);
return;
}
cv::Mat mask = lowPassMask(getfourierPaddedSize(src));
freqFilter(src, dst, mask);
}
void OpenCvImgProc::highPassFilter(const cv::Mat& src, cv::Mat& dst) const {
std::function<void(const cv::Mat&, cv::Mat&)> filter = [=](const cv::Mat& src, cv::Mat& dst) {
cv::Mat kernel{-1, 0, 1, -2, 0, 2, -1, 0, 1};
cv::filter2D(src, dst, -1, kernel);
};
if(src.channels() > 1){
this->rgbFilter(src, dst, filter);
return;
}
filter(src, dst);
}
void OpenCvImgProc::highPassFilterFreq(const cv::Mat& src, cv::Mat& dst) const {
if (src.channels() > 1) {
cv::cvtColor(src, dst, cv::COLOR_BGR2HSV);
cv::Mat HSVChannels[3];
cv::split(dst, HSVChannels);
cv::Mat mask = highPassMask(getfourierPaddedSize(HSVChannels[2]));
freqFilter(HSVChannels[2], HSVChannels[2], mask);
std::vector<cv::Mat> channels = {HSVChannels[0],HSVChannels[1],HSVChannels[2]};
cv::merge(channels,dst);
cv::cvtColor(dst,dst, cv::COLOR_HSV2BGR);
return;
}
cv::Mat mask = highPassMask(getfourierPaddedSize(src));
freqFilter(src, dst, mask);
}
void OpenCvImgProc::freqFilter(const cv::Mat& src, cv::Mat& dst, cv::Mat mask) const {
if (src.channels() > 1){
throw std::invalid_argument("Mat convertToFourier(Mat img) only works with 1 channel matrix ");
}
cv::Mat planes[2];
cv::Mat realPlane, imaginaryPlane;
fourierPlanes(src, planes);
fftShift(planes);
applyFFtFilter(planes, mask);
ifftShift(planes);
cv::merge(planes, 2, dst);
inverseFourier(dst, dst);
dst = prepMatForConverting(dst);
return;
}
void OpenCvImgProc::histEqualize(const cv::Mat& src, cv::Mat& dst) const {
histogramEqualization(src, dst);
}
void OpenCvImgProc::rgbFilter(const cv::Mat& src, cv::Mat& dst, std::function<void(const cv::Mat&, cv::Mat&)> filter) const{
cv::cvtColor(src, dst, cv::COLOR_BGR2HSV);
cv::Mat HSVChannels[3];
cv::split(dst, HSVChannels);
filter(src, dst);
std::vector<cv::Mat> channels = {HSVChannels[0],HSVChannels[1],HSVChannels[2]};
cv::merge(channels,dst);
cv::cvtColor(dst,dst, cv::COLOR_HSV2BGR);
}
cv::Mat OpenCvImgProc::highPassMask(cv::Size maskSize) const {
int cx = maskSize.width/2;
int cy = maskSize.height/2;
int rectSize = lround(cx > cy ? cy * 0.6: cx * 0.6);
int rectTopLeftX = cx-(rectSize/2), rectTopLeftY = cy-(rectSize/2);
cv::Mat mask = cv::Mat::ones(maskSize, CV_32F);
mask(cv::Rect(rectTopLeftX, rectTopLeftY, rectSize, rectSize)) = 0;
return mask;
}
cv::Mat OpenCvImgProc::lowPassMask(cv::Size maskSize) const {
int cx = maskSize.width/2;
int cy = maskSize.height/2;
int rectSize = lround(cx > cy ? cy * 0.35: cx * 0.35);
int rectTopLeftX = cx-(rectSize/2), rectTopLeftY = cy-(rectSize/2);
cv::Mat mask = cv::Mat::zeros(maskSize, CV_32F);
mask(cv::Rect(rectTopLeftX, rectTopLeftY, rectSize, rectSize)) = 1;
return mask;
}