输入彩色图像 imread
转换为灰度图像 cvtColor
转换为二值图像 adaptiveThreshold
定义结构元素
开操作(膨胀+腐蚀)提取水平与垂直线
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src;
src = imread("1.jpg");
if (src.empty())
{
printf("could not load image");
}
imshow("input image", src);
Mat gray_src;
cvtColor(src, gray_src, CV_BGR2GRAY);//由彩色图像转换为灰度图像
imshow("gray iamge", gray_src);
Mat bin_image1, bin_image2;
//~取反的意思
//由灰度图像转换为二值图像
adaptiveThreshold(gray_src, bin_image1, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
adaptiveThreshold(~gray_src, bin_image2, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
imshow("bin inage1", bin_image1);
imshow("bin inage2", bin_image2);
waitKey(0);
return 0;
}