博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

OpenCV (十)提取水平线与垂直线

步骤:

  1. 读取图片
  2. 转换为灰度图片
  3. 将灰度图片转化为二值图片 adaptiveThreshold()
  4. 腐蚀
  5. 膨胀

 

adapativeThreshold():

 

 

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

Mat src, dst, bilary_gray, output;

int main(int argc, char** argv) {
	src = imread("D:/OpenCVprj/image/test6.jpg");
	if (!src.data) {
		cout << "Could not load image......\n" << endl; 
			return -1;
	}
	imshow("src", src);

	cvtColor(src, dst, CV_BGR2GRAY);
	imshow("dst", dst);
	adaptiveThreshold(~dst, bilary_gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
	//double maxValue,二值图像最大值
	//int adaptiveMethod,自适应方法,只能其中之一 - ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C 
	//int thresholdType,阈值类型
	//int blockSize,块大小
	//double C, 常量C 可以是正数,0,负数
	imshow("bilary_gray", bilary_gray);
	Mat kernel1 = getStructuringElement(MORPH_RECT, Size(dst.cols/16, 1), Point(-1, -1));
	//Mat kernel2 = getStructuringElement(MORPH_RECT, Size(1, dst.rows/16), Point(-1, -1));
	erode(bilary_gray, dst, kernel1);
	dilate(dst, output, kernel1);
	bitwise_not(output, output);
	imshow("output", output);

	waitKey(0);
	return 0;
}

  

 

应用补充:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

Mat src, dst, bilary_gray, output;

int main(int argc, char** argv) {
	src = imread("D:/OpenCVprj/image/test6.jpg");
	if (!src.data) {
		cout << "Could not load image......\n" << endl; 
			return -1;
	}
	imshow("src", src);

	cvtColor(src, dst, CV_BGR2GRAY);
	imshow("dst", dst);
	adaptiveThreshold(~dst, bilary_gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
	//double maxValue,二值图像最大值
	//int adaptiveMethod,自适应方法,只能其中之一 - ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C 
	//int thresholdType,阈值类型
	//int blockSize,块大小
	//double C, 常量C 可以是正数,0,负数
	imshow("bilary_gray", bilary_gray);
	//Mat kernel1 = getStructuringElement(MORPH_RECT, Size(dst.cols/16, 1), Point(-1, -1));
	//Mat kernel2 = getStructuringElement(MORPH_RECT, Size(1, dst.rows/16), Point(-1, -1));
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	erode(bilary_gray, dst, kernel);
	dilate(dst, output, kernel);
	bitwise_not(output, output);
	//blur(output, output, Size(3, 3), Point(-1, -1));
	imshow("output", output);
	
	waitKey(0);
	return 0;
}

  

posted @ 2019-07-24 09:43  haibochina  阅读(2171)  评论(0编辑  收藏  举报