百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

[OpenCV实战]31 使用OpenCV将一个三角形仿射变换到另一个三角形

bigegpt 2024-08-28 12:08 4 浏览

在本文中,我们会看到如何将一个三角形仿射变换到另一个三角形。在图形学的研究中,研究者常常进行三角形之间的变换操作,因为任意的3D表面都可以用多个三角形去近似表示。同样的,图像也可以分解成多个三角形来表示。但是在OpenCV中并没有直接将三角形仿射变换成另一个三角形的函数。本教程将逐步说明如何将下图中左图中的三角形转换为右图。

在我们深入研究代码之前,我们需要了解仿射变换是什么。

1 什么是仿射变换?

仿射变换是将一个3个点的点集(即三角形)来转换到任意3个点另一点集的最简单的方法。它包含平移(移动),缩放,旋转和裁剪等操作。下图说明了如何使用仿射变换来改变正方形的形状。请注意,使用仿射变换,您可以在任何方向和比例下将正方形的形状更改为平行四边形。然而,仿射变换不够灵活,无法将方形变换为任意四边形。换句话说,在仿射变换之后,平行线继续平行。

在OpenCV中,仿射变换可以用一个2×3的矩阵表示,这个矩阵的前两列表示旋转、缩放、裁剪操作,最后一列表示平移操作。如下公式所示:

给定一点

,上面的仿射变换使用下面给出的等式得到点

2 使用OpenCV进行三角形仿射变换

我们现在知道,要将三角形变形到另一个三角形,我们将需要使用仿射变换。在OpenCV中,warpAffine函数允许您对图像应用仿射变换,但不能对图像内的三角形区域应用仿射变换。

为了克服这个限制,我们在源三角形周围找到一个边界框,并从源图像中裁剪出矩形区域。然后,我们将仿射变换应用于裁剪图像以获得输出图像。前一步是至关重要的,因为它允许我们将仿射变换应用于图像的某一区域,从而提高计算性能。最后,我们通过用白色填充输出三角形内的像素来创建三角形掩模。与输出图像相乘时,此掩模将三角形外部的所有像素变为黑色,同时保留三角形内所有像素的颜色。在我们进入细节之前,让我们读入输入和输出图像,并定义输入和输出三角形。对于本教程,我们的输出图像只是白色,但如果您愿意,可以读取另一个图像。

2.1 定义输入和输出

我们现在需要定义输入图像和输出图像,以及输入输出三角形坐标。我们已准备好完成将输入三角形内的所有像素转换为输出三角形所需的步骤。输入输出设定代码如下:

C++:

// Read input image and convert to float
Mat img1 = imread("robot.jpg");
img1.convertTo(img1, CV_32FC3, 1/255.0);
 
// Output image is set to white
Mat imgOut = Mat::ones(imgIn.size(), imgIn.type());
imgOut = Scalar(1.0,1.0,1.0);
 
// Input triangle
vector <Point2f> tri1;
tri1.push_back(Point2f(360,200));
tri1.push_back(Point2d(60,250));
tri1.push_back(Point2f(450,400));
     
// Output triangle
vector <Point2f> triOut;
tri2.push_back(Point2f(400,200));
tri2.push_back(Point2f(160,270));
tri2.push_back(Point2f(400,400));

Python:

# Read input image and convert to float
img1 = cv2.imread("robot.jpg")
 
# Output image is set to white
img2 = 255 * np.ones(img_in.shape, dtype = img_in.dtype)
 
# Define input and output triangles 
tri1 = np.float32([[[360,200], [60,250], [450,400]]])
tri2 = np.float32([[[400,200], [160,270], [400,400]]])

2.2 计算边界框

在此步骤中,我们计算三角形周围的边界框。这个想法只是扭曲图像的一小部分而不是整个图像以提高效率。通过boundingRect()得到包覆此三角形的最小正矩形。代码如下:

C++:

// Find bounding rectangle for each triangle
Rect r1 = boundingRect(tri1);
Rect r2 = boundingRect(tri2);

Python:

