去除直线

#include<iostream>
#include<opencv.hpp>
#include<io.h>
using namespace cv;
using namespace std;

void removeLine(string srcPath,string saveName)
{
	Mat srcImage = imread(srcPath, 0);
	Mat dst = Mat::zeros(Size(srcImage.cols, srcImage.rows), CV_8UC1);
	Mat binary = Mat(Size(srcImage.cols, srcImage.rows), CV_8UC1);
	threshold(srcImage, binary, 150, 255, THRESH_BINARY);
	vector<Vec4i> lines;
	HoughLinesP(~binary, lines, 1, CV_PI / 180, 80, 200, 30);
	int maxDistance = 0;
	Point start;
	Point end;
	Point maxStart;
	Point maxEnd;
	for (size_t i = 0; i < lines.size(); i++)
	{
		Vec4i l = lines[i];
		start.x = l[0];
		start.y = l[1];
		end.x = l[2];
		end.y = l[3];
		if (abs(end.x - start.y)>maxDistance)
		{
			maxDistance = end.x - start.y;
			maxStart = start;
			maxEnd = end;
		}
	}	
	line(dst, maxStart, maxEnd, Scalar(255), 1);//, CV_AA
	for (int i = 0; i < dst.rows; i++)
	{
		for (int j = 0; j < dst.cols; j++)
		{
			if (static_cast<int>(dst.at<uchar>(i, j)) == 255)
			{
				//cout << "111" << endl;
				int up = 0, down = 0;
				int white = 0;
				for (int m = i; m >= 0; m--)
				{
					if ((int)binary.at<uchar>(m, j) == 0)
					{
						//	cout << "222" << endl;
						up++;
						white = 0;
					}
					else white++;
					if (white > 2) break;
				}
				white = 0;
				for (int n = i; n < dst.rows; n++)
				{
					if ((int)binary.at<uchar>(n, j) == 0)
					{
						//	cout << "333" << endl;
						down++;
						white = 0;
					}
					else white++;
					if (white > 2) break;
				}
				//	cout << down + up << endl;
				if (down + up<8)
				{
					//cout << "444" << endl;
					for (int h = -up; h <= down; h++)
						if (h + i < 0)
						{
							binary.at<uchar>(0, j) = 255;
						}
						else
						{
							binary.at<uchar>(h + i, j) = 255;
						}		
				}
			}
		}
	}
	imwrite(saveName, binary);
}

void removeDot_1(string srcPath, string saveName)
{
	//removeDot("D:/Image/Hiragana1/ATH_20190104-01005_TIF_1.TIF","2.png");
	Mat srcImage = imread(srcPath, 0);
	//imshow("srcImage", srcImage);
	//waitKey(0);
	int Area1 = 15;
	Mat out = Mat::zeros(Size(srcImage.cols, srcImage.rows), srcImage.type());
	std::vector<std::vector<cv::Point>> contours;
	Mat horizontal = ~srcImage.clone();
	Mat dst1 = ~srcImage;
	//imshow("dst1", dst1);
	//waitKey(0);
	//去除虚线
	cv::findContours(dst1, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
	contours.erase(std::remove_if(contours.begin(), contours.end(),
		[Area1](const std::vector<cv::Point>& c) {return cv::contourArea(c) < Area1; }), contours.end());
	cv::drawContours(out, contours, -1, cv::Scalar(255), CV_FILLED);
	/*imshow("out", ~out);
	waitKey(0);*/

	/*去除直线*/
	int scale = 20; //这个值越大,检测到的直线越多

	int horizontalsize = horizontal.cols / scale;

	Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize, 1));
	erode(horizontal, horizontal, horizontalStructure, Point(-1, -1));
	dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1));
	//blur(horizontal,horizontal,Size(5,5));
	//dilate(horizontal, horizontal, element1);
	/*imshow("horizontal", horizontal);
	waitKey(0);*/
	std::vector<std::vector<cv::Point>> contours1;
	cv::findContours(horizontal, contours1, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
	cv::drawContours(out, contours1, -1, cv::Scalar(0), CV_FILLED);
	imwrite(saveName,~out);
	//imshow("final", ~out);
	//waitKey(0);
}

void getAllFiles(string path, vector<string>& files)
{
	//文件句柄  
	__int64 hFile = 0;
	//文件信息  
	struct __finddata64_t  fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	if ((hFile = _findfirst64(p.assign(path).append("/*.TIF").c_str(), &fileinfo)) == -1)
	{
		cout << "No file is found\n" << endl;
	}
	else
	{
		do
		{
			files.push_back(p.assign(path).append("/").append(fileinfo.name));
		} while (_findnext64(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}
}
#if 1
int main()
{
	vector<string> files;
	getAllFiles("D:/Image/Hiragana1/", files);
	char str[30];
	int size = files.size();
	for (int i = 0; i < size; i++)
	{
		cout << files[i] << endl;
		int pos1 = files[i].find_last_of('/');
		int pos2 = files[i].find('.');
		string name2 = files[i].substr(pos1 + 1, pos2 - pos1 - 1);
		string save = "D:/Image/Hiragana1_result/" + name2 + ".TIF";
	   // removeDot(files[i].c_str(),save.c_str());
		removeLine(files[i].c_str(), save.c_str());
	}
	return 0;
}
#endif 




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值