(原創) 如何产生Yokoi Connectivity Number? (.NET) (C++/CLI) (C/C++) (Image Processing)
本范例先将leng.jpg轉成binary image,然後从512*512 downsampling成64*64,downsampling的规则为以8*8为unit,取topmost-left为downsampled data,最后产生Yokoi Connectivity Number。
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : memset1.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to produce Yokoi connectivity number
7
Release : 12/06/2006
8
*/
9
10
#include "stdafx.h"
11
#include <fstream>
12
#include <iostream>
13
14
using namespace System::Drawing;
15
using namespace System::Drawing::Imaging;
16
17
// enum for q,r,s
18
// only support in C++/CLI
19
enum class hType {
20
q,
21
r,
22
s
23
};
24
25
// Binarize image
26
void binarize(Bitmap^ , Bitmap^);
27
// Downsampling image by factor
28
void downSampleing(Bitmap^ , Bitmap^, int, int);
29
// h func for Yokoi
30
hType h(Color, Color, Color, Color);
31
// h func for Yokoi
32
int f(hType, hType, hType, hType);
33
// Process Yokoi connectivity number
34
void yokoi(Bitmap^ , const char*);
35
36
int main() {
37
// Read lena.jpg
38
Bitmap^ oriImg = gcnew Bitmap("lena.jpg");
39
// Declare binary image for lena.jpg
40
Bitmap^ binImg = gcnew Bitmap(oriImg->Width, oriImg->Height);
41
// Binarize lena.jpg
42
binarize(oriImg, binImg);
43
44
// Declare down-sampling image for binarized image
45
Bitmap^ dsImg = gcnew Bitmap(64, 64);
46
// Downsampling image by factor 8*8
47
downSampleing(binImg, dsImg, 8, 8);
48
// Process Yokoi connectivity number
49
yokoi(dsImg, "YokoiMatrix.txt");
50
51
return 0;
52
}
53
54
// Binarize image
55
void binarize(Bitmap^ oriImg, Bitmap^ binImg) {
56
for (int y = 0; y != oriImg->Height; ++y) {
57
for (int x = 0; x != oriImg->Width; ++x) {
58
int gray = (oriImg->GetPixel(x, y).R +
59
oriImg->GetPixel(x, y).G +
60
oriImg->GetPixel(x, y).B) / 3;
61
62
// If intensity >= 128, set the pixel to black.
63
// If intensity < 128, set the pixel to white.
64
if (gray >= 128) {
65
binImg->SetPixel(x, y, Color::White);
66
}
67
else {
68
binImg->SetPixel(x, y, Color::Black);
69
}
70
}
71
}
72
}
73
74
// Downsampling image by factor
75
void downSampleing(Bitmap^ oriImg, Bitmap^ dsImg, int unitX, int unitY) {
76
for (int x = 0, i = 0; x != oriImg->Width; x += unitX, ++i) {
77
for (int y = 0, j = 0; y != oriImg->Height; y += unitY, ++j) {
78
dsImg->SetPixel(i, j,oriImg->GetPixel(x, y));
79
}
80
}
81
}
82
83
// h func for Yokoi
84
// Computer and Robot Vision P.274, Robert M. Haralick
85
hType h(Color b, Color c, Color d, Color e) {
86
if (b == c && (d != b || e != b)) {
87
//return q;
88
return hType::q;
89
}
90
else if (b == c && (d == b && e == b)) {
91
//return r;
92
return hType::r;
93
}
94
else {
95
//return s;
96
return hType::s;
97
}
98
}
99
100
// f func for Yokoi
101
// Computer and Robot Vision P.274, Robert M. Haralick
102
int f(hType a1, hType a2, hType a3, hType a4) {
103
if ((a1 == a2) && (a2 == a3) && (a3 == a4) && (a4 == hType::r)) {
104
return 5;
105
}
106
else {
107
// Count the number of q
108
int n = 0;
109
110
if (a1 == hType::q) ++n;
111
if (a2 == hType::q) ++n;
112
if (a3 == hType::q) ++n;
113
if (a4 == hType::q) ++n;
114
115
return n;
116
}
117
}
118
119
// Process Yokoi connectivity number
120
void yokoi(Bitmap^ oriImg, const char* fileName) {
121
std::ofstream output(fileName);
122
123
for (int y = 0; y != oriImg->Height; ++y) {
124
for (int x = 0; x != oriImg->Width; ++x) {
125
Color x0 = oriImg->GetPixel(x, y);
126
127
// Only process 1 in binary image (White)
128
if (x0 == Color::FromArgb(255,255,255)) {
129
Color x1 = Color::Black;
130
if (x+1 < oriImg->Width) { // Check for the boundary
131
x1 = oriImg->GetPixel(x+1, y);
132
}
133
134
Color x2 = Color::Black;
135
if (y-1 >= 0) {
136
x2 = oriImg->GetPixel(x, y-1);
137
}
138
139
Color x3 = Color::Black;
140
if (x-1 >= 0) {
141
x3 = oriImg->GetPixel(x-1, y);
142
}
143
144
Color x4 = Color::Black;
145
if (y+1 < oriImg->Height) {
146
x4 = oriImg->GetPixel(x, y+1);
147
}
148
149
Color x5 = Color::Black;
150
if (x+1 < oriImg->Width && y+1 < oriImg->Height) {
151
x5 = oriImg->GetPixel(x+1, y+1);
152
}
153
154
Color x6 = Color::Black;
155
if (x+1 < oriImg->Width && y-1 >= 0) {
156
x6 = oriImg->GetPixel(x+1, y-1);
157
}
158
159
Color x7 = Color::Black;
160
if (x-1 >= 0 && y-1 >= 0) {
161
x7 = oriImg->GetPixel(x-1, y-1);
162
}
163
164
Color x8 = Color::Black;
165
if (x-1 >= 0 && y+1 < oriImg->Height) {
166
x8 = oriImg->GetPixel(x-1, y+1);
167
}
168
169
// Computer and Robot Vision P.274, Robert M. Haralick
170
int n = f(h(x0, x1, x6, x2),
171
h(x0, x2, x7, x3),
172
h(x0, x3, x8, x4),
173
h(x0, x4, x5, x1));
174
175
// Write to file
176
output << n << " ";
177
178
}
179
else { // 0 in binary image (Black)
180
output << " " << " ";
181
}
182
}
183
// New line
184
output << std::endl;
185
}
186
}

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

原图

Downsampling

Yokoi Connectivity Number
1
1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 2 2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 1 1 5 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1
63
1 1 1 5 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1
64
1 1 1 1 1 0 0 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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

Reference