# Find bounding box. 
r1 = cv2.boundingRect(tri1)
r2 = cv2.boundingRect(tri2)

2.3 裁剪图像和更改坐标

要有效地将仿射变换应用于图像而不是整个图像,我们将根据上一步中计算的边界框裁剪输入图像。还需要修改三角形的坐标以反映它们在新裁剪图像中的位置。这是通过从三角形的x和y坐标减去边界框左上角顶点的x和y坐标来完成的。代码如下:

C++:

// Offset points by left top corner of the respective rectangles
vector<Point2f> tri1Cropped, tri2Cropped;
vector<Point> tri2CroppedInt;
 
for(int i = 0; i < 3; i++)
{
    tri1Cropped.push_back( Point2f( tri1[i].x - r1.x, tri1[i].y -  r1.y) );
    tri2Cropped.push_back( Point2f( tri2[i].x - r2.x, tri2[i].y - r2.y) );
 
    // fillConvexPoly needs a vector of Point and not Point2f
    tri2CroppedInt.push_back( Point((int)(tri2[i].x - r2.x), (int)(tri2[i].y - r2.y)) );
}
 
// Apply warpImage to small rectangular patches
Mat img1Cropped;
img1(r1).copyTo(img1Cropped);

Python:

# Offset points by left top corner of the 
# respective rectangles
 
tri1Cropped = []
tri2Cropped = []
     
for i in xrange(0, 3):
  tri1Cropped.append(((tri1[0][i][0] - r1[0]),(tri1[0][i][1] - r1[1])))
  tri2Cropped.append(((tri2[0][i][0] - r2[0]),(tri2[0][i][1] - r2[1])))
 
# Apply warpImage to small rectangular patches
img1Cropped = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]

2.4 计算仿射变换矩形

我们刚刚在裁剪的输入和输出图像中获得了输入和输出三角形的坐标。使用这两个三角形,我们可以找到仿射变换,它将使用以下代码将输入三角形转换为裁剪图像中的输出三角形。

C++:

// Given a pair of triangles, find the affine transform.
Mat warpMat = getAffineTransform( tri1Cropped, tri2Cropped );

Python:

# Given a pair of triangles, find the affine transform.
warpMat = cv2.getAffineTransform( np.float32(tri1Cropped), np.float32(tri2Cropped) )

2.5 应用仿射变换到三角形

将上一步骤中找到的仿射变换矩阵应用于裁剪的输入图像,以获得裁剪的输出图像。在OpenCV中,您可以使用warpAffine将仿射变换应用于图像。代码如下:

C++:

// Apply the Affine Transform just found to the src image
Mat img2Cropped = Mat::zeros(r2.height, r2.width, img1Cropped.type());
warpAffine( img1Cropped, img2Cropped, warpMat, img2Cropped.size(), INTER_LINEAR, BORDER_REFLECT_101);

Python:

# Apply the Affine Transform just found to the src image
img2Cropped = cv2.warpAffine( img1Cropped, warpMat, (r2[2], r2[3]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101 )

2.6 屏蔽三角形外的像素

在上一步中,我们获得了输出矩形图像。但是,我们对矩形区域内的三角形感兴趣。因此,我们使用fillConvexPoly创建一个掩模,用于遮蔽三角形外的所有像素。这个新的裁剪图像最终可以使用输出边界矩形的左上角坐标点置于输出图像中的正确位置。

C++:

// Get mask by filling triangle
Mat mask = Mat::zeros(r2.height, r2.width, CV_32FC3);
fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), 16, 0);
     
// Copy triangular region of the rectangular patch to the output image
multiply(img2Cropped,mask, img2Cropped);
multiply(img2(r2), Scalar(1.0,1.0,1.0) - mask, img2(r2));
img2(r2) = img2(r2) + img2Cropped;

Python:

# Get mask by filling triangle
mask = np.zeros((r2[3], r2[2], 3), dtype = np.float32)
cv2.fillConvexPoly(mask, np.int32(tri2Cropped), (1.0, 1.0, 1.0), 16, 0);
 
# Apply mask to cropped region
img2Cropped = img2Cropped * mask
 
