Opencv/C++ 去除扫描图片的白色边框

问题描述:扫描得到的图像文件,存在白色的边框,需要切除。如下图所示。

解决思路:有两种方式。

(1)先通过边缘检测,再进行轮廓发现或者直线检测最大外接矩形,这个方法可以自动调整角度问题。

(2)先通过二值分割,再进行形态学操作,最后利用Hough直线检测找到最大外接矩形,这种方式不能自动的调整角度,肯呢个比较麻烦。

这里选择第一种方式。

首先考虑如果这个图片的角度正好,不需要调整的情况。

算法步骤:

(1)首先读入图片,并转换成灰度图像gray_src。

(2)对gray_src图像进行canny边缘检测,得到一张单通道边缘图片canny_out。

(3)对 canny_out图像进行轮廓检测,并得到多组轮廓信息存于vector容器中。

(4)循环检测筛选合适的轮廓,判断每个轮廓的最小外接矩阵的大小是否符合条件,符合条件的轮廓的外接矩形bbox保存下来。

(5)最后利用得到的bbox获取切边后的图像。

注意事项:canny检测的阈值,我是事先通过trackbar调出来的,大概是50左右。

代码如下:

void FindROI(int, void*) {
	//先转换成灰度图像
	cvtColor(src, gray_src, CV_BGR2GRAY);

	//Canny检测,采用是L1计算方式(false)
	Mat canny_output;
	Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);

	//定义数组存放检测到外形信息,在进行轮廓发现,这会有很多轮廓
	vector<vector<Point>>contours;
	vector<Vec4i>hireachy;
	findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	
	//定义轮廓的外接矩形最小的宽度和长度
	int minw = src.cols*0.75;
	int minh = src.rows*0.75;

	//定义随机数
	RNG rng(12345);

	//定义矩形承接合适轮廓的最小外接矩形
    Rect bbox;

	//定义模板用于把轮廓画出来
	Mat drawImage = Mat::zeros(src.size(), CV_8SC3);
	printf("%d\n", contours.size());
	for (size_t t = 0; t < contours.size(); t++) {
		//寻找每个轮廓的最小的外接矩形,这个矩形可以使多角度的。
		RotatedRect minRect = minAreaRect(contours[t]);
		
		//找到最小矩形的角度,然后打印出来
		float degree = abs(minRect.angle);
		//printf("current angle:%f\n", degree);

		//判断最小的矩形是否大于最小宽度和高度
		if(minRect.size.width > minw && minRect.size.height > minh&& minRect.size.width < (src.cols - 5)) {
			
			//提取4个角的信息
			Point2f pts[4];
			minRect.points(pts);
			bbox = minRect.boundingRect();
			Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
			//绘制4个点的连线,0,1,2,3。没有4,所以第二点对4 取模即可。
			for (int i = 0; i < 4; i++) {
				line(drawImage, pts[i], pts[(i + 1) % 4], color, 2, 8, 0);
			}
		}
	}
	imshow(output_win, drawImage);

	//显示最后的切边的成图
	if (bbox.width > 0 && bbox.height > 0) {
		Mat roiImg = src(bbox);
		imshow(roi_win, roiImg);
	}
}

 这个是滑动窗口的代码

//设置全局变量
const char* output_win = "Contours";
int max_level = 255;      //滑动条的最大值
int threshold_value =100;//阈值的初始值

//滑动窗口
createTrackbar("Threshold:", output_win, &threshold_value, max_level, FindROI);
FindROI(0,0);

最后的结果图如下:

接下来讨论下如果图片不是正放,存在角度问题的。

算法基本思想:可以想找图片的最小外接矩形,这个矩形的角度肯定不是正的。然后对该矩形进行角度的调整,最后再切边。

                         也可以对矩形框出来的ROI区域进行切边,之后再调整角度。

接下来的代码完成角度的调整。调整之后对画出来的ROI区域再用上述的切边程序完成白边的去除。

void Check_Skew(int, void*) {
	
	//转换成灰度图像
	cvtColor(src, gray_src, CV_BGR2GRAY);

	//Canny检测
	Mat canny_out;
	Canny(gray_src, canny_out, threshold_value, threshold_value * 2, 3, false);

	//轮廓发现
	vector<vector<Point>>contours;
	vector<Vec4i>hireachy;
	findContours(canny_out, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
 
     //先找出最小的外接矩形框
	Mat drawImg = Mat::zeros(src.size(), CV_8UC3);
	float maxw = 0;
	float maxh = 0;
	double degree = 0;
	for (size_t t = 0; t < contours.size(); t++) {
		RotatedRect minRect = minAreaRect(contours[t]);
		degree = abs(minRect.angle);
		if (degree > 0) {
			maxw = max(maxw, minRect.size.width);
			maxh = max(maxh, minRect.size.height);
			}
	}
	RNG rng(123456);

    //求出外接矩形框的角度
	for (size_t t = 0; t < contours.size(); t++) {
		RotatedRect minRect = minAreaRect(contours[t]);
		if (maxw == minRect.size.width&&maxh == minRect.size.height) {
			degree = minRect.angle;
			Point2f pts[4];
            minRect.points(pts);
			Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
			for (int i = 0; i < 4; i++) {
				line(drawImg, pts[i], pts[(i + 1) % 4], color, 2, 8, 0);
			}
		}
	}
	printf("max width:%f\n", maxw);
	printf("max height:%f\n", maxh);
	printf("degree:%f\n", degree);

	//寻找矩形框的旋转中心,就是整张图片的中心点作为旋转中心
	Point2f center(src.cols / 2, src.rows / 2);

    //根据中心点和旋转角度生成旋转矩阵
	Mat rotm = getRotationMatrix2D(center, degree, 1.0);

    //仿射变换,将图像的角度矫正,得到最终的结果dst
	Mat dst;
	warpAffine(src, dst, rotm, src.size(), INTER_LINEAR, 0, Scalar(255, 255, 255));
	imshow("Correct Image", dst);

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值