-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanBoardFilter.cpp
214 lines (170 loc) · 6.23 KB
/
ScanBoardFilter.cpp
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
//
// ScanBoardFilter.cpp
// bookseg
//
// Created by brdev on 15/12/4.
// Copyright © 2015年 brdev. All rights reserved.
//
#include "ScanBoardFilter.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
void Scan::calhistOfRGB(Mat& src)
{
Mat dst;
//分割图像为3个通道即:B, G and R
vector<Mat> bgr_planes;
split( src, bgr_planes );
//创建箱子的数目
int histSize = 256;
//设置范围 ( for B,G,R) )
float range[] = { 0, 256 } ;//不包含上界256
const float* histRange = { range };
//归一化,起始位置直方图清除内容
bool uniform = true; bool accumulate = false;
Mat b_hist, g_hist, r_hist;
//计算每个平面的直方图
//&bgr_planes[]原数组,1原数组个数,0只处理一个通道,
//Mat()用于处理原来数组的掩膜,b_hist将要用来存储直方图的Mat对象
//1直方图的空间尺寸,histsize每一维的箱子数目,histrange每一维的变化范围
//uniform和accumulate箱子的大小一样,直方图开始的位置清除内容
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
//画直方图( B, G and R)
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
//归一化结果为 [ 0, histImage.rows ]
//b_hist输入数组,b_hist输出数组,
//0和histImage.rows归一化的两端限制值,
//NORM_MINMAX归一化类型 -1输出和输入类型一样,Mat()可选掩膜
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
//为每个通道画图
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0
);
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0
);
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0
);
}
//显示输出结果
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage );
waitKey(10);
}
float Scan::getGamma(Mat &src)
{
CV_Assert(src.data);
CV_Assert(src.depth() != sizeof(uchar));
int height = src.rows;
int width = src.cols;
long size = height * width;
//!< histogram
float histogram[256] = {0};
uchar pvalue = 0;
MatIterator_<uchar> it, end;
for( it = src.begin<uchar>(), end = src.end<uchar>(); it != end; it++ )
{
pvalue = (*it);
histogram[pvalue]++;
}
int threshold = 0; //otsu阈值
long sum0 = 0, sum1 = 0; //前景的灰度总和和背景灰度总和
long cnt0 = 0, cnt1 = 0; //前景的总个数和背景的总个数
double w0 = 0, w1 = 0; //前景和背景所占整幅图像的比例
double u0 = 0, u1 = 0; //前景和背景的平均灰度
double u = 0; //图像总平均灰度
double variance = 0; //前景和背景的类间方差
double maxVariance = 0; //前景和背景的最大类间方差
int i, j;
for(i = 1; i < 256; i++) //一次遍历每个像素
{
sum0 = 0;
sum1 = 0;
cnt0 = 0;
cnt1 = 0;
w0 = 0;
w1 = 0;
for(j = 0; j < i; j++)
{
cnt0 += histogram[j];
sum0 += j * histogram[j];
}
u0 = (double)sum0 / cnt0;
w0 = (double)cnt0 / size;
for(j = i ; j <= 255; j++)
{
cnt1 += histogram[j];
sum1 += j * histogram[j];
}
u1 = (double)sum1 / cnt1;
w1 = 1 - w0; // (double)cnt1 / size;
u = u0 * w0 + u1 * w1;
//variance = w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2);
variance = w0 * w1 * (u0 - u1) * (u0 - u1);
if(variance > maxVariance)
{
maxVariance = variance;
threshold = i;
}
}
return threshold / 255.0;
}
void Scan::gammaCorrection(Mat &src, Mat &dst, float fGamma)
{
CV_Assert(src.data);
// accept only char type matrices
CV_Assert(src.depth() != sizeof(uchar));
// build look up table
unsigned char lut[256];
for( int i = 0; i < 256; i++ )
{
lut[i] = saturate_cast<uchar>(pow((float)(i/255.0), fGamma) * 255.0f);
}
// case 1 and 3 for different channels
dst = src.clone();
const int channels = dst.channels();
switch(channels)
{
case 1:
{
MatIterator_<uchar> it, end;
for( it = dst.begin<uchar>(), end = dst.end<uchar>(); it != end; it++ )
*it = lut[(*it)];
break;
}
case 3:
{
MatIterator_<Vec3b> it, end;
for( it = dst.begin<Vec3b>(), end = dst.end<Vec3b>(); it != end; it++ )
{
(*it)[0] = lut[((*it)[0])]; // B
(*it)[1] = lut[((*it)[1])]; // G
(*it)[2] = lut[((*it)[2])]; // R
}
break;
}
} // end for switch
// transpose(dst, dst);
// flip(dst, dst, 1);
// transpose(dst, dst);
// transpose(dst, dst);
// transpose(dst, dst);
// transpose(dst, dst);
}