Face morphing using opencv - python

Can I get some ideas on how to morph the face in a live video using opencv? I have tried Face substitution but it is implemented using openFrameworks.
I would like to implement the same using opencv. Is there any other methods available in opencv than diirectly porting Face substituion code from openFrameworks to Opencv?
I have also gone through this link, but few people have mentioned as the face morphing is deprecated in opencv?

I recently wrote an article on face morphing using OpenCV. I have shared the code in C++ / Python. You can find the details here
http://www.learnopencv.com/face-morph-using-opencv-cpp-python/
but the basic idea is as follows.
Find Point Correspondences in the two input images.
Do Delaunay triangulation on the points.
The amount of morphing is controlled by a parameter alpha. E.g .for alpha = 0, you will get Ted Cruz in the example below, and for alpha = 1. you will get Hillary Clinton. For any other alpha, it will be the blend between the two. Use alpha to calculate the location of the points in the output ( morphed ) image by taking a weighted average of the two input image points.
Calculate the affine transform between every triangle in the input images and the destination image ( morphed image ).
Warp triangle from each input images to the output image, and blend the pixels based on alpha. Do this for every triangle and you get the output morphed image.
Hope this helps.

I don't know any libraries that do this specifically, but you could cobble together something yourself. You'd need a set of common fiducial points that you reference in all faces. Then you'd want to use those point to do Delaunay triangulation on it.
Now you can either do the transform directly from one face chassis to the other, or you can write it to an intermediary normalized face, make changes to that and then write it anywhere.

