int main(int argc, char** argv) {
Mat src, dst;
src = imread(STRPAHT3);
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "result image";
Mat gray_src;
//转灰度图
cvtColor(src, gray_src, CV_BGR2GRAY);
//imshow("gray image", gray_src);
Mat binImg;
//转换为二值图像 – adaptiveThreshold
/*
Mat src, // 输入的灰度图像
Mat dest, // 二值图像
double maxValue, // 二值图像最大值
int adaptiveMethod // 自适应方法,只能其中之一 ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C
int thresholdType, // 阈值类型
int blockSize, // 块大小
double C // 常量C 可以是正数,0,负数
*/
adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
//imshow("binary image", binImg);
//一个像素宽的水平线 - 水平长度 width / 30
//一个像素宽的垂直线 – 垂直长度 height / 30
int xsize = binImg.cols / 30;
int ysize = binImg.rows / 30;
// 水平结构元素
Mat hline = getStructuringElement(MORPH_RECT, Size(xsize, 1), Point(-1, -1));
// 垂直结构元素
Mat vline = getStructuringElement(MORPH_RECT, Size(1, ysize), Point(-1, -1));
//矩形
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
//提取横线
Mat hbin;
erode(binImg, hbin, hline);
dilate(hbin, dst, hline);
//imshow("Final Result", dst);
//提取竖线
Mat vbin;
erode(binImg, vbin, vline);
dilate(vbin, dst, vline);
//imshow("Final Result", dst);
//矩形
Mat Bbin;
erode(binImg, Bbin, kernel);
dilate(Bbin, dst, kernel);
//像素取反操作
bitwise_not(dst, dst);
imshow("Final Result", dst);
waitKey(0);
return 0;
}