Learn OpenCV之WarpTriangle

这篇文章将讲述的是如何将一个图片内的三角形内容映射到另一个图片内的不同形状的三角形内。

在图形学的研究中,研究者常常进行三角形之间的变换操作,因为任意的3D表面都可以用多个三角形去近似表示。同样的,图片也可以分解成多个三角形来表示。但是在OpenCV中并没有一个直接可以将三角形转换为另一个三角形的函数。

下面将详细的讲述,如果将下图中的左图转化成右图。
在这里插入图片描述

在此之前,先介绍一个什么是仿射变换。

什么是仿射变换

仿射变换是一种将一个三个点的点集(例如三角形)变换到另一个三点点集的最简单的变换方法。它包括平移、缩放、旋转和剪切操作。下图展示了对一个正方形做仿射变换的效果。但是仿射变换还是有一定局限性的,它不能将一个正方形形变为任意的四边形。一对平行的两条边经过放射变换后仍是平行的。
在这里插入图片描述
在OpenCV中,仿射变换可以用一个 2 × 3 2 \times 3 的矩阵表示,这个矩阵的前两列表示旋转、缩放、裁剪操作,后两列表示平移操作。如下公式所示
S = [ a b t x c d t y ] S = \left[ \begin{matrix} a & b & t_{x} \\ c & d & t_{y} \end{matrix} \right ]
假设现在给定一点其坐标为 ( x , y ) (x, y) ,通过上述仿射变换得到点坐标 ( x t , y t ) (x_{t}, y_{t}) 如下式所示
[ x t y t ] = [ a b c d ] [ x y ] + [ t x t y ] \left[\begin{matrix}x_{t} \\ y_{t}\end{matrix}\right] =\left[\begin{matrix}a & b \\ c & d\end{matrix}\right] \left[\begin{matrix}x \\ y\end{matrix}\right]+ \left[\begin{matrix}t_{x} \\ t_{y}\end{matrix}\right]

使用OpenCV完成三角形的仿射变换

在OpenCV中,函数warpAffine可以对一张图片进行仿射变换,但是不能直接对图片中的三角形做仿射变换。为了完成三角形的仿射变换,可以在图片中先找到三角形的外界矩形并将它从图片中截取出来,然后将截取的矩形进行仿射变换从而获得输出图像。*(其实可以不用截取三角形的外接矩形也能完成,但是使用全图会增加计算量。)*最后我们制作一个蒙版(mask),蒙版大小和仿射变换输出图像大小一样,蒙版里需要进行输出的三角形区域值为1其他为0,这样将蒙版与输出图片相乘就可以得到最终的三角形输出了。

下面进行代码的详细讲解,在这里输出图片是一个全白的图片,你也可以将仿射变换后的三角形放置在其他图片中。

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 img2 = 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(Point2f(60, 250));
tri1.push_back(Point2f(450, 400));

// Output triangle
vector <Point2f> tri2;
tri2.push_back(Point2f(400, 200));
tri2.push_back(Point2f(160, 270));
tri2.push_back(Point2f(400, 400));

Python

# Read input image
img1 = cv2.imread("robot.jpg")

# Output image is set to white
img2 = 255 * np.ones(img1.shape, dtype = img1.type)

# 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 triangle
Rect r1 = boundingRect(tri1);
Rect r2 = boundingRect(tri2);

Python

# Find bounding box. 
r1 = cv2.boundingRect(tri1)
r2 = cv2.boundingRect(tri2)
2.截取外界矩形并修改三角形的坐标值

因为要在上面获得的外接矩形中进行仿射变换,所以原点从图片的左上角点变成了外接矩形的左上角点,从而要修改三角形的三点坐标。

C++

// Offset points by left top corner of the respective rectangles
vector<Point2f> tri1Cropped, tri2Cropped;
vector<Point2f> 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]]
3.获取仿射变换矩阵

通过上述得到的输入三角形的三个坐标和输出三角形的三个坐标,可以计算出仿射变换要使用的仿射变换矩阵。

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))
4.对外接矩形做仿射变换

通过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 ))
5.利用蒙版获取最终的三角形仿射变换结果

蒙版可以先建立一个值全为0的图片,然后利用fillConvexPoly函数将要获取的三角形区域填充为1,最后将蒙版与上个步骤输出的图片结果相乘就可以得到对应区域的值了。

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[3], 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

到这里三角形的仿射变换就完成了。

代码下载
原博客-Warp one triangle to another using OpenCV ( C++ / Python )

猜你喜欢

转载自blog.csdn.net/liuxiaoheng1992/article/details/85165409