Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most basic approach to implement a liquify filter like the one photoshop has , in java?

What is the most basic approach to implement a liquify filter like the one photoshop has , in java ?

like image 507
Tony Avatar asked Dec 22 '22 22:12

Tony


2 Answers

Basically, you have a source image and a mesh. The mesh starts as a grid with perfect squares, but gets deformed. The algorithm is

  For Each section of the mesh
     For Each pixel of the section
         (x, y) = Location in original photo for this pixel // (floating point)
         color = ColorFromOriginal(x, y) // this needs to blend neighboring pixels if fractional
         setColor(color)

Figuring out the (x, y) is simple geometry -- map the center of the deformed square to the center of the original, then figure out which triangle you are in (N, S, E, W) and map the deformed triangle to the original.

  +---------+
  |\       /|
  | \  N  / |
  |  \   /  |
  |   \ /   |
  | W  X  E |
  |   / \   |
  |  /   \  |
  | /  S  \ |
  |/       \|
  +---------+

Once you have the (x, y) in floating point, calculate it's color by blending the four pixels that overlap that floating pt. coordinate in the ratio of the overlap.

Integer pixels

   +----+----+----+
   |    |    |    |
   |    |    |    |
   +----+----+----+
   |    |    |    |
   |    |    |    |
   +----+----+----+
   |    |    |    |
   |    |    |    |
   +----+----+----+

Floating pt. pixel overlaid on it

   +----+----+----+
   |    |    |    |
   |   x|xx  |    |
   +----+----+----+
   |   x|xx  |    |
   |    |    |    |
   +----+----+----+
   |    |    |    |
   |    |    |    |
   +----+----+----+

The result color is the blending of the four pixels in the ratio of how much it overlaps.

This is exactly the algorithm of a resize (resample) -- The mesh is not deformed, just enlarged, so the triangle step is unnecessary, but it's the same idea.

like image 173
Lou Franco Avatar answered Dec 24 '22 14:12

Lou Franco


What you are looking for is basically a warp filter, you can check out: http://www.jhlabs.com/ip/filters/ and I guess what you are looking for is http://www.jhlabs.com/ip/filters/WarpFilter.html

Hope that helps

like image 39
Deniz Mert Edincik Avatar answered Dec 24 '22 15:12

Deniz Mert Edincik