(三)OpenCV图像处理_05_透视校正

  1. 拍摄或者扫描图像不是规则的矩形,会对后期处理产生不好影响,需要通过透视变换校正得到正确形状。
  2. 通过二值分割 + 形态学方法 + Hough直线 +透视变换
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
RNG rng;

int main(int argc, char** argv)
{
   
	Mat src,temp_threshold,temp_mor,temp_contours,temp_hough,dst;
	src = imread("../path.jpg");
	if (src.empty())
	{
   
		cout << "could not load image1..." << endl;
		return -1;
	}

	namedWindow("src", WINDOW_AUTOSIZE);
	imshow("src", src);
	cvtColor(src, temp_threshold, COLOR_BGR2GRAY);
	//通过二值分割
	threshold(temp_threshold,temp_threshold, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
	//imshow("Threshold", temp_threshold);

	//形态学方法
	Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
	morphologyEx(temp_threshold,temp_mor,MORPH_CLOSE,kernel, Point(-1, -1), 3);
	//imshow("Morphology", temp_mor);

	//寻找轮廓
	bitwise_not(temp_mor, temp_mor, Mat());
	vector<vector<Point>> contours;
	vector<Vec4i> hireachy;
	findContours(temp_mor, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());

	//绘制轮廓
	temp_contours = Mat::zeros(src.size(), CV_8UC3);
	for (size_t i = 0; i < contours.size(); i++)
	{
   
		Rect rect = boundingRect(contours[i]);//boundingRect计算轮廓的垂直边界最小矩形,矩形是与图像上下边界平行的
		if (rect.width > (src.cols / 2) && rect.width < src.cols - 5)
		{
   
			drawContours(temp_contours, contours, static_cast<int>(i), Scalar(255,0,255), 2, 8, hireachy, 0
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值