Happy new year. I've started this blog today with an article about computer vision.
Introduction
In this article, I will explain about "Image Morphing" which means changing the shape of a object gradually into the shape of another object. I wrote a program code using OpenCV. My algorithm is based on this slide. My code morph myself into "dragon". Why dragon? In Japan, each year is associated with an animal according to a 12-year mathematical cycle, and 2012 is a year-of-dragon. So, this program is a kind of new year greeting to readers :)Overview
Image Morphing needs two images and mapping between their feature point as input, and is composed of two phase. First, images need to be warped so that their corresponding feature point is at the same position. Second, blend two images into one image using cross-dissolving. So images are warped and cross-dissolving into one image while time parameter is increasing. I will explain the detail about these methods.Warping
Warping is required to make corresponding pixels in two images be at the same position. To do so, we have to prepare pairs of lines. In this experiment, I use these two images. the left is a source image, and the right is a destination image.source image | destination image |
Pairs of lines for these images are below.
First, lines of the source image.
[srcFeature.txt]
15 74 130 122 1 74 130 82 132 82 132 106 135 106 135 153 137 106 135 118 177 118 177 153 137 122 1 205 130 74 130 81 209 205 130 208 219 81 209 123 266 208 219 123 266 98 211 119 226 119 226 159 215 98 211 119 206 119 206 159 215And lines of the destination image are below.
[destFeature.txt]
15 84 83 147 20 84 83 96 82 96 82 119 88 119 88 184 88 119 88 154 127 154 127 184 88 147 20 230 82 84 83 75 135 230 82 221 135 75 135 156 275 221 135 156 275 80 135 156 257 156 257 217 140 80 135 151 154 151 154 217 140The first line is the number of lines n. Next n lines contains four integers - Px, Py, Qx, Qy which represent the coordinate of the starting point and the ending point. For example, the first line or left image is from (74, 130) to (122, 1) which means from left ear to top of the head, and the first line of right image is the corresponding one.
We need to get warped image from the pairs. If the location of X' on the destination image is given, how to calculate the location of X on the source image which is corresponding to X'?
First, we calculate u and v. The value u goes from 0 to 1 as the pixel moves from P' to Q'. The value v is the perpendicular distance from the line to X'. Once we get u and v, we can apply them to the source image, and so X coordinate will be acquired.
In this case, we have multiple pairs of lines, so we need to average the sum of X coordinate weighted by a kind of factor. The factor is set to be inversely proportional to the distance from line and directly proportional to the length of the line. (the details are described in the slide mentioned in "Instruction".)
By calculating source pixels for each pixels in the destination image, we can get warped image whose shape is closer to the destination image.