Here are the steps of the face morphing implementation with mesh-warping algorithm (you could implement it with opencv or python scipy / scikit-image):
Defining Correspondences: find point-correspondences between the faces to be aligned using facial landmarks (detect landmarks with dlib, e.g.,).
Delaunay Triangulation: You need to provide a triangulation (Delaunay triangulation, e.g.) of these points that will be used for morphing (with scipy.spatial's Delaunay, e.g.,).
Computing the Mid-way (morphed) Face: computing the average shape, warp both faces into that shape (calculate the affine transforms using the triangles in source image and the corresponding ones using the morphed image and warp the points inside the triangles and alpha-blend the warped images to obtain the final morphed image, using scikit-image's warp(), e.g.,).
The next animation shows the animated output from an implementation of mesh-warping algorithm with scipy / numpy / scikit-image in python (sequence of morph images starting from Monalisa image to Leonardo da Vinci image). This can be found here too.
Another popular algorithm is Beier-neely morphing algorithm (https://en.wikipedia.org/wiki/Beier%E2%80%93Neely_morphing_algorithm)

Check a face-morphing tool in Python using OpenCV

Related

How to implement radial motion blur by Python OpenCV?

I can find motion blur kernel in horizontal and vertical direction, e.g. this link.
However, how can I implement radial motion blur like following pictures? I can find this functionality in Photoshop etc. I cannot find any kernel reference in website. How can I implement it by python opencv? Thanks
I don't think OpenCV has something like this built-in, but DIPlib has: dip.AdaptiveGauss(). It blurs the image with a different Gaussian at every pixel. One image indicates the orientation of the Gaussian, another one indicates the scaling.
This is how I replicated your blurred image:
import diplib as dip
img = dip.ImageRead('rose.jpg')
scale = dip.CreateRadiusCoordinate(img.Sizes()) / 100
angle = dip.CreatePhiCoordinate(img.Sizes())
out = dip.AdaptiveGauss(img, [angle, scale], [1,5])
dip.Show(out)
Disclaimer: I'm an author of DIPlib.

Triangulation of Polygons in a binary Mask

I´m currently working on a project to measure the surface of plant leaves. Until now I´ve successfully implemented an RCNN model to segment individual leaves and also generated a depth map using stereo computer vision which allows me to calculate distances between any two points.
Now I´m stuck trying to connect everything together in order to calculate the area of a leaf/polygon.
**I got original RGB images, Binary masks containing leaves, and also the depth information of every pixel.
Can someone please point me in the right direction?**
I reckon the right way would be to use Delauney triangulation on the polygons in the binary masks and then calculate the surface using the distance between the 3 points of each triangle. I haven't been able to find something quite similar to my problem which is implemented in python.
Thanks so much for your help in advance. I´ll upload a picture of an RGB image with the masks plotted.
leaf instance segmentation
Count the pixels inside the outlines (by polygon filling) or use the shoelace formula.

How to detect the relative depth of pixels on a image?

I am trying to obtain the relative depth of pixels of an image. For example, the image in https://www.awn.com/news/nvidia-unveils-quadro-rtx-worlds-first-ray-tracing-gpu . I don't need the precise distance of each pixel, which I believe would be impossible, but I would like to get something as "the green ball is further than the other balls". Is it possible using OpenCV in python? The codes I generated can identify each ball, but not their relative distance or depth, so they are pretty much useless to my intents.
That's an ill-posed problem (you can not measure depth with a single RGB camera) and a topic of resent research. I found this survey paper. Most often a depth image is learned from an RGB image using convolutional neural networks.
However, if you use a lot of prior information about your scene (all objects are circular within in the image and the partially visible circles corresponds to the ones which are in the background), then you might be able to do something with heuristical methods like, thresholding, edge detection or hough transforms, but it won't be easy.

How to mosaic/bend/curve image with curvature in python?

I have an image that represents the elevation of some area. But the drone that made it didn't necessarily go in a straight line(although image is always rectangular). I also have gps coordinates generated every 20cm of the way.
How can I "bend" this rectangular image (curve/mosaic) so that it represents the curved path that the drone actually went through? (in python)
I haven't managed to write any code as I have no idea what is the name of this "warping" of the image. Please find the attached image as a wanted end state, and normal horizontal letters as a start state.
There might be a better answer, but I guess you could use the remapping functions of openCV for that.
The process would look like that :
From your data, get your warping function. This will be a function that maps (x,y) pixel values from your input image I to (x,y) pixel values from your output image O
Compute the size needed in the output image to host your whole warped image, and create it
Create two maps, mapx and mapy, which will tell the pixel coordinates in I for every pixel in 0 (that's, in a sense, the inverse of your warping function)
Apply OpenCV remap function (which is better than simply applying your maps because it interpolates if the output image is larger than the input)
Depending on your warping function, it might be very simple, or close to impossible to apply this technique.
You can find an example with a super simple warping function here : https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/remap/remap.html
More complex examples can be looked at in OpenCV doc and code when looking at distortion and rectification of camera images.

Detect an arc from an image contour or edge

I am trying to detect arcs inside an image. The information that I have for certain with me is the radius of the arc. I can try and maybe get the centre of the circle whose arc I want to identify.
Is there any algorithm in Open CV which can tell us that the detected contour ( or edge from canny edge is an arc or an approximation of an arc)
Any help on how this would be possible in OpenCV with Python or even a general approach would be very helpful
Thanks
If you think that there will not be any change in the shape (i mean arc won't become line or something like this) then you can have a look a Generalized Hough Transform (GHT) which can detect any shape you want.
Cons:
There is no directly function in openCV library for GHT but you can get several source code at internet.
It is sometimes slow but can become fast if you set the parameters properly.
It won't be able to detect if the shape changes. for exmaple, i tried to detect squares using GHT and i got good results but when square were not perfect squares (i.e. rectangle or something like that), it didn't detect.
You can do it this way:
Convert the image to edges using canny filter.
Make the image binary using threshold function there is an option for regular threshold, otsu or adaptive.
Find contours with sufficient length (findContours function)
Iterate all the contours and try to fit ellipse (fitEllipse function)
Validate fitted ellipses by radius.
Check if detected ellipse is good fit - checking how much of the contour pixels are on the detected ellipse.
Select the best one.
You can try to increase the speed using RANSAC each time selecting 6 points from binarized image and trying to fit.
My math is rusty, but...
What about evaluating a contour by looping over its composite edge-nodes and finding those where the angle between the edges doesn't change too rapidly AND doesn't change sign?
A chain of angles (θ) where:
0 < θi < θmax
with number of edges (c) where:
c > dconst
would indicate an arc of:
radius ∝ 1/(θi + θi+1 + ...+ θn)/n)
or:
r ∝ 1/θave
and:
arclenth ∝ c
A way of finding these angles is discussed at Get angle from OpenCV Canny edge detector

Categories

Resources