这个二值化算法主要针对文档图像,原理较复杂,感兴趣的同学可以自己搜一下论文看看。下面先给出代码
void SauvolA(vector<string> &FilesVec,vector<Mat> &outputImg,string root)
{
char version;
int c;
int winx = 0, winy = 0;
float optK = 0.5;
bool didSpecifyK = false;
string Saveroot;
// Determine the method
for (int i = 0;i < FilesVec.size();i++)
{
cout << "image name: " << FilesVec[i] << endl;
Mat input = imread(FilesVec[i], CV_LOAD_IMAGE_GRAYSCALE);
if ((input.rows <= 0) || (input.cols <= 0))
{
cout << "*** ERROR: Couldn't read input image " << FilesVec[i] << endl;
exit(1);
}
Saveroot.clear();
Saveroot = root;
Saveroot = root + (FilesVec[i].substr(FilesVec[i].find_last_of("\\") + 1));
winy = (int)(2.0 * input.rows - 1) / 3;
winx = (int)input.cols - 1 < winy ? input.cols - 1 : winy;
if (winx > 100)
winx = winy = 40;
Mat output(input.rows, input.cols, CV_8U);
NiblackSauvolaWolfJolion(input, output, winx, winy, optK, 128);
outputImg.push_back(output);
cerr << "Writing binarized image to file '" << Saveroot << "'.\n";
imwrite(Saveroot, output);
}
cout << "end" << endl;
}
void NiblackSauvolaWolfJolion(Mat im, Mat output, int winx, int winy, double k, double dR)//k=0.5,dR=128;
{
double m, s, max_s;
double th = 0;
double min_I, max_I;
int wxh = winx / 2;
int wyh = winy / 2;
in