2019独角兽企业重金招聘Python工程师标准>>>

1、下载Visual Studio2013 :http://pan.baidu.com/s/1qYmAZJe ,直接解压安装;

2、下载openCV: https://sourceforge.net/projects/opencvlibrary/files/ ;建议下载3.0版本的,3.1以后的版本里面不再有x86,但是vs2013中不支持V14的编译器,所以需要注意vs和opencv的版本对应,负责容易出现未知问题; 此处请参考: http://blog.csdn.net/poem_qianmo/article/details/19809337

3、配置vs: http://blog.csdn.net/u013105549/article/details/50493069

4、注意: 引用照片时请注意照片路径: 问题基本是图片路径的问题。应将程序相应的图像放置在工程目录下(和cpp源文件同一目录下)。: http://blog.csdn.net/ture_dream/article/details/52600897

4、截取四边形原理: http://www.cnblogs.com/frombeijingwithlove/p/4226489.html

http://vitrum.github.io/2015/07/28/Opencv-%E5%BA%94%E7%94%A8%EF%BC%8C%E5%9B%BE%E7%89%87%E9%87%8C%E5%8F%96%E5%87%BA%E5%9B%9B%E8%BE%B9%E5%BD%A2/

5、截取的具体代码:

//
//  opencv.cpp
//  功能: 图片里取出四边形
//  环境搭建:
//  参考地址:http://vitrum.github.io/2015/07/28/Opencv-%E5%BA%94%E7%94%A8%EF%BC%8C%E5%9B%BE%E7%89%87%E9%87%8C%E5%8F%96%E5%87%BA%E5%9B%9B%E8%BE%B9%E5%BD%A2/
//
//#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>using namespace cv;
using namespace std;/**
* 边缘检测
* @param gray - grayscale input image
* @param canny - output edge image
*/
void getCanny(Mat gray, Mat &canny) {Mat thres;double high_thres = threshold(gray, thres, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU), low_thres = high_thres * 0.5;Canny(gray, canny, low_thres, high_thres);
}struct Line {Point _p1;Point _p2;Point _center;Line(Point p1, Point p2) {_p1 = p1;_p2 = p2;_center = Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);}
};bool cmp_y(const Line &p1, const Line &p2) {return p1._center.y < p2._center.y;
}bool cmp_x(const Line &p1, const Line &p2) {return p1._center.x < p2._center.x;
}/**
* Compute intersect point of two lines l1 and l2
* @param l1
* @param l2
* @return Intersect Point
*/
Point2f computeIntersect(Line l1, Line l2) {int x1 = l1._p1.x, x2 = l1._p2.x, y1 = l1._p1.y, y2 = l1._p2.y;int x3 = l2._p1.x, x4 = l2._p2.x, y3 = l2._p1.y, y4 = l2._p2.y;if (float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)) {Point2f pt;pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;return pt;}return Point2f(-1, -1);
}void scan(String file, bool debug = false) {// 读入原图Mat img = imread(file);Mat img_proc;int w = img.size().width, h = img.size().height, min_w = 200;// 计算缩放比例double scale = min(10.0, w * 1.0 / min_w);int w_proc = w * 1.0 / scale, h_proc = h * 1.0 / scale;// 缩小图片分辨率,提高计算速度resize(img, img_proc, Size(w_proc, h_proc));Mat img_dis = img_proc.clone();/*获取纸的四个边*/Mat gray, canny;cvtColor(img_proc, gray, CV_BGR2GRAY);// 用canny算子进行边缘检测getCanny(gray, canny);// w_proc 就是缩小后的画面宽度,20是把间距20以内的线段延长拼接为一条直线vector<Vec4i> lines;vector<Line> horizontals, verticals;HoughLinesP(canny, lines, 1, CV_PI / 180, w_proc / 3, w_proc / 3, 20);for (size_t i = 0; i < lines.size(); i++) {Vec4i v = lines[i];double delta_x = v[0] - v[2], delta_y = v[1] - v[3];Line l(Point(v[0], v[1]), Point(v[2], v[3]));// get horizontal lines and vertical lines respectivelyif (fabs(delta_x) > fabs(delta_y)) {horizontals.push_back(l);}else {verticals.push_back(l);}// for visualization onlyif (debug)line(img_proc, Point(v[0], v[1]), Point(v[2], v[3]), Scalar(0, 0, 255), 1, CV_AA);}// 边缘情况下,当没有足够的线检测if (horizontals.size() < 2) {if (horizontals.size() == 0 || horizontals[0]._center.y > h_proc / 2) {horizontals.push_back(Line(Point(0, 0), Point(w_proc - 1, 0)));}if (horizontals.size() == 0 || horizontals[0]._center.y <= h_proc / 2) {horizontals.push_back(Line(Point(0, h_proc - 1), Point(w_proc - 1, h_proc - 1)));}}if (verticals.size() < 2) {if (verticals.size() == 0 || verticals[0]._center.x > w_proc / 2) {verticals.push_back(Line(Point(0, 0), Point(0, h_proc - 1)));}if (verticals.size() == 0 || verticals[0]._center.x <= w_proc / 2) {verticals.push_back(Line(Point(w_proc - 1, 0), Point(w_proc - 1, h_proc - 1)));}}// 按中心点排序sort(horizontals.begin(), horizontals.end(), cmp_y);sort(verticals.begin(), verticals.end(), cmp_x);// for visualization onlyif (debug) {line(img_proc, horizontals[0]._p1, horizontals[0]._p2, Scalar(0, 255, 0), 2, CV_AA);line(img_proc, horizontals[horizontals.size() - 1]._p1, horizontals[horizontals.size() - 1]._p2, Scalar(0, 255, 0), 2, CV_AA);line(img_proc, verticals[0]._p1, verticals[0]._p2, Scalar(255, 0, 0), 2, CV_AA);line(img_proc, verticals[verticals.size() - 1]._p1, verticals[verticals.size() - 1]._p2, Scalar(255, 0, 0), 2, CV_AA);}/* 透视变换 */// define the destination image size: A4 - 200 PPIint w_a4 = 1654, h_a4 = 2339;//int w_a4 = 595, h_a4 = 842;Mat dst = Mat::zeros(h_a4, w_a4, CV_8UC3);// 求四顶点坐标 corners of destination image with the sequence [tl, tr, bl, br]vector<Point2f> dst_pts, img_pts;dst_pts.push_back(Point(0, 0));dst_pts.push_back(Point(w_a4 - 1, 0));dst_pts.push_back(Point(0, h_a4 - 1));dst_pts.push_back(Point(w_a4 - 1, h_a4 - 1));// corners of source image with the sequence [tl, tr, bl, br]img_pts.push_back(computeIntersect(horizontals[0], verticals[0]));img_pts.push_back(computeIntersect(horizontals[0], verticals[verticals.size() - 1]));img_pts.push_back(computeIntersect(horizontals[horizontals.size() - 1], verticals[0]));img_pts.push_back(computeIntersect(horizontals[horizontals.size() - 1], verticals[verticals.size() - 1]));// 转换成原始图像比例尺for (size_t i = 0; i < img_pts.size(); i++) {// for visualization onlyif (debug) {circle(img_proc, img_pts[i], 10, Scalar(255, 255, 0), 3);}img_pts[i].x *= scale;img_pts[i].y *= scale;}// 得到的变换矩阵 用getPerspectiveTransform计算转化矩阵,再用warpPerspective调用转化矩阵进行拉伸Mat transmtx = getPerspectiveTransform(img_pts, dst_pts);// 应用透视变换warpPerspective(img, dst, transmtx, dst.size());// 保存照片到本地imwrite("dst.jpg", dst);// for visualization onlyif (debug) {namedWindow("dst", CV_WINDOW_KEEPRATIO);imshow("src", img_dis);imshow("canny", canny);imshow("img_proc", img_proc);imshow("dst", dst);waitKey(0);}
}int main(int argc, char** argv) {string img_path[] = { "6.jpg", "images/doc2.jpg", "images/doc3.jpg" };scan(img_path[0]);return 0;
}