# Copy triangular region of the rectangular patch to the output image
img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] * ( (1.0, 1.0, 1.0) - mask )
     
img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] + img2Cropped

3 代码

本文所有代码见:

?https://github.com/luohenyueji/OpenCV-Practical-Exercise??

C++:

#include "pch.h"
#include <opencv2/opencv.hpp>
#include <stdlib.h>

using namespace cv;
using namespace std;

/**
 * @brief Warps and alpha blends triangular regions from img1 and img2 to img 图像仿射变换
 * 
 * 
 * @param img1 输入图像
 * @param img2 输出图像
 * @param tri1 输入三角形坐标点
 * @param tri2 输出三角形坐标点
 */
void warpTriangle(Mat &img1, Mat &img2, vector<Point2f> tri1, vector<Point2f> tri2)
{
  // Find bounding rectangle for each triangle
  //得到每个三角形的最小外接矩形
  Rect r1 = boundingRect(tri1);
  Rect r2 = boundingRect(tri2);

  // Offset points by left top corner of the respective rectangles
  // 获得剪裁后的坐标点
  //输入和输出三角形坐标点
  vector<Point2f> tri1Cropped, tri2Cropped;
  //输出三角形坐标点int形式
  vector<Point> tri2CroppedInt;
  for (int i = 0; i < 3; i++)
  {
    tri1Cropped.push_back(Point2f(tri1[i].x - r1.x, tri1[i].y - r1.y));
    tri2Cropped.push_back(Point2f(tri2[i].x - r2.x, tri2[i].y - r2.y));

    // fillConvexPoly needs a vector of Point and not Point2f
    tri2CroppedInt.push_back(Point((int)(tri2[i].x - r2.x), (int)(tri2[i].y - r2.y)));
  }

  // Apply warpImage to small rectangular patches 应用仿射变换到三角形外接矩形
  Mat img1Cropped;
  //提取外接矩形区域
  img1(r1).copyTo(img1Cropped);

  // Given a pair of triangles, find the affine transform.
  // 提取仿射变换矩阵
  Mat warpMat = getAffineTransform(tri1Cropped, tri2Cropped);

  // Apply the Affine Transform just found to the src image
  Mat img2Cropped = Mat::zeros(r2.height, r2.width, img1Cropped.type());
  // 应用仿射变换
  warpAffine(img1Cropped, img2Cropped, warpMat, img2Cropped.size(), INTER_LINEAR, BORDER_REFLECT_101);

  // Get mask by filling triangle 获得掩模
  Mat mask = Mat::zeros(r2.height, r2.width, CV_32FC3);
  //填充多边形
  fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), 16, 0);

  // Copy triangular region of the rectangular patch to the output image
  // 应用掩模,获得输出图
  // 提取掩模对应的图像区域
  multiply(img2Cropped, mask, img2Cropped);
  // 获得输出图像掩模区域
  multiply(img2(r2), Scalar(1.0, 1.0, 1.0) - mask, img2(r2));
  // 保存仿射变换结果
  img2(r2) = img2(r2) + img2Cropped;
}

