欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV圖像幾何變換之透視變換

 更新時間:2021年03月19日 15:10:17   作者:liekkas0626  
這篇文章主要為大家詳細(xì)介紹了OpenCV圖像幾何變換之透視變換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了OpenCV圖像幾何變換之透視變換的具體代碼,供大家參考,具體內(nèi)容如下

1. 基本原理

透視變換(Perspective Transformation)的本質(zhì)是將圖像投影到一個新的視平面,其通用變換公式為:

(u,v)為原始圖像像素坐標(biāo),(x=x'/w',y=y'/w')為變換之后的圖像像素坐標(biāo)。透視變換矩陣圖解如下:

仿射變換(Affine Transformation)可以理解為透視變換的特殊形式。透視變換的數(shù)學(xué)表達(dá)式為:

所以,給定透視變換對應(yīng)的四對像素點坐標(biāo),即可求得透視變換矩陣;反之,給定透視變換矩陣,即可對圖像或像素點坐標(biāo)完成透視變換,如下圖所示:

2. OpenCV透視變換函數(shù)

Mat getPerspectiveTransform(const Point2f* src, const Point2f* dst)
// Calculate a perspective transform from four pairs of the corresponding points.
// src – Coordinates of quadrangle vertices in the source image.
// dst – Coordinates of the corresponding quadrangle vertices in the destination image.
 
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
// Apply a perspective transform to an image.
// src – Source image.
// dst – Destination image that has the size dsize and the same type as src.
// M – 3*3 transformation matrix.
// dsize – Size of the destination image.
// flags – Combination of interpolation methods and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation (dstsrc).
// borderMode – Pixel extrapolation method. When borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image that corresponds to the “outliers” in the source image are not modified by the function.
// borderValue – Value used in case of a constant border. By default, it is 0.

3. 程序

#include <iostream>
 
#include "highgui.h"
#include "opencv2/imgproc/imgproc.hpp"
 
int main()
{
 // get original image.
 cv::Mat originalImage = cv::imread("road.png");
 
 // perspective image.
 cv::Mat perspectiveImage;
 
 // perspective transform
 cv::Point2f objectivePoints[4], imagePoints[4];
 
 // original image points.
 imagePoints[0].x = 10.0; imagePoints[0].y = 457.0;
 imagePoints[1].x = 395.0; imagePoints[1].y = 291.0;
 imagePoints[2].x = 624.0; imagePoints[2].y = 291.0;
 imagePoints[3].x = 1000.0; imagePoints[3].y = 457.0;
 
 // objective points of perspective image.
 // move up the perspective image : objectivePoints.y - value .
 // move left the perspective image : objectivePoints.x - value.
 double moveValueX = 0.0;
 double moveValueY = 0.0;
 
 objectivePoints[0].x = 46.0 + moveValueX; objectivePoints[0].y = 920.0 + moveValueY;
 objectivePoints[1].x = 46.0 + moveValueX; objectivePoints[1].y = 100.0 + moveValueY;
 objectivePoints[2].x = 600.0 + moveValueX; objectivePoints[2].y = 100.0 + moveValueY;
 objectivePoints[3].x = 600.0 + moveValueX; objectivePoints[3].y = 920.0 + moveValueY;
 
 cv::Mat transform = cv::getPerspectiveTransform(objectivePoints, imagePoints);
 
 // perspective.
 cv::warpPerspective(originalImage,
   perspectiveImage,
   transform,
   cv::Size(originalImage.rows, originalImage.cols),
   cv::INTER_LINEAR | cv::WARP_INVERSE_MAP);
 
 // cv::imshow("perspective image", perspectiveImage);
 // cvWaitKey(0);
 
 cv::imwrite("perspectiveImage.png", perspectiveImage);
 
 return 0;
}

原始圖像及其透視變換結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++11時間日期庫chrono的使用

    C++11時間日期庫chrono的使用

    chrono是C++11中新加入的時間日期操作庫,可以方便地進(jìn)行時間日期操作,本文詳細(xì)的介紹了一下如何使用,感興趣的可以了解一下
    2022-01-01
  • Visual C++中MFC消息的分類

    Visual C++中MFC消息的分類

    標(biāo)準(zhǔn)(窗口)消息:窗口消息一般與窗口內(nèi)部運作有關(guān),如創(chuàng)建窗口,繪制窗口,銷毀窗口,通常,消息是從系統(tǒng)發(fā)到窗口,或從窗口發(fā)到系統(tǒng)
    2012-11-11
  • 關(guān)于C++內(nèi)存中字節(jié)對齊問題的詳細(xì)介紹

    關(guān)于C++內(nèi)存中字節(jié)對齊問題的詳細(xì)介紹

    本篇文章是對C++內(nèi)存中字節(jié)對齊的問題進(jìn)行了詳細(xì)的分析與總結(jié)。需要的朋友參考下
    2013-05-05
  • 解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法

    解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法

    這篇文章主要介紹了解析C++編程中的選擇結(jié)構(gòu)和switch語句的用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • C++ float、double判斷是否等于0問題

    C++ float、double判斷是否等于0問題

    這篇文章主要介紹了C++ float、double判斷是否等于0問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C++中sprintf使用的方法與printf的區(qū)別分析

    C++中sprintf使用的方法與printf的區(qū)別分析

    這篇文章主要介紹了C++中sprintf使用的方法與printf的區(qū)別,實例分析了sprintf與printf的具體用法及相關(guān)注意事項,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • 詳解QML?調(diào)用?C++?中的內(nèi)容

    詳解QML?調(diào)用?C++?中的內(nèi)容

    這篇文章主要介紹了QML?怎么調(diào)用?C++?中的內(nèi)容,這里主要是總結(jié)一下,怎么在 QML 文件中引用 C ++ 文件里定義的內(nèi)容,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • C語言之詳解靜態(tài)變量static

    C語言之詳解靜態(tài)變量static

    在C語言中static是用來修飾變量和函數(shù)的,這篇文章詳細(xì)介紹了static主要作用,文章中有詳細(xì)的代碼實例,需要的朋友可以參考閱讀
    2023-04-04
  • C語言鏈表案例學(xué)習(xí)之通訊錄的實現(xiàn)

    C語言鏈表案例學(xué)習(xí)之通訊錄的實現(xiàn)

    為了將所學(xué)到的鏈表的知識進(jìn)行鞏固學(xué)習(xí),做到學(xué)以致用,本文將利用鏈表制作一個簡單的通訊錄。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-10-10
  • 利用QT設(shè)計秒表功能

    利用QT設(shè)計秒表功能

    這篇文章主要為大家詳細(xì)介紹了利用QT設(shè)計秒表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論