warpperspective 透视变化的opencv2.0实现

1st-------2nd |         | |         | |         |3rd-------4th
原始代码
             cv::Mat src= cv::imread( "c://03p.jpg",0);
                 if (!src.data)
                                 return 0;
                 vector<Point> not_a_rect_shape;
                not_a_rect_shape.push_back(Point(122,0));
                not_a_rect_shape.push_back(Point(814,0));
                not_a_rect_shape.push_back(Point(22,540));
                not_a_rect_shape.push_back(Point(910,540));
                 // 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 = src.clone();
                polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, CV_AA);
                imwrite( "draw.jpg", draw);
                 // Assemble a rotated rectangle out of that info
                RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape));
                std::cout << "Rotated box set to (" << box.boundingRect().x << "," << box.boundingRect().y << ") " << box.size.width << "x" << box.size.height << std::endl;
                 // Does the order of the points matter? I assume they do NOT.
                 // But if it does, is there an easy way to identify and order
                 // them as topLeft, topRight, bottomRight, bottomLeft?
                 //(0,0) (960,0) (0,540) (960,540)
                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(0, 0);
                dst_vertices[1] = Point(960,0);
                dst_vertices[2] = Point(0,540);
                dst_vertices[3] = Point(960,540);
                Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
                cv::Mat rotated;
                warpPerspective(src, 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( "03p.jpg",src);
                cv::waitKey();
                 return 0;
结果基本是出来了,下一步就是要将其和现有的代码结合起来。

=====================================================2020年4月23日20:25:54===========================================添加Python版本的实现=======================
import cv2
import numpy as np

src = cv2.imread("e:/template/steel03.jpg")
rows,cols,ch = src.shape
pts1 = np.float32([[122,0],[814,0],[22,540],[910,540]])
pts2 = np.float32([[0,0],[960,0],[0,540],[960,540]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(src,M,(cols,rows))

cv2.imshow("src",dst)

cv2.waitKey(0)

warpperspective 透视变化的opencv实现的更多相关文章

  1. opencv学习--透视变化

    透视变换和仿射变换具有很大的相同特性,前面提到了放射变化,这里再次把它拿出和透视变换进行比较 #include"cv.h" #include"highgui.h" ...

  2. python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接

    最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...

  3. 机器学习进阶-案例实战-答题卡识别判 1.cv2.getPerspectiveTransform(获得投射变化后的H矩阵) 2.cv2.warpPerspective(H获得变化后的图像) 3.cv2.approxPolyDP(近似轮廓) 4.cv2.threshold(二值变化) 7.cv2.countNonezeros(非零像素点个数)6.cv2.bitwise_and(与判断)

    1.H = cv2.getPerspectiveTransform(rect, transform_axes) 获得投射变化后的H矩阵 参数说明:rect表示原始的位置左上,右上,右下,左下, tra ...

  4. (GO_GTD_2)基于OpenCV和QT,建立Android图像处理程序

    一.综述     如何采集图片?在windows环境下,我们可以使用dshow,在linux下,也有ffmpeg等基础类库,再不济,opencv自带的videocapture也是提供了基础的支撑.那么 ...

  5. opencv车道线检测

    opencv车道线检测 完成的功能 图像裁剪:通过设定图像ROI区域,拷贝图像获得裁剪图像 反透视变换:用的是老师给的视频,没有对应的变换矩阵.所以建立二维坐标,通过四点映射的方法计算矩阵,进行反透视 ...

  6. opencv透视变换GetPerspectiveTransform的总结

    对于透视变换,必须为map_matrix分配一个3x3数组,除了3x3矩阵和三个控点变为四个控点外,透视变化在其他方面与仿射变换完全类似.具体可以参考:点击打开链接 主要用到两个函数WarpPersp ...

  7. OpenCV中对图像进行二值化的关键函数——cvThreshold()。

    函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvThreshold( const CvArr* src, CvArr* dst, double threshold, doub ...

  8. VS2010+Opencv+SIFT以及出现的问题-关于代码sift_3_c的说明

    http://blog.sina.com.cn/s/blog_a6b913e30101dvrt.html 一.前提 安装Opencv,因该版本的SIFT是基于Opencv的. 下载SIFT源码,见Ro ...

  9. opencv笔记6:角点检测

    time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...

随机推荐

  1. 如何在 网站页面中插入ppt/pdf 文件,使用插件,Native pdf 支持,chrome,Edge,Firefox,

    1 经过测试:在网页中插入 ppt 不好使:可能是浏览器=>同源策略 error?             pdf 可以正常使用:   <前提:一定要放在服务器端才行!> 2 经过e ...

  2. HTML script tag type all in one

    HTML script tag type all in one script type https://developer.mozilla.org/en-US/docs/Web/HTML/Elemen ...

  3. js & void & undefined & null

    js & void & undefined & null The void operator evaluates the given expression and then r ...

  4. autocode & API

    autocode & API https://autocode.com/app/ https://autocode.com/lib/ api-service https://dashboard ...

  5. C++ 0LL

    C++ 0LL C plus plus L / l means long LL === long long int countDigitOne(int n) { int countr = 0; for ...

  6. Redux Middleware All in One

    Redux Middleware All in One https://redux.js.org/advanced/middleware https://redux.js.org/api/applym ...

  7. js & Input & Paste & Clipboard & upload & Image

    js & Input & Paste & Clipboard & upload & Image input paste upload image js Clip ...

  8. AST & js interpreter

    AST & js interpreter 抽象语法树 & Javascript 解析器 https://astexplorer.net/ https://esprima.org/dem ...

  9. React In Depth

    React In Depth React Component Lifecycle https://reactjs.org/docs/react-component.html https://react ...

  10. PAUL ADAMS ARCHITECT LTD:拜登上台后,美国房地产走势如何?

    受全球经济危机影响,美国经济的复苏之路并不平坦,然而住宅房地产市场却是少数的例外,表现得非常亮眼.而随着美国拜登总统上台的临近,美国房产市场还会持续当前的发展吗?对于这个问题,近日,美国知名房地产公司 ...