问题描述
拍摄或者扫描图像不是规则的矩形,会对后期处理产生不 好影响,需要通过透视变换校正得到正确形状。
解决思路
通过二值分割 + 形态学方法 + Hough直线 +透视变换

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h> using namespace cv;
using namespace std; int main(int argc, char** argv) {
Mat src = imread("D:/case6.png");
if (src.empty()) {
printf("could not load image...\n");
return ;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); // 二值处理 取反
Mat gray_src, binary, dst;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
threshold(gray_src, binary, , , THRESH_BINARY_INV | THRESH_OTSU);
//imshow("binary image", binary); // 形态学操作
Mat kernel = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -));
morphologyEx(binary, dst, MORPH_CLOSE, kernel, Point(-, -), );
//imshow("morphology", dst); // 轮廓发现
bitwise_not(dst, dst, Mat());
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(dst, contours, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point()); // 轮廓绘t制
int width = src.cols;
int height = src.rows;
Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = ; t < contours.size(); t++) {
Rect rect = boundingRect(contours[t]);
if (rect.width > width / && rect.width < width - ) {
drawContours(drawImage, contours, static_cast<int>(t), Scalar(, , ), , , hireachy, , Point());
}
}
//imshow("contours", drawImage); vector<Vec4i> lines;
Mat contoursImg;
int accu = min(width*0.5, height*0.5);
cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
HoughLinesP(contoursImg, lines, CV_HOUGH_PROBABILISTIC, CV_PI / 200.0, accu, accu, );
Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = ; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(linesImage, Point(ln[], ln[]), Point(ln[], ln[]), Scalar(, , ), , , );
}
printf("number of lines : %d\n", lines.size());
//imshow("lines image", linesImage); // 寻找与定位上下左右四条直线
int deltah = ;
Vec4i topLine, bottomLine;
Vec4i leftLine, rightLine;
for (int i = ; i < lines.size(); i++) {
Vec4i ln = lines[i];
deltah = abs(ln[] - ln[]);
if (ln[] < height / 2.0 && ln[] < height / 2.0 && deltah < accu - ) {
if (topLine[] > ln[] && topLine[] > ) {
topLine = lines[i];
}
else {
topLine = lines[i];
}
}
if (ln[] > height / 2.0 && ln[] > height / 2.0 && deltah < accu - ) {
bottomLine = lines[i];
}
if (ln[] < width / 2.0 && ln[] < width / 2.0) {
leftLine = lines[i];
}
if (ln[] > width / 2.0 && ln[] > width / 2.0) {
rightLine = lines[i];
}
}
cout << "top line : p1(x, y) = " << topLine[] << "," << topLine[] << " p2(x, y) = " << topLine[] << "," << topLine[] << endl;
cout << "bottom line : p1(x, y) = " << bottomLine[] << "," << bottomLine[] << " p2(x, y) = " << bottomLine[] << "," << bottomLine[] << endl;
cout << "left line : p1(x, y) = " << leftLine[] << "," << leftLine[] << " p2(x, y) = " << leftLine[] << "," << leftLine[] << endl;
cout << "right line : p1(x, y) = " << rightLine[] << "," << rightLine[] << " p2(x, y) = " << rightLine[] << "," << rightLine[] << endl; // 拟合四条直线方程,求直线相交的点
float k1, c1;
k1 = float(topLine[] - topLine[]) / float(topLine[] - topLine[]);
c1 = topLine[] - k1 * topLine[];
float k2, c2;
k2 = float(bottomLine[] - bottomLine[]) / float(bottomLine[] - bottomLine[]);
c2 = bottomLine[] - k2 * bottomLine[];
float k3, c3;
k3 = float(leftLine[] - leftLine[]) / float(leftLine[] - leftLine[]);
c3 = leftLine[] - k3 * leftLine[];
float k4, c4;
k4 = float(rightLine[] - rightLine[]) / float(rightLine[] - rightLine[]);
c4 = rightLine[] - k4 * rightLine[]; // 四条直线交点
Point p1; // 左上角
p1.x = static_cast<int>((c1 - c3) / (k3 - k1));
p1.y = static_cast<int>(k1*p1.x + c1);
Point p2; // 右上角
p2.x = static_cast<int>((c1 - c4) / (k4 - k1));
p2.y = static_cast<int>(k1*p2.x + c1);
Point p3; // 左下角
p3.x = static_cast<int>((c2 - c3) / (k3 - k2));
p3.y = static_cast<int>(k2*p3.x + c2);
Point p4; // 右下角
p4.x = static_cast<int>((c2 - c4) / (k4 - k2));
p4.y = static_cast<int>(k2*p4.x + c2);
cout << "p1(x, y)=" << p1.x << "," << p1.y << endl;
cout << "p2(x, y)=" << p2.x << "," << p2.y << endl;
cout << "p3(x, y)=" << p3.x << "," << p3.y << endl;
cout << "p4(x, y)=" << p4.x << "," << p4.y << endl; // 显示四个点坐标
circle(linesImage, p1, , Scalar(, , ), , , );
circle(linesImage, p2, , Scalar(, , ), , , );
circle(linesImage, p3, , Scalar(, , ), , , );
circle(linesImage, p4, , Scalar(, , ), , , );
line(linesImage, Point(topLine[], topLine[]), Point(topLine[], topLine[]), Scalar(, , ), , , );
//imshow("four corners", linesImage); // 透视变换
vector<Point2f> src_corners();
src_corners[] = p1;
src_corners[] = p2;
src_corners[] = p3;
src_corners[] = p4; vector<Point2f> dst_corners();
dst_corners[] = Point(, );
dst_corners[] = Point(width, );
dst_corners[] = Point(, height);
dst_corners[] = Point(width, height); // 获取透视变换矩阵
Mat resultImage;
Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR);
namedWindow("Final Result", CV_WINDOW_AUTOSIZE);
imshow("Final Result", resultImage); waitKey();
return ;
}

