-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageprocessor.cpp
More file actions
199 lines (168 loc) · 5.53 KB
/
imageprocessor.cpp
File metadata and controls
199 lines (168 loc) · 5.53 KB
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
#include "imageprocessor.h"
#include <QDebug>
#ifdef HAVE_OPENCV
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/photo.hpp>
#endif
ImageProcessor::ImageProcessor(QObject *parent)
: QObject(parent)
{
}
#ifdef HAVE_OPENCV
cv::Mat ImageProcessor::loadImage(const QString &path)
{
cv::Mat img = cv::imread(path.toStdString(), cv::IMREAD_UNCHANGED);
if (img.empty()) {
qDebug() << "無法載入圖像 / Failed to load image:" << path;
}
return img;
}
QImage ImageProcessor::matToQImage(const cv::Mat &mat)
{
if (mat.empty()) {
return QImage();
}
cv::Mat temp;
// 根據通道數和深度轉換 / Convert based on channels and depth
switch (mat.type()) {
case CV_8UC4: {
// BGRA to RGBA
cv::cvtColor(mat, temp, cv::COLOR_BGRA2RGBA);
QImage image(temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGBA8888);
return image.copy();
}
case CV_8UC3: {
// BGR to RGB
cv::cvtColor(mat, temp, cv::COLOR_BGR2RGB);
QImage image(temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
return image.copy();
}
case CV_8UC1: {
// Grayscale
QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Grayscale8);
return image.copy();
}
default: {
// 轉換為 8UC3 / Convert to 8UC3
mat.convertTo(temp, CV_8UC3);
cv::cvtColor(temp, temp, cv::COLOR_BGR2RGB);
QImage image(temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
return image.copy();
}
}
}
cv::Mat ImageProcessor::qImageToMat(const QImage &image)
{
if (image.isNull()) {
return cv::Mat();
}
QImage img = image.convertToFormat(QImage::Format_RGB888);
cv::Mat mat(img.height(), img.width(), CV_8UC3, (void*)img.constBits(), img.bytesPerLine());
cv::Mat result;
cv::cvtColor(mat, result, cv::COLOR_RGB2BGR);
return result.clone();
}
cv::Mat ImageProcessor::applyGaussianBlur(const cv::Mat &input, int ksize, double sigmaX, double sigmaY)
{
if (input.empty()) return cv::Mat();
cv::Mat result;
// 確保 ksize 為奇數 / Ensure ksize is odd
if (ksize % 2 == 0) ksize++;
cv::GaussianBlur(input, result, cv::Size(ksize, ksize), sigmaX, sigmaY);
return result;
}
cv::Mat ImageProcessor::applyCanny(const cv::Mat &input, double threshold1, double threshold2, int apertureSize)
{
if (input.empty()) return cv::Mat();
cv::Mat gray, result;
if (input.channels() > 1) {
cv::cvtColor(input, gray, cv::COLOR_BGR2GRAY);
} else {
gray = input.clone();
}
cv::Canny(gray, result, threshold1, threshold2, apertureSize);
return result;
}
cv::Mat ImageProcessor::applyBrightnessContrast(const cv::Mat &input, double alpha, int beta)
{
if (input.empty()) return cv::Mat();
cv::Mat result;
input.convertTo(result, -1, alpha, beta);
return result;
}
cv::Mat ImageProcessor::applyGamma(const cv::Mat &input, double gamma)
{
if (input.empty()) return cv::Mat();
cv::Mat lookupTable(1, 256, CV_8U);
uchar* p = lookupTable.ptr();
for (int i = 0; i < 256; ++i) {
p[i] = cv::saturate_cast<uchar>(pow(i / 255.0, gamma) * 255.0);
}
cv::Mat result;
cv::LUT(input, lookupTable, result);
return result;
}
cv::Mat ImageProcessor::convertColor(const cv::Mat &input, int code)
{
if (input.empty()) return cv::Mat();
cv::Mat result;
cv::cvtColor(input, result, code);
return result;
}
cv::Mat ImageProcessor::resize(const cv::Mat &input, int width, int height, int interpolation)
{
if (input.empty()) return cv::Mat();
cv::Mat result;
cv::resize(input, result, cv::Size(width, height), 0, 0, interpolation);
return result;
}
cv::Mat ImageProcessor::rotate(const cv::Mat &input, double angle)
{
if (input.empty()) return cv::Mat();
cv::Point2f center(input.cols / 2.0, input.rows / 2.0);
cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, angle, 1.0);
cv::Mat result;
cv::warpAffine(input, result, rotationMatrix, input.size());
return result;
}
cv::Mat ImageProcessor::flip(const cv::Mat &input, int flipCode)
{
if (input.empty()) return cv::Mat();
cv::Mat result;
cv::flip(input, result, flipCode);
return result;
}
cv::Mat ImageProcessor::threshold(const cv::Mat &input, double thresh, double maxval, int type)
{
if (input.empty()) return cv::Mat();
cv::Mat gray, result;
if (input.channels() > 1) {
cv::cvtColor(input, gray, cv::COLOR_BGR2GRAY);
} else {
gray = input.clone();
}
cv::threshold(gray, result, thresh, maxval, type);
return result;
}
cv::Mat ImageProcessor::morphologyEx(const cv::Mat &input, int op, int shape, int ksize)
{
if (input.empty()) return cv::Mat();
cv::Mat kernel = cv::getStructuringElement(shape, cv::Size(ksize, ksize));
cv::Mat result;
cv::morphologyEx(input, result, op, kernel);
return result;
}
bool ImageProcessor::saveImage(const cv::Mat &image, const QString &path, const QVector<int> ¶ms)
{
if (image.empty()) {
qDebug() << "無法保存空圖像 / Cannot save empty image";
return false;
}
std::vector<int> compressionParams;
for (int param : params) {
compressionParams.push_back(param);
}
return cv::imwrite(path.toStdString(), image, compressionParams);
}
#endif // HAVE_OPENCV