Pages

Jan 1, 2012

Image Morphing


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 215
And 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 140
The 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.

Cross-dissolving

Cross-dissolving is simpler than warping. We already have similar-shape images. For each pixel in the destination image, we mix the values of corresponding source pixel and destination pixel. The ratio of mixture depends on the time parameter. Time parameter goes from 0 to 1, so at the beginning point(t=0) the ratio of the source image is 1 and the other is 0, and at the end point (t=1) vice versa.

Warping+cross-dissolving=Morphing

As I mentioned above, the time parameter goes from 0 to 1. To complete morphing the source image into the destination image, at first we compute the intermediate image at each time. This means that both the source image and the destination image are warped into the intermediate image. So, for each pixels in intermediate image, we can also compute the corresponding pixels in the source image and the destination image. After that, apply cross-dissolving to these pixels.

Result


Source code

Notice: This source code requires OpenCV 2.0 or later, and this only outputs image files corresponding to each frame. I used ffmpeg to combine those image files into a movie file.

16 comments:

  1. Takahiro, seems there are quite a number of typo in the source code. Would be able to send me the original source file?

    ReplyDelete
  2. This is original source file I made. Where do you find difficulty?

    ReplyDelete
  3. Hi Takahiro,
    I tried to compile your code but it has many issues(Not only 1 or 2)
    Try to fix it please.
    10x

    ReplyDelete
    Replies
    1. I did not see error. Please copy some of errors.

      Delete
  4. Hi. It would be really helpful if you could share your source code which uses the above morphing code. Thanks in advance.

    ReplyDelete
  5. Just check your code once again...i think the problem is that it has not been posted correctly. You for loops are not closing.
    eg: for (int i=0; i> x1 >> y1 >> x2 >> y2;
    CvPoint p1=cvPoint(x1,y1);
    CvPoint p2=cvPoint(x2,y2);
    srcFeatures.pb(mp(p1,p2));
    }
    There are many more...so just update the post

    ReplyDelete
    Replies
    1. Thank you for pointing out. I fixed code embedding.

      Delete
  6. Hi Takahiro,
    i compile success but picture cannot show .pls tell me to set pic ??

    ReplyDelete
    Replies
    1. set const char* srcImageFile = argv[1];
      const char* srcFeatureFile = argv[2];
      const char* destImageFile = argv[3];
      const char* destFeatureFile = argv[4]; ?????

      Delete
    2. In my dragon example, argv[2] is srcFeature.txt and argv[4] is destFeature.txt described in this article. In these files, first line is a number of "lines" after the first line and each lines after the first line represents the x-y axis of the line on the image. Each line consists of 4 integer, x-axis/y-axis of the start point and x-axis/y-axis of the end point. The line of srcFeature.txt corresponds to destFeature.txt. For example, in my dragon example, The line between (74, 130) and (122, 1) on the src image corresponds to the line between (84, 83) and (147, 20) on the dest image. "correspond" means the lines on the src image is morphed into the line on the dest image.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I can do it ............... YE THANK YOU VERY MUCH .... >_______________________<

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Although I do not mention maintain percentages on my website the term is price defining outcome of|as a result of} it comes up a lot. The maintain share is the ratio of chips the on line casino retains to the whole chips bought. If each player loses their entire buy of chips then the maintain will be one hundred pc. It 1xbet korea is possible for the maintain to exceed one hundred pc if gamers carry to the desk chips purchased at another desk.

    ReplyDelete
  11. Europe’s GGR is expected to hit €33 billion by 2023, and about 55% of that is expected to come back from cellular playing. In 2016, the most common reasons for playing online were 토토사이트 cash (55.9%), enjoyable (54.5%), and the lure of profitable (53.3%). America’s total gross gaming income of casinos in 2020 was $29.ninety eight billion. The final step for most people will be to accept accept|to simply accept} Ignition’s phrases and circumstances.

    ReplyDelete
  12. Today heaps of|there are many} licensing jurisdictions positioned round the} world and offering completely different terms for their clients. Depending on the nation, licenses can be local, international , have a unique set of documents for registration, prices of registration and further support, numerous working situations and other special particulars. Blackjack is the game of option to many high-rollers and do you know why? Because blackjack is a difficult, logic and skill-based sport the place your pondering, strategy, and calculations determine the result result} 카지노 of the game. Alabama could ultimately choose to go the identical route as neighboring Tennessee, which runs an unique online market. The business proposal has reached the signature threshold essential to qualify, although collections nonetheless need to be verified by state officials.

    ReplyDelete