#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src,dst;
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);
//水平 垂直
//构造一个结构元素是:水平线
Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1),Point(-1, -1));
Mat vline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));
Mat temp;
erode(bin_image1, temp, hline); //先腐蚀水平
dilate(temp, dst, hline); //后膨胀水平
imshow("Final result", temp); //得到的只保留水平线
waitKey(0);
return 0;
}
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src,dst;
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);
//水平 垂直
//构造一个结构元素是:水平线
Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1),Point(-1, -1));
Mat vline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));
Mat temp;
erode(bin_image1, temp, hline); //先腐蚀水平
dilate(temp, dst, hline); //后膨胀水平
imshow("Final result", temp); //得到的只保留水平线
waitKey(0);
return 0;
}