int main()
{
  // Read input image and convert to float
  // 读取图像,并将图像转换为float
  Mat imgIn = imread("./image/robot.jpg");
  imgIn.convertTo(imgIn, CV_32FC3, 1 / 255.0);

  // Output image is set to white
  Mat imgOut = Mat::ones(imgIn.size(), imgIn.type());
  //设定输出,输出为纯白色图像
  imgOut = Scalar(1.0, 1.0, 1.0);

  // Input triangle 输入三角形坐标点
  vector<Point2f> triIn;
  triIn.push_back(Point2f(360, 200));
  triIn.push_back(Point2d(60, 250));
  triIn.push_back(Point2f(450, 400));

  // Output triangle 输出三角形坐标点
  vector<Point2f> triOut;
  triOut.push_back(Point2f(400, 200));
  triOut.push_back(Point2f(160, 270));
  triOut.push_back(Point2f(400, 400));

  // Warp all pixels inside input triangle to output triangle 仿射变换
  warpTriangle(imgIn, imgOut, triIn, triOut);

  // Draw triangle on the input and output image.

  // Convert back to uint because OpenCV antialiasing
  // does not work on image of type CV_32FC3

  //保存为INT型
  imgIn.convertTo(imgIn, CV_8UC3, 255.0);
  imgOut.convertTo(imgOut, CV_8UC3, 255.0);

  // Draw triangle using this color
  Scalar color = Scalar(255, 150, 0);

  // cv::polylines needs vector of type Point and not Point2f
  vector<Point> triInInt, triOutInt;
  for (int i = 0; i < 3; i++)
  {
    triInInt.push_back(Point(triIn[i].x, triIn[i].y));
    triOutInt.push_back(Point(triOut[i].x, triOut[i].y));
  }

  // Draw triangles in input and output images
  //在图中画出三角形
  polylines(imgIn, triInInt, true, color, 2, 16);
  polylines(imgOut, triOutInt, true, color, 2, 16);

  imshow("Input", imgIn);
  imshow("Output", imgOut);
  waitKey(0);

  return 0;
}

Python:

#!/usr/bin/env python

# Copyright (c) 2016 Satya Mallick <spmallick@learnopencv.com>
# All rights reserved. No warranty, explicit or implicit, provided.

import cv2
import numpy as np


# Warps and alpha blends triangular regions from img1 and img2 to img
def warpTriangle(img1, img2, tri1, tri2) :
    
    # Find bounding rectangle for each triangle
    r1 = cv2.boundingRect(tri1)
    r2 = cv2.boundingRect(tri2)
    
    # Offset points by left top corner of the respective rectangles
    tri1Cropped = []
    tri2Cropped = []
    
    for i in range(0, 3):
        tri1Cropped.append(((tri1[0][i][0] - r1[0]),(tri1[0][i][1] - r1[1])))
        tri2Cropped.append(((tri2[0][i][0] - r2[0]),(tri2[0][i][1] - r2[1])))

    # Crop input image
    img1Cropped = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]

    # Given a pair of triangles, find the affine transform.
    warpMat = cv2.getAffineTransform( np.float32(tri1Cropped), np.float32(tri2Cropped) )
    
    # Apply the Affine Transform just found to the src image
    img2Cropped = cv2.warpAffine( img1Cropped, warpMat, (r2[2], r2[3]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101 )

    # Get mask by filling triangle
    mask = np.zeros((r2[3], r2[2], 3), dtype = np.float32)
    cv2.fillConvexPoly(mask, np.int32(tri2Cropped), (1.0, 1.0, 1.0), 16, 0);

    img2Cropped = img2Cropped * mask
    
    # Copy triangular region of the rectangular patch to the output image
    img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] * ( (1.0, 1.0, 1.0) - mask )
    
    img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] = img2[r2[1]:r2[1]+r2[3], r2[0]:r2[0]+r2[2]] + img2Cropped


if __name__ == '__main__' :

    # Read input image
    imgIn = cv2.imread("./image/robot.jpg")
    
    # Output image is set to white
    imgOut = 255 * np.ones(imgIn.shape, dtype = imgIn.dtype)
    
    # Input triangle
    triIn = np.float32([[[360,200], [60,250], [450,400]]])
    
    # Output triangle
    triOut = np.float32([[[400,200], [160,270], [400,400]]])
    
    # Warp all pixels inside input triangle to output triangle
    warpTriangle(imgIn, imgOut, triIn, triOut)

    # Draw triangle using this color
    color = (255, 150, 0)

    # Draw triangles in input and output images.
    cv2.polylines(imgIn, triIn.astype(int), True, color, 2, 16)
    cv2.polylines(imgOut, triOut.astype(int), True, color, 2, 16)

    cv2.imshow("Input", imgIn)
    cv2.imshow("Output", imgOut)
    
    
    cv2.waitKey(0)

4 参考

??https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python/??

相关推荐

当Frida来“敲”门(frida是什么)

0x1渗透测试瓶颈目前,碰到越来越多的大客户都会将核心资产业务集中在统一的APP上,或者对自己比较重要的APP,如自己的主业务,办公APP进行加壳,流量加密,投入了很多精力在移动端的防护上。而现在挖...