opencv实践::透视变换的更多相关文章

  1. OpenCV】透视变换 Perspective Transformation(续)

    载分 [OpenCV]透视变换 Perspective Transformation(续) 分类: [图像处理] [编程语言] 2014-05-27 09:39 2776人阅读 评论(13) 收藏 举 ...

  2. 【OpenCV】透视变换矫正

    演示结果参考: 功能实现:运行程序,会显示图片的尺寸,按回车键后,依次点击需矫正的图片的左上.右上.左下.右下角,并能显示其坐标,结果弹出矫正后的图片,如图上的PIC2对话框.可以继续选择图片四个点进 ...

  3. SVM:从理论到OpenCV实践

    (转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途)   一.理论 参考网友的博客: (1)[理论]支持向量机1: Maximum Marg ...

  4. HOG:从理论到OpenCV实践

    (转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 1.HOG特征描述子的定义:     locally normalised ...

  5. 【opencv实践】边缘检测

    边缘检测: 一.canny算子 Canny边缘检测根据对信噪比与定位乘积进行测度,得到最优化逼近算子,也就是Canny算子.类似与 LoG 边缘检测方法,也属于先平滑后求导数的方法. 二.canny算 ...

  6. OpenCV实践之路——人脸检测(C++/Python) 【转】

    转自:http://blog.csdn.net/xingchenbingbuyu/article/details/51105159 版权声明:本文为博主原创文章,转载请联系作者取得授权. 本文由@星沉 ...

  7. OpenCV实践之路——Python的安装和使用

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/50936076 微博:http ...

  8. opencv实践::对象提取与测量

    问题描述 照片是来自太空望远镜的星云图像,科学家想知道它的面 积与周长. 解决思路 方法一: 通过二值分割+图像形态学+轮廓提取 #include <opencv2/opencv.hpp> ...

  9. opencv实践::对象计数

    问题描述 真实案例,农业领域经常需要计算对象个数 或者在其它领域拍照自动计数,可以提供效率,减低成本 解决思路 通过二值分割+形态学处理+距离变换+连通区域计算 #include <opencv ...

随机推荐

  1. iOS面试题整理带答案

    iOS面试题整理带答案       找工作,面试是避免不了的! 而技术开发面试,问一些技术相关的问题是必须的,最新的技术可能人人都趋之若鹜,但有些原理和基础的也希望都有了解. 这里整理了一些iOS相关 ...

  2. Java开发者薪资最低?程序员只能干到30岁?国外真的没有996?Intellij真的比Eclipse受欢迎?

    Stack Overflow作为全球最大的程序设计领域的问答网站,每年都会出据一份开发者调查报告.近日,Stack Overflow公布了其第9次年度开发者调查报告(https://insights. ...

  3. Mysql - 关于relay_log_recovery参数的测试

    一.概述 官方文档中对relay_log_recovery参数的解释 Enables automatic relay log recovery immediately following server ...

  4. java数据结构——数组(Array)

    数据结构+算法是我们学习道路上的重中之重,让我们一起进步,一起感受代码之美! /** * 让我们从最基本的数据结构——数组开始吧 * 增.删.改.查.插.显示 */ public class Seql ...

  5. 【呕心总结】python如何与mysql实现交互及常用sql语句

    9 月初,我对 python 爬虫 燃起兴趣,但爬取到的数据多通道实时同步读写用文件并不方便,于是开始用起mysql.这篇笔记,我将整理近一个月的实战中最常用到的 mysql 语句,同时也将涉及到如何 ...

  6. 用Python构造ARP请求、扫描、欺骗

    目录 0. ARP介绍 1. Scapy简述 2. Scapy简单演示 2.1 安装 2.2 构造包演示 2.2.1 进入kamene交互界面 2.2.2 查看以太网头部 2.2.3 查看 ICMP ...

  7. Docker系列(二):通过Docker安装使用 Kubernetes (K8s)

    Docker社区版从17.12版本开始已经提供了对Kubernetes的支持.但是由于其安装过程依赖的镜像服务在国内访问很不稳定,很多朋友都无法配置成功.我们提供了一个简单的工具帮助大家开启Docke ...

  8. css 添加手状样式,鼠标移上去变小手

    cursor:pointer, 简单实用. 前端工作一年多,竟然没有博客.说出来别人都要笑话,这是一个新的开始.

  9. ELK 学习笔记之 elasticsearch bool组合查询

    elasticsearch bool组合查询: 相当于sql:where _type = 'books' and (price = 500 or title = 'bigdata') Note: mu ...

  10. Kafka 学习笔记之 Producer/Consumer (Scala)

    既然Kafka使用Scala写的,最近也在慢慢学习Scala的语法,虽然还比较生疏,但是还是想尝试下用Scala实现Producer和Consumer,并且用HashPartitioner实现消息根据 ...