-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcess.cpp
More file actions
291 lines (234 loc) · 7.63 KB
/
ImageProcess.cpp
File metadata and controls
291 lines (234 loc) · 7.63 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
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
286
287
288
289
290
#include "pch.h"
#include "ImageProcess.h"
Mat ImageProcess::RGB_2_Gray(Mat src)
{
Mat result;
cvtColor(src, result, cv::COLOR_RGB2GRAY);
return result;
}
bool CheckSrcIsNull(Mat src)
{
return src.empty() ? true : false;
}
void ImageProcess::Show_Histogram(Mat src)
{
Mat image_gray, hist; //定义输入图像,灰度图像, 直方图
if (src.empty())
{
MessageBoxA(NULL, "请先导入图片!!!", "警告", MB_OK);
return;
}
cvtColor(src, image_gray, COLOR_BGR2GRAY); //灰度化
//imshow("image_gray", image_gray); //显示灰度图像
//获取图像直方图
int histsize = 256;
float ranges[] = { 0,256 };
const float* histRanges = { ranges };
calcHist(&image_gray, 1, 0, Mat(), hist, 1, &histsize, &histRanges, true, false);
//创建直方图显示图像
int hist_h = 300;//直方图的图像的高
int hist_w = 512; //直方图的图像的宽
int bin_w = hist_w / histsize;//直方图的等级
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));//绘制直方图显示的图像
//绘制并显示直方图
normalize(hist, hist, 0, hist_h, NORM_MINMAX, -1, Mat());//归一化直方图
for (int i = 1; i < histsize; i++)
{
line(histImage, Point((i - 1) * bin_w, hist_h - cvRound(hist.at<float>(i - 1))),
Point((i)*bin_w, hist_h - cvRound(hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);
}
imshow("histImage", histImage);
waitKey(0); //暂停,保持图像显示,等待按键结束
}
Mat ImageProcess::EqualizeHist(Mat src)
{
Mat image_gray, image_enhanced; //定义灰度图像、直方图
if (src.empty())
{
MessageBoxA(NULL, "请先导入图片!!!", "警告", MB_OK);
return Mat();
}
cvtColor(src, image_gray, COLOR_BGR2GRAY); //灰度化
//imshow("image_gray", image_gray);
equalizeHist(image_gray, image_enhanced);
//imshow("image_enhanced", image_enhanced);
return image_enhanced;
}
Mat ImageProcess::GrayScaleLinearTransform(Mat inputImage, double alpha, double beta)
{
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
//线性变换
Mat transformImage = Mat::zeros(grayImage.size(), CV_8U);
for (int y = 0; y < grayImage.rows; y++)
{
for (int x = 0; x < grayImage.cols; x++)
{
int originPixelValue = static_cast<int>(grayImage.at<uchar>(y, x));
int newPixelValue = cv::saturate_cast<uchar>(alpha * originPixelValue + beta);
transformImage.at<uchar>(y, x) = newPixelValue;
}
}
return transformImage;
}
Mat ImageProcess::GrayLogTransform(Mat inputImage, double c)
{
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
//对数变换 new_image = c * log(1 + origin)
Mat newTransformImage = Mat::zeros(grayImage.size(),CV_8U);
for (int y = 0; y < inputImage.rows; y++)
{
for (int x = 0; x < inputImage.cols; x++)
{
int originalPixelValue = static_cast<int>(grayImage.at<uchar>(y, x));
int newPixelValue = saturate_cast<uchar>(c * std::log(1 + originalPixelValue));
newTransformImage.at<uchar>(y, x) = newPixelValue;
}
}
return newTransformImage;
}
Mat ImageProcess::GrayScaleGamaTransform(Mat inputImage, double gama)
{
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
//灰度伽马变换 new_image = 255 * (origin / 255) ^ gama
Mat newTransformImage = Mat::zeros(grayImage.size(), CV_8U);
for (int y = 0; y < inputImage.rows; y++)
{
for (int x = 0; x < inputImage.cols; x++)
{
int originalPixelValue = static_cast<int>(grayImage.at<uchar>(y, x));
int newPixelValue = saturate_cast<uchar>(255 * std::pow(originalPixelValue / 255, gama));
newTransformImage.at<uchar>(y, x) = newPixelValue;
}
}
return newTransformImage;
}
Mat ImageProcess::ImageErosion(Mat inputImage, int erosionSize)
{
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2 * erosionSize + 1, 2 * erosionSize + 1),cv::Point(erosionSize, erosionSize));
// 进行腐蚀操作
cv::Mat erodedImage;
cv::erode(grayImage, erodedImage, element);
return erodedImage;
}
Mat ImageProcess::ImageDilation(Mat inputImage, int dilationSize)
{
// 将RGB图像转换为灰度图像
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
// 定义膨胀核(结构元素)
Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2 * dilationSize + 1, 2 * dilationSize + 1),
cv::Point(dilationSize, dilationSize));
// 进行膨胀操作
Mat dilatedImage;
dilate(grayImage, dilatedImage, element);
return dilatedImage;
}
Mat ImageProcess::ImageThresholdSegmentation(Mat inputImage, int thresholdSize)
{
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
Mat segmentImage;
threshold(grayImage, segmentImage, thresholdSize, 255, cv::THRESH_BINARY);
return segmentImage;
}
Mat ImageProcess::ImageAdaptiveThresholdSegmentation(Mat inputImage, int blockSize, int subtractValue)
{
// 将RGB图像转换为灰度图像
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
// 进行自适应阈值分割
Mat segmentedAdaptive;
adaptiveThreshold(grayImage, segmentedAdaptive, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C,
cv::THRESH_BINARY, blockSize, subtractValue);
return segmentedAdaptive;
}
Mat ImageProcess::RegionGrowingSegmentation(Mat inputImage, int seedX, int seedY, int thresholdSize)
{
// 将RGB图像转换为灰度图像
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
// 初始化分割图像
Mat segmentedImage = cv::Mat::zeros(grayImage.size(), CV_8U);
// 获取种子点的初始灰度值
int seedValue = static_cast<int>(grayImage.at<uchar>(seedY, seedX));
// 创建队列用于保存待处理的像素坐标
std::queue<std::pair<int, int>> pixelQueue;
pixelQueue.push(std::make_pair(seedX, seedY));
// 开始区域生长
while (!pixelQueue.empty()) {
int x = pixelQueue.front().first;
int y = pixelQueue.front().second;
pixelQueue.pop();
// 检查当前像素是否已被处理过
if (segmentedImage.at<uchar>(y, x) == 0) {
// 计算当前像素与种子点的灰度差
int pixelValue = static_cast<int>(grayImage.at<uchar>(y, x));
int diff = std::abs(seedValue - pixelValue);
// 如果灰度差小于阈值,则加入分割区域
if (diff <= thresholdSize) {
segmentedImage.at<uchar>(y, x) = 255;
// 将当前像素的邻域加入队列
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
int newX = x + dx;
int newY = y + dy;
if (newX >= 0 && newX < grayImage.cols && newY >= 0 && newY < grayImage.rows) {
pixelQueue.push(std::make_pair(newX, newY));
}
}
}
}
}
}
return segmentedImage;
}
Mat ImageProcess::SobelEdgeDetection(Mat inputImage, int kernelSize)
{
Mat grayImage;
cvtColor(inputImage, grayImage, COLOR_BGR2GRAY);
//进行Sobel边缘监测
Mat gradientX, gradientY;
Sobel(grayImage, gradientX, CV_16S, 1, 0, kernelSize);
Sobel(grayImage, gradientY, CV_16S, 0, 1, kernelSize);
//计算梯度幅值
Mat absGradientX, absGradientY;
convertScaleAbs(gradientX, absGradientX);
convertScaleAbs(gradientX, absGradientY);
//合并x和y的梯度幅值
Mat gradientImage;
addWeighted(absGradientX, 0.5, absGradientY, 0.5, 0, gradientImage);
return gradientImage;
}
Mat ImageProcess::CannyEdgeDetection(Mat inputImage, double threshold1, double threshold2)
{
// 将RGB图像转换为灰度图像
Mat grayImage;
cvtColor(inputImage, grayImage, cv::COLOR_BGR2GRAY);
// 进行Canny边缘检测
Mat edgeImage;
Canny(grayImage, edgeImage, threshold1, threshold2);
return edgeImage;
}
Mat ImageProcess::MeanFilter(Mat inputImage, int kernelSize)
{
Mat filterImage;
blur(inputImage, filterImage, cv::Size(kernelSize, kernelSize));
return filterImage;
}
Mat ImageProcess::MedianFilter(Mat inputImage, int kernelSize)
{
Mat filterImage;
medianBlur(inputImage, filterImage, kernelSize);
return filterImage;
}
Mat ImageProcess::GaussianFilter(Mat inputImage, int kernelSize, double sigma)
{
Mat filterImage;
GaussianBlur(inputImage, filterImage, cv::Size(kernelSize, kernelSize), sigma);
return filterImage;
}