-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binarization.cc
142 lines (128 loc) · 3.55 KB
/
Binarization.cc
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
#include <iostream>
#include <cmath>
#include <vector>
#include <omp.h>
#include "Binarization.h"
// rgb转灰度图
void RGBToGRAY(const unsigned char* src, unsigned char* dst, int width, int height) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
dst[i] = 0.299 * src[i * 3 + 0] + 0.587 * src[i * 3 + 1] + 0.114 * src[i * 3 + 2];
}
}
// 转灰度图
void ImageToGRAY(const unsigned char* src, unsigned char* dst, int width, int height, int channels) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
dst[i] = 0.299 * src[i * channels + 0] + 0.587 * src[i * channels + 1] + 0.114 * src[i * channels + 2];
}
}
// 二进制阈值化
void BinaryThresholded(const unsigned char* src, unsigned char* dst, int width, int height, double thresh) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
if (src[i] < thresh) {
dst[i] = 0;
} else {
dst[i] = 255;
}
}
}
// 反二进制阈值化
void AntiBinaryThresholding(const unsigned char* src, unsigned char* dst, int width, int height, double thresh) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
if (src[i] < thresh) {
dst[i] = 255;
} else {
dst[i] = 0;
}
}
}
// 截断阈值化
void TruncationThresholding(const unsigned char* src, unsigned char* dst, int width, int height, double thresh) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
if (src[i] < thresh) {
dst[i] = src[i];
} else {
dst[i] = thresh;
}
}
}
// 反阈值化为0
void AntiThresholdingTo0(const unsigned char* src, unsigned char* dst, int width, int height, double thresh) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
if (src[i] < thresh) {
dst[i] = src[i];
} else {
dst[i] = 0;
}
}
}
// 阈值化为0
void ThresholdingTo0(const unsigned char* src, unsigned char* dst, int width, int height, double thresh) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
if (src[i] < thresh) {
dst[i] = 0;
} else {
dst[i] = src[i];
}
}
}
// 平均值阈值
double AverageValueThreshold(const unsigned char* src, int width, int height) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
int sum = 0;
for (size_t i = 0; i < width * height; i++) {
sum += src[i];
}
return (double)sum / (double)(width * height); // 修正括号位置
}
// 中值阈值
double MedianThreshold(const unsigned char* src, int width, int height) {
int sum[256] = {0};
int threshold = 0;
int medianCount = (width * height) / 2; // 中值位置
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (size_t i = 0; i < width * height; i++) {
sum[src[i]]++;
}
// 找到中值位置对应的灰度级
for (int i = 0; i < 256; i++) {
medianCount -= sum[i];
if (medianCount <= 0) {
threshold = i;
break;
}
}
return (double)threshold;
}
// 高斯滤波
void GaussianFilter(const unsigned char* src, unsigned char* dst, int width, int height) {}
// 自适应阈值
void AdaptiveThreshold(const unsigned char* src, unsigned char* dst, int width, int height) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
}