效果:

原图:

识别结果:

转载于:https://my.oschina.net/freelili/blog/887879

用openCV取出图片中的四边形相关推荐

  1. Python,OpenCV提取图片中的多个茄子种子轮廓,并按从左到右排序后显示

    Python,OpenCV提取图片中的多个茄子种子轮廓,并按从左到右排序后显示 1. 效果图 2. 源码 写这篇博客源于博友的提问,期望把下图中的多个茄子种子按从左到右的顺序提取出来: 1. 效果图 ...

  2. opencv去除图片中某一颜色(python实现)

    opencv去除图片中某一颜色(python实现) 一 打开图片 ​ 打开图片的时候最好使用windows自带的"画图"软件查看(在画图软件下通过句柄精灵获得点的RGB值与open ...

  3. OpenCV 识别图片中的米粒个数,并计算米粒的平均面积和长度(转)

    介绍 OpenCV+Python 使用OpenCV构建图像识别算法,识别图片中的米粒个数,并计算米粒的平均面积和长度 软件架构 模块:OpenCV 4.0.0.21 编程语言:Python 3.7.2 ...

  4. OpenCV 识别图片中的米粒个数,并计算米粒的平均面积和长度

    介绍 OpenCV+Python 使用OpenCV构建图像识别算法,识别图片中的米粒个数,并计算米粒的平均面积和长度 软件架构 模块:OpenCV 4.0.0.21 编程语言:Python 3.7.2 ...

  5. 使用opencv去除图片中的水印代码

    很抱歉,因为涉及代码编写,我无法通过文字详细的呈现.但是我可以给出一些概述性的信息. 使用OpenCV去除图片中的水印通常需要使用图像处理技术,如图像的二值化,膨胀和腐蚀,形态学处理等. 步骤: 读取 ...

  6. python更改图片中物体的颜色_Python Opencv提取图片中某种颜色组成的图形的方法...

    Python Opencv提取图片中某种颜色组成的图形的方法 主要目标识别图中红色的裂缝,尝试了几种不同的方法,最后发现比较每一点的RGB差值可以很好的解决这个问题,也就是提取图片中的红色相关信息.处 ...

  7. opencv实现图片中文字识别并切割

    背景: 为了提升用户欣赏书法图片的体验,需要从高清TIF图片中把每个字都切割出来,手动切割太麻烦,所以利用opencv自动识别图片中的文字,并将每个文字切割保存. 实现代码: import cv2 i ...

  8. opencv 阈值分割_用 OpenCV 去除图片中的水印,骚操作!

    点击上方 小张Python,加为星标 第一时间收到 Python 技术干货! 参考连接:https://stackoverflow.com/questions/32125281/removing-wa ...

  9. 利用OpenCV从图片中提取矩形并标注坐标(室内平面地图)——(一)

    某城市会展中心室内地图 背景 一名室内设计师的日常工作从设计一张会展地图开始.常常有这样的场景:划分除规范的展位后,进入销售阶段,频繁的需要修改这张地图,如展示拆分.合并.换位置.标记已交易. 问题 ...

最新文章

  1. python点操作符语法_最基础的python语法
  2. Swagger扩展为你添油加气
  3. Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】
  4. 应急管理部等四部门联合督导 烟花爆竹安全生产工作
  5. VS2010 修改全局 include目录
  6. python 保存图片_python 输出文档到world
  7. 【乌拉喵.教程】TestBench仿真给输出脚赋值引起的问题
  8. 在我心目中的霸气海贼王——路飞 不一样的路飞
  9. Aspose.Words使用模板导出数据库中图片内容
  10. 北森{“message“:“un-authorized“}
  11. 从血红细胞衰老看中老年疑难病和亚健康--
  12. 阿里云高主频通用型hfg7云服务器磁盘I/O性能表详解
  13. 同花顺模拟炒股软件 v8.40.29 官方版
  14. 侯捷-C++面向对象高级开发(操作符重载与临时对象)
  15. matlab结构体与元胞,元胞数组与结构体数组
  16. c语言笔试程序改错题,C语言笔试--程序改错题.doc
  17. 【资料分享】迪文屏使用经验分享
  18. 网络计算机应急处理,国家计算机网络应急技术处理协调中心-计算机网络安全应急处理.ppt...
  19. 电压型逆变器和电流型逆变器的不同
  20. 2.4 人工智能项目开发与验收

热门文章

  1. con 元器件符号_PROTEUS中元器件符号
  2. 混合正弦余弦算法和Lévy飞行的麻雀算法
  3. FLINK 窗口实现原理
  4. 互联网家谱受到追捧,传统修谱方式面临淘汰,数字家谱:好用
  5. core dumped调试Segmentation fault
  6. 中文姓名按照拼音排序-python
  7. 使用wx原生方法扫描获取SN码
  8. 银行招考计算机专业考什么,银行笔试一般都考什么?
  9. 解决win10设备管理器及操作找不到蓝牙
  10. Light OJ 1197