# What function to apply a Metamorphopsia (grid warp) translation?

How would I go about finding or creating a function to warp images dependent on grid lines surrounding an origin (I don't wish for a grid on my actual images):

It should work in practically the same manner as the envelope distort tool in Adobe Illustrator, though I wish for mine to be adjusted according to amplification of the warp, so small multiplier would lead to less warp, whilst higher would lead to greater warp encompassing more of the image surrounding the origin. Also of note is I wish for my images to keep their colours. I use Python. The distortion surrounding the point can be random, just not extreme.

Background info: Compounding adversarial attacks into biological relevance and then researching the biological relations of different NNs. Created images previously (through illustrator) though the amplification will allow me to go back and forth between early and late stages of diseases, and obviously just make it far simpler for me to increase my dataset tenfold, which I desperately need also!

Sorry for using medical term for the translation name, image processing is not my area of expertise in the slightest!

• Can I please ask if this was resolved? – A_A Jun 2 at 8:43
• I wasn't able to do it through scipy, ended up scripting it through Photoshop. I dunno if I can select your answer because I've no idea if it works. – bemzoo Jun 2 at 10:11
• I appreciate it. If I find some time, I might try to add some more practical information, but the more "difficult" thing to demonstrate what would be relevant to you would (still) be the definition of the transform. All the best. – A_A Jun 2 at 10:57

What you are looking for is called a "Geometric Transformation".

When applied to image processing, it comes down to a re-mapping of pixel positions. In optical systems, they can be used to represent certain distortions caused by lenses. In fact, ray transfer matrices end up being applied as geometric transformations to images, since they represent exactly how is a pixel displaced from its original position as its light ray travels within an optical system.

There are two things that need a little bit more consideration with geometric transformations:

1. How to express the mapping of pixels; and
2. How to apply the interpolation.

Expressing the mapping of the pixels might be relatively "easy" for known distortions (as mentioned earlier) but what is depicted in the body of the question here seems to be arbitrary (?). In any case, you are going to have to somehow express the function that takes a pixel from location $$x,y$$ to location $$x',y'$$ in a way that makes sense to the problem at hand. The way that image processing software (such as Illustrator that you mention) does it, is by setting up a grid that divides the image into a set of patches. These lines can be piecewise linear or bezier curves. This "control" (in terms of a user interface element) is not exactly straightforward to build.

Interpolation is absolutely necessary here because the geometric transformation might "ask" for pixel values between known pixels. There are obvious ways out of this problem (e.g. linear interpolation) but it is very likely that you will need a different method that makes sense within your application.

In Python, geometric transformations can be handled by Scipy's geometric_transformation(). Its use is straightforward provided that you know the input and output pixel locations.

Hope this helps.

EDIT:

How would I go about remapping these pixels considering I don't want singular pixels remapped but the entire areas 'pulled' in a direction.

This is what the geometric transformation does, as long as the input/output point pairs are provided.

Will interpolation work with colour in my image?

Yes.

Do you have any suggestions where I could learn how to do this practically? Arbitrary can come after I've sorted this first part out!

Yes, have a look at the links i provided. From the mathematical point of view, we are talking about a series of linear transforms that map a "rectangle" to a distorted "rectangle". See the documentation of the geometric_transformation().

• It doesn't have to be arbitrary, can be same effect applied to every picture, just "seemingly" arbitrary. How would I go about remapping these pixels considering I don't want singular pixels remapped but the entire areas 'pulled' in a direction. Will interpolation work with colour in my image? Do you have any suggestions where I could learn how to do this practically? Arbitrary can come after I've sorted this first part out! – bemzoo May 21 at 16:41
• imgur.com/a/Vg6IPSn is an example I've made to describe the translation – bemzoo May 21 at 17:34
• I either run into issues of me needing 3channel, or I run into issues of pixel alignment. – bemzoo May 21 at 18:57
• @bemzoo Please see edits – A_A May 22 at 11:22