OpenCV学习第十三篇:提取水平和垂直线(去除干扰线)

1.结构元素
可以是任意形状的结构元素:矩形,圆,直线,磁盘形状,砖石形状等
2.提取步骤
输入图像彩色图像imread
转换为灰度图像cvtColor
转换为二值图像adaptiveThreshold
定义结构元素
开操作(腐蚀+膨胀)提取水平和垂直线
3.api
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,负数)

直接提取api
morphologyEx(binimage, dst, CV_MOP_OPEN, hline);
4.实例
代码如下:

#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("F:/picture/去除干扰线.png");
	if (src.empty()) {
		printf("could not image");
		return -1;
	}
	Mat  gray_src;
	cvtColor(src, gray_src, CV_BGR2GRAY);
	imshow("灰色", gray_src);

	//转换为二值图像
	Mat binimage;
	adaptiveThreshold(~gray_src, binimage, 255, ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 15, -2);
	imshow("二值", binimage);
	Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16,1), Point(-1, - 1));
	Mat yline = getStructuringElement(MORPH_RECT, Size(1, src.rows/16), Point(-1, -1));
	Mat sline = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1, -1));
	Mat temp;
	erode(binimage, temp, sline);
	dilate(temp, dst, sline);
	//morphologyEx(binimage, dst, CV_MOP_OPEN, hline);
	//blur(dst, dst, Size(3, 3), Point(-1, -1));
	bitwise_not(dst, dst);

	imshow("水平线", dst);	
	imshow("测试", src);
	waitKey(0);
	return 0;
}

效果如下:

OpenCV学习第十三篇:提取水平和垂直线(去除干扰线)_第1张图片

你可能感兴趣的:(OpenCV学习旅程,c语言,opencv)