opencv 仿射变换_如何使用OpenCV将一个三角形扭曲到另一个三角形

e7c3dd8453c50d60453868a5c25b4d06.png

图1:左图中蓝色三角形内的所有像素都已转换为右图中的蓝色三角形。

在本教程中,我们将看到如何将图像中的单个三角形扭曲到另一个图像中的另一个三角形。

在计算机图形学中,人们一直处理翘曲三角形,因为任何3D表面都可以用三角形近似。图像可以分解为三角形并扭曲。但是,在OpenCV中,没有开箱即用的方法可以将三角形内的像素扭曲到另一个三角形内的像素。

本教程将逐步说明如何将图1中左图中的三角形转换为右图。

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

什么是仿射变换?

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

6ccbaa9ee7b8901a050d91c5d2ba283a.png

在OpenCV中,仿射变换是2×3矩阵。该矩阵的前两列编码旋转,缩放和剪切,最后一列编码平移(即移位)。

a8148aea29fc55eb247a898fb9052235.png

给定一点(x,y),上面的仿射变换,(x_t,y_t)使用下面给出的等式将其移动到点

9dd8d5bf22cc823e3a9f16ec6c793f32.png

使用OpenCV进行三角形变形

我们现在知道,要将三角形扭曲到另一个三角形,我们将需要使用仿射变换。在OpenCV中,warpAffine允许您对图像应用仿射变换,但不能对图像内的三角形区域应用仿射变换。为了克服这个限制,我们在源三角形周围找到一个边界框,并从源图像中裁剪出矩形区域。然后,我们将仿射变换应用于裁剪图像以获得输出图像。前一步是至关重要的,因为它允许我们将仿射变换应用于图像的小区域,从而提高计算性能。最后,我们通过用白色填充输出三角形内的像素来创建三角形蒙版。与输出图像相乘时,此蒙版将三角形外部的所有像素变为黑色,同时保留三角形内所有像素的颜色。

在我们进入细节之前,让我们读入输入和输出图像,并定义输入和输出三角形。对于本教程,我们的输出图像只是白色,但如果您愿意,可以读取另一个图像。

C++

// Read input image and convert to floatMat img1 = imread("robot.jpg");img1.convertTo(img1, CV_32FC3, 1/255.0); // Output image is set to whiteMat imgOut = Mat::ones(imgIn.size(), imgIn.type());imgOut = Scalar(1.0,1.0,1.0); // Input trianglevector  tri1;tri1.push_back(Point2f(360,200));tri1.push_back(Point2d(60,250));tri1.push_back(Point2f(450,400)); // Output trianglevector  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]]])

我们现在定义了输入和输出,我们已准备好完成将输入三角形内的所有像素转换为输出三角形所需的步骤。

1.计算边界框

在此步骤中,我们计算三角形周围的边界框。这个想法只是扭曲图像的一小部分而不是整个图像以提高效率。

C++

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

Python

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

2.裁剪图像和更改坐标

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

C++

// Offset points by left top corner of the respective rectanglesvector tri1Cropped, tri2Cropped;vector 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 patchesMat 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 patchesimg1Cropped = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]

3.估计仿射变换

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

// 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) )

4.边界框内

扭曲像素将上一步骤中找到的仿射变换应用于裁剪的输入图像,以获得裁剪的输出图像。在OpenCV中,您可以使用warpAffine将仿射变换应用于图像。

C++

// Apply the Affine Transform just found to the src imageMat 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 imageimg2Cropped = cv2.warpAffine( img1Cropped, warpMat, (r2[2], r2[3]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101 )

5.屏蔽三角形外的像素

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

C++

// Get mask by filling triangleMat 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 imagemultiply(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 trianglemask = 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 regionimg2Cropped = img2Cropped * mask # Copy triangular region of the rectangular patch to the output imageimg2[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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值