OpenCV利用透视变换将斜体校正

	cv::Mat src = cv::imread("D:\\sxl\\处理图片\\斜体分割\\测试图\\正2.png");
	if (!src.data)
		return;
	//--------------旋转------------------------------------------------------
	double angle = 19;
	Point2f center(src.cols / 2, src.rows / 2);
	Mat rot = getRotationMatrix2D(center, angle, 1);
	Rect bbox = RotatedRect(center, src.size(), angle).boundingRect();

	rot.at<double>(0, 2) += bbox.width / 2.0 - center.x;
	rot.at<double>(1, 2) += bbox.height / 2.0 - center.y;

	//设置选择背景边界颜色:绿色 
	Scalar borderColor = Scalar(255, 255, 255);
	Mat dst;
	warpAffine(src, dst, rot, bbox.size(), INTER_LINEAR, BORDER_CONSTANT, borderColor);

	Mat gray, BinImg;
	if (dst.channels() == 3)
	{
		cvtColor(dst, gray, CV_BGR2GRAY);   //转换成灰度图
	}
	else
	{
		gray = dst;
	}
	//--------------旋转------------------------------------------------------


	vector<Point> not_a_rect_shape;
	not_a_rect_shape.push_back(Point(3, 82));
	not_a_rect_shape.push_back(Point(2, 276));
	not_a_rect_shape.push_back(Point(234, 191));
	not_a_rect_shape.push_back(Point(239, 1));
	// For debugging purposes, draw green lines connecting those points
	// and save it on disk
	const Point* point = &not_a_rect_shape[0];
	int n = (int)not_a_rect_shape.size();
	Mat draw = gray.clone();
	polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, CV_AA);
	imwrite("draw.jpg", draw);
	//  topLeft, topRight, bottomRight, bottomLeft
	cv::Point2f src_vertices[4];
	src_vertices[0] = not_a_rect_shape[0];
	src_vertices[1] = not_a_rect_shape[1];
	src_vertices[2] = not_a_rect_shape[2];
	src_vertices[3] = not_a_rect_shape[3];

	Point2f dst_vertices[4];
	dst_vertices[0] = Point(2, 2);
	dst_vertices[1] = Point(0, 194);
	dst_vertices[2] = Point(235, 190);
	dst_vertices[3] = Point(239, 1);
	Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
	cv::Mat rotated;
	warpPerspective(gray, rotated, warpMatrix, rotated.size(), INTER_LINEAR, BORDER_CONSTANT);
	// Display the image
	cv::namedWindow("Original Image");
	cv::imshow("Original Image", src);
	cv::namedWindow("warp perspective");
	cv::imshow("warp perspective", rotated);
	imwrite("result.jpg", src);
	cv::waitKey();
	return ;

效果图:
这里写图片描述
这里写图片描述
这里写图片描述

欢迎扫码关注我的微信公众号

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值