-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary
91 lines (90 loc) · 2.38 KB
/
Binary
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
typedef struct
{
int index;//下标
int freq;//频数
}Peak;
void Binary()
{
char imageName[] = "C:\\Users\\24932\\Desktop\\landsat8\\123.png";
Mat M = imread(imageName, IMREAD_COLOR); // 读入图片
if (M.empty()) // 判断文件是否正常打开
{
cout << "open file error";
}
imshow("原始图像", M);
int channel = M.channels();
int rows = M.rows;
int cols = M.cols;
Mat impGray(rows, cols, CV_8UC1);
if (channel == 3)
{
impGray = ToGrey(M).clone();
imshow("灰度图像", impGray);
}
if (channel == 1)
{
impGray = M.clone();
}
imshow("灰度图像", impGray);
Peak peaks[256];//波峰
int histogram[256];//直方图
for (int i = 0; i < 255; i++) histogram[i] = 0;//初始直方图各个灰度值个数为0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
histogram[impGray.at<uchar>(i, j)]++;//统计各个灰度出现的次数得到直方图
}
}
int size = 0;//波峰数
for (int i = 1; i < 254; i++)//统计波峰数量与频数
{
if (histogram[i] > histogram[i - 1] && histogram[i] > histogram[i + 1])
{
peaks[size].index = i;
peaks[size].freq = histogram[i];
size++;
}
}
//寻找最大的两个波峰
Peak Max = peaks[0], Snd = peaks[0];
for (int i = 0; i <size; i++)
{
if (peaks[i].freq > Max.freq)
{
Snd = Max;
Max = peaks[i];
}
else if (peaks[i].freq>Snd.freq)
{
Snd = peaks[i];
}
}
//找到波谷;
int q = min(Max.index, Snd.index);//双峰值较小下标
int p = max(Max.index, Snd.index);//双峰值较大下标
Peak k = Max;//波谷点
for (;q < p; q++)
{
if (histogram[q]<k.freq)
{
k.freq = histogram[q];
k.index = q;
}
}
//二值化
Mat Mq(rows, cols, CV_8UC1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (impGray.at<uchar>(i, j) < k.index)
Mq.at<uchar>(i, j) =0;
else
Mq.at<uchar>(i, j) =255;
}
}
imshow("二值化后图像", Mq);
imwrite("C:\\Users\\24932\\Desktop\\landsat8\\峰值法二值化.png", Mq);
waitKey();
}