服务端性能测试实战3-性能测试脚本开发

前言在前面的两篇文章中,我们分别介绍了性能测试的理论知识以及性能测试计划制定,本篇文章将重点介绍性能测试脚本开发。脚本开发将分为两个阶段:阶段一:了解各个接口的入参、出参,使用Python代码模拟前端...

Springboot整合Apache Ftpserver拓展功能及业务讲解(三)

今日分享每天分享技术实战干货,技术在于积累和收藏,希望可以帮助到您,同时也希望获得您的支持和关注。架构开源地址:https://gitee.com/msxyspringboot整合Ftpserver参...

Linux和Windows下:Python Crypto模块安装方式区别

一、Linux环境下:fromCrypto.SignatureimportPKCS1_v1_5如果导包报错:ImportError:Nomodulenamed'Crypt...

Python 3 加密简介(python des加密解密)

Python3的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto和cryptography上,我...

怎样从零开始编译一个魔兽世界开源服务端Windows

第二章:编译和安装我是艾西,上期我们讲述到编译一个魔兽世界开源服务端环境准备,那么今天跟大家聊聊怎么编译和安装我们直接进入正题(上一章没有看到的小伙伴可以点我主页查看)编译服务端:在D盘新建一个文件夹...

附1-Conda部署安装及基本使用(conda安装教程)

Windows环境安装安装介质下载下载地址:https://www.anaconda.com/products/individual安装Anaconda安装时,选择自定义安装,选择自定义安装路径:配置...

如何配置全世界最小的 MySQL 服务器

配置全世界最小的MySQL服务器——如何在一块IntelEdison为控制板上安装一个MySQL服务器。介绍在我最近的一篇博文中,物联网,消息以及MySQL,我展示了如果Partic...

如何使用Github Action来自动化编译PolarDB-PG数据库

随着PolarDB在国产数据库领域荣膺桂冠并持续获得广泛认可,越来越多的学生和技术爱好者开始关注并涉足这款由阿里巴巴集团倾力打造且性能卓越的关系型云原生数据库。有很多同学想要上手尝试,却卡在了编译数据...

面向NDK开发者的Android 7.0变更(ndk android.mk)

订阅Google官方微信公众号:谷歌开发者。与谷歌一起创造未来!受Android平台其他改进的影响,为了方便加载本机代码,AndroidM和N中的动态链接器对编写整洁且跨平台兼容的本机...

信创改造--人大金仓(Kingbase)数据库安装、备份恢复的问题纪要

问题一:在安装KingbaseES时,安装用户对于安装路径需有“读”、“写”、“执行”的权限。在Linux系统中,需要以非root用户执行安装程序,且该用户要有标准的home目录,您可...

OpenSSH 安全漏洞,修补操作一手掌握

1.漏洞概述近日,国家信息安全漏洞库(CNNVD)收到关于OpenSSH安全漏洞(CNNVD-202407-017、CVE-2024-6387)情况的报送。攻击者可以利用该漏洞在无需认证的情况下,通...

Linux:lsof命令详解(linux lsof命令详解)

介绍欢迎来到这篇博客。在这篇博客中,我们将学习Unix/Linux系统上的lsof命令行工具。命令行工具是您使用CLI(命令行界面)而不是GUI(图形用户界面)运行的程序或工具。lsoflsof代表&...

幻隐说固态第一期:固态硬盘接口类别

前排声明所有信息来源于网络收集,如有错误请评论区指出更正。废话不多说,目前固态硬盘接口按速度由慢到快分有这几类:SATA、mSATA、SATAExpress、PCI-E、m.2、u.2。下面我们来...

新品轰炸 影驰SSD多款产品登Computex

分享泡泡网SSD固态硬盘频道6月6日台北电脑展作为全球第二、亚洲最大的3C/IT产业链专业展,吸引了众多IT厂商和全球各地媒体的热烈关注,全球存储新势力—影驰,也积极参与其中,为广大玩家朋友带来了...