问题描述
拍摄或者扫描图像不是规则的矩形,会对后期处理产生不 好影响,需要通过透视变换校正得到正确形状。
解决思路
通过二值分割 + 形态学方法 + 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. DNA sequence(映射+BFS)

    Problem Description The twenty-first century is a biology-technology developing century. We know tha ...

  2. 安卓APP开发简单实例 结对编程心得

    开始说起搞APP开发,自己和小伙伴的编程水平真的很低,无从下手,只有在网上找点案列,学习着怎样开发,结对编程还是面临着许多问题的,大家的水平有所差异和编程风格不同,我们用eclipse做了一个仿微信登 ...

  3. 基于LeNet的手写汉字识别(caffe)

    我假设已经成功编译caffe,如果没有,请参考http://caffe.berkeleyvision.org/installation.html 在本教程中,我假设你的caffe安装目录是CAFFE_ ...

  4. charles Web界面设置

    本文参考:charles Web界面设置 Web Inerface Web界面可以让您使用Web浏览器控制查询,您可以访问 http://control.charles 的Web界面,当查询运行时,您 ...

  5. spring中基于注解使用ehcache

    继续上篇,这篇介绍服务层缓存,基于注解的方式使用ehcache 注解的标签主要有4个:@Cacheable.@CacheEvict.@CachePut.@Caching,他们的用法是: @Cachea ...

  6. [翻译] ASP.NET Core 3.0 的新增功能

    ASP.NET Core 3.0 的新增功能 全文翻译自微软官方文档英文版 What's new in ASP.NET Core 3.0 本文重点介绍了 ASP.NET Core 3.0 中最重要的更 ...

  7. 【linux】【jenkins】自动化运维六 构建生成备份

    push tag用于提交代码构建成功后push tag,以防提交代码报错,方便回滚之前正常的代码. 由于采用docker部署的形式,构建失败自动回滚还未实现,待研究解决. 构建后操作选择 Git Pu ...

  8. Spring 梳理 - 开启并配置 Spring MVC 的方法

    传统web.xm中配置两个上下文+两个context对应的xml+两个上下文bean分别手动配置 传统web.xm中配置两个上下文+两个context对应的xml+<mvc:annotation ...

  9. .net 更新access数据库 影响的行数为0

    在更新Access数据库的时候,明明传入的数据及参数类型都正确,但是一直更新不了,查看影响的行数一直为0 此原因为 C#操作Access数据库的时候更新的参数和条件参数要按照更新语句中的顺序进行设置, ...

  10. jquery 全选,反选

    <?php foreach ($contents as $item) { ?> <tr> <td><input name="qx" val ...