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. ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

    #1235 : New Teaching Buildings 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 Thanks to the generous finance ...

  2. ReactDOM API All In One

    ReactDOM API All In One React DOM API render() hydrate() unmountComponentAtNode() findDOMNode() crea ...

  3. WebIDE All In One

    WebIDE All In One web IDE Visual Studio Code vscode Code editing Redefined. Free. Built on open sour ...

  4. 如何用 js 实现一个 new 函数

    如何用 js 实现一个 new 函数 原理 new 关键字实现经过了如下过程 创建一个空对象 obj = {} 链接到原型 obj.proto = constructor.prototype 绑定 t ...

  5. css break-inside

    css break-inside The break-inside CSS property sets how page, column, or region breaks should behave ...

  6. Angular Routing

    Angular Routing v9.0.7 https://angular.io/start/start-routing

  7. Flutter 将TextField平滑过渡到Text

    import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...

  8. Flutter: AnimatedList 一个滚动容器,可在插入或移除项目时为其设置动画

    Flutter Widget of the Week import 'dart:math'; import 'package:flutter/material.dart'; void main() = ...

  9. 2021-2-19:请问你知道 Java 如何高性能操作文件么?

    一般高性能的涉及到存储框架,例如 RocketMQ,Kafka 这种消息队列,存储日志的时候,都是通过 Java File MMAP 实现的,那么什么是 Java File MMAP 呢? 什么是 J ...

  10. Python爬虫系统化学习(3)

    一般来说当我们爬取网页的整个源代码后,是需要对网页进行解析的. 正常的解析方法有三种 ①:正则匹配解析 ②:BeatuifulSoup解析 ③:lxml解析 正则匹配解析: 在之前的学习中,我们学习过 ...