Opencv 提取水平 垂直线,去除杂线,提取对象

Opencv 提取水平 垂直线,去除杂线,提取对象_第1张图片

#include
#include

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
	Mat src = imread("截图3.jpg");
	if (src.empty())
	{
		return -1;
	}
	String strInput = "input image";
	namedWindow(strInput, CV_WINDOW_AUTOSIZE);
	imshow(strInput, src);
	Mat dst;
	cvtColor(src, dst, CV_BGR2GRAY);//转灰度
	imshow("output grap image", dst);
	Mat binimg;
	adaptiveThreshold(~dst, binimg, 255, ADAPTIVE_THRESH_MEAN_C, ADAPTIVE_THRESH_MEAN_C, 15, -2);//转二值
	imshow("binary image", binimg);
	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 kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));//去除杂线 提取对象
	Mat tmp;
	//erode(binimg, tmp, vLine);
	//dilate(tmp, dst, vLine);
	morphologyEx(binimg, dst, CV_MOP_OPEN,hLine);
	bitwise_not(dst, dst);//取反
	blur(dst, dst, Size(3, 3), Point(-1, -1));
	imshow("Final image", dst);

	waitKey(0);
	return 0;
}

 

你可能感兴趣的:(Opencv)