-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterworker.cpp
285 lines (253 loc) · 10.5 KB
/
filterworker.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/***********************************************************************
* FILENAME : filterworker.cpp
*
* LICENSE:
* qcvTouchUp provides an image processing toolset for editing
* photographs, purposed and packaged for use in a desktop application
* user environment. Copyright (C) 2018, Matthew R. Miller
*
* This program 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 (version 3 of the License) and the
* 3-clause BSD License as agreed upon through the use of the Qt toolkit
* and OpenCV libraries in qcvTouchUp development, respectively. Copies
* of the appropriate license files for qcvTouchup, and its source code,
* can be found in LICENSE.Qt.txt and LICENSE.CV.txt.
*
* This program 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 and
* 3-clause BSD License along with this program. If not, please see
* <http://www.gnu.org/licenses/> and <https://opencv.org/license.html>.
*
* If you wish to contact the developer about this project, please do so
* through their account at <https://github.com/mattrussmill>
*
* DESCRIPTION :
* This is the worker thread object tied to the filtermenu.cpp object.
* The worker performs the operations for filtering the image such as
* sharpening, bluring, and edge detect. It collects all necessary slider
* values in a QVarient and copies the object through the signal/signal
* mechanism to provide the necessary parameters for image processing.
*
* NOTES :
* This worker thread does not use the OpenCL enhancements offered implicitly
* through the UMat OpenCV object. The filtering operations had bugs associated
* with them. This may be bugs within the OpenCV Version used (3.3.1) but I think
* these are hardware accelerated implicitly anyway with OpenCV 3+ ?
*
* AUTHOR : Matthew R. Miller START DATE : March 03/08/2019
*
* CHANGES : N/A - N/A
*
* VERSION DATE WHO DETAIL
* 0.1 03/08/2019 Matthew R. Miller Initial Rev
*
************************************************************************/
#include "filterworker.h"
#include "signalsuppressor.h"
#include "filtermenu.h"
#include <QMutex>
#include <QString>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/ocl.hpp>
#include <QDebug>
/* Constructor initializes the appropriate member variables for the worker object. */
FilterWorker::FilterWorker(const cv::Mat *masterImage, cv::Mat *previewImage, QMutex *mutex, QObject *parent)
: QObject(parent)
{
emit updateStatus("Filter Menu initializing...");
mutex_m = mutex;
masterImage_m = masterImage;
previewImage_m = previewImage;
qDebug() << "Filter Worker Created! - Images:" << masterImage_m << previewImage_m;
emit updateStatus("");
}
// destructor
FilterWorker::~FilterWorker()
{
qDebug() << "FilterWorker destroyed";
}
/* This member (slot) recieves the data from the controlling class (slow thread). The data
* is sent as a pointer to the class itself who's member contains the data. To see how this
* works see signalsuppressor.h/cpp. The format is tied to the associated menu object. */
void FilterWorker::receiveSuppressedSignal(SignalSuppressor *dataContainer)
{
data_m = dataContainer->getNewData().toByteArray();
int *parameters = reinterpret_cast<int*>(data_m.data());
performImageFiltering(parameters);
emit updateDisplayedImage();
}
/* This slot is used to update the member addresses for the master and preview images stored
* in the controlling thread. If the Mat's become empty in the controlling thread this slot
* should be signaled with nullptrs to signify they are empty. */
void FilterWorker::receiveImageAddresses(const cv::Mat *masterImage, cv::Mat *previewImage)
{
masterImage_m = masterImage;
previewImage_m = previewImage;
qDebug() << "Filter Worker Images:" << masterImage_m << previewImage_m;
}
/* Sets the kernel radius for a filter to a maximum of 0.015 times the smallest image dimension.
* The kernel size is then scaled between 1 and 100% of its maximum size through weightPercent.
* The result of this function must also always be odd. */
int FilterWorker::kernelSize(QSize image, int weightPercent)
{
int ksize;
if(image.width() > image.height())
ksize = image.height();
else
ksize = image.width();
if(weightPercent > 100)
weightPercent = 100;
else if(weightPercent < 1)
weightPercent = 1;
ksize *= 0.015 * (weightPercent / 100.0);
return ksize | 1;
}
/* Generates a 2D Laplacian kernel for use with OpenCV's Filter2D function. If the desired size is
* negative, the kernel will output a Mat with one element of 1. If passed an even size, the next
* greatest odd size is used.*/
cv::Mat FilterWorker::makeLaplacianKernel(int size)
{
if(size < 1)
size = 1;
size |= 1; //must be odd
int matCenter = size >> 1;
//fill new kernel with zeroes
cv::Mat newKernel = cv::Mat::zeros(size, size, CV_32F);
//fill matrix from center; traverse approx 1/4 elements
int kernelPoint;
int kernelSum = 0;
for(int i = 0; i < matCenter + 1; i++)
{
for(int j = 0; j < matCenter + 1; j++)
{
kernelPoint = -(1 + i + j - matCenter);
if (kernelPoint > 0) kernelPoint = 0;
//top left
newKernel.at<float>(cv::Point(i, j)) = kernelPoint;
//bottom right
newKernel.at<float>(cv::Point(size - i - 1, size - j - 1)) = kernelPoint;
//do not write & count multiple times and to sum properly
if(i != size >> 1 && j != size >> 1)
{
kernelSum += kernelPoint * 4;
//top right
newKernel.at<float>(cv::Point(size - i - 1, j)) = kernelPoint;
//bottom left
newKernel.at<float>(cv::Point(i, size - j - 1)) = kernelPoint;
}
else
{
kernelSum += kernelPoint * 2;
}
}
}
//adjust the kernel sum to exclude the center point. Invert and set as center.
kernelSum -= newKernel.at<float>(cv::Point(matCenter, matCenter)) * 2;
newKernel.at<float>(cv::Point(matCenter, matCenter)) = -kernelSum;
return newKernel;
}
/* Performs the smoothing, sharpening, and edge detection operations from the Filter menu
* in the GUI. Switch statement selects the type of smoothing that will be applied to the
* image in the master buffer. The parameter array passes all the necessary parameters to
* the worker thread based on the openCV functions it calls.*/
void FilterWorker::performImageFiltering(int *parameter)
{
emit updateStatus("Working...");
if(mutex_m) mutex_m->lock();
if(masterImage_m == nullptr || previewImage_m == nullptr)
{
if(mutex_m) mutex_m->unlock();
qDebug() << "Cannot perform Adjustments, image not attached";
return;
}
switch (parameter[FilterMenu::KernelOperation])
{
case FilterMenu::SmoothFilter:
{
int ksize = kernelSize(QSize(masterImage_m->cols, masterImage_m->rows), parameter[FilterMenu::KernelWeight]);
switch (parameter[FilterMenu::KernelType])
{
case FilterMenu::FilterGaussian:
{
//For Gaussian, sigma should be 1/4 size of kernel. (HAS GLITCH WITH UMAT OUTPUT)
cv::GaussianBlur(*masterImage_m, *previewImage_m, cv::Size(ksize, ksize), ksize * 0.25);
qDebug() << "Filter Gaussian" << ksize;
break;
}
case FilterMenu::FilterMedian:
{
cv::medianBlur(*masterImage_m, *previewImage_m, ksize);
qDebug() << "Filter Median" << ksize;
break;
}
default: //FilterMenu::FilterAverage
{
cv::blur(*masterImage_m, *previewImage_m, cv::Size(ksize, ksize));
qDebug() << "Filter Average" << ksize;
break;
}
}
break;
}
case FilterMenu::SharpenFilter:
{
int ksize = kernelSize(QSize(masterImage_m->cols, masterImage_m->rows), parameter[FilterMenu::KernelWeight]);
switch (parameter[FilterMenu::KernelType])
{
case FilterMenu::FilterLaplacian:
{
//blur first to reduce noise
cv::GaussianBlur(*masterImage_m, *previewImage_m, cv::Size(3, 3), 0);
cv::filter2D(*previewImage_m, *previewImage_m, CV_8U,
makeLaplacianKernel(parameter[FilterMenu::KernelWeight]));
cv::addWeighted(*masterImage_m, .9, *previewImage_m, .1, 255 * 0.1, *previewImage_m, masterImage_m->depth());
break;
}
default: //FilterMenu::FilterUnsharpen
{
cv::GaussianBlur(*masterImage_m, tmpImage_m, cv::Size(ksize, ksize), ksize * 0.25);
cv::addWeighted(*masterImage_m, 1.5, tmpImage_m, -0.5, 0, *previewImage_m, masterImage_m->depth());
break;
}
}
break;
}
case FilterMenu::EdgeFilter:
{
switch (parameter[FilterMenu::KernelType])
{
//these opencv functions can have aperature size of 1/3/5/7
case FilterMenu::FilterLaplacian:
{
cv::Laplacian(*masterImage_m, *previewImage_m, CV_8U, parameter[FilterMenu::KernelWeight]);
break;
}
case FilterMenu::FilterSobel:
{
cv::Sobel(*masterImage_m, tmpImage_m, CV_8U, 1, 0, parameter[FilterMenu::KernelWeight]);
cv::Sobel(*masterImage_m, *previewImage_m, CV_8U, 0, 1, parameter[FilterMenu::KernelWeight]);
cv::addWeighted(tmpImage_m, 0.5, *previewImage_m, 0.5, 0, *previewImage_m, masterImage_m->depth());
break;
}
default: //FilterMenu::FilterCanny
{
cv::Canny(*masterImage_m, *previewImage_m, 80, 200, parameter[FilterMenu::KernelWeight]);
qDebug() << "channels:" << QString::number(masterImage_m->channels());
break;
}
}
break;
}
default:
qDebug() << "NO FILTERING OCCURED";
break;
}
//after computation is complete, push image and histogram to GUI if changes were made
if(mutex_m) mutex_m->unlock();
emit updateStatus("");
}