I have the following Python code

import matplotlib.pyplot as plt
import numpy as np

def make_random_gradient(image_shape):
    x, y = np.meshgrid(np.linspace(0, 1, image_shape[1]), 
                       np.linspace(0, 1, image_shape[0]))

    grad = x * np.random.uniform(-1, 1) + y * np.random.uniform(-1, 1)

    return grad

if __name__ == '__main__':
    grad = make_random_gradient((64, 80, 3))

In particular, you should note the name of the function make_random_gradient. This function produces images similar to the following one.

enter image description here

In the past, I followed a computer vision course. I've also looked at the images in the Wikipedia article related to the image gradient. In a way, the function make_random_gradient seems to generate "random image gradients", even though they are not black and white, like the images in the linked Wikipedia article. I am looking for a rigorous and mathematical explanation of why the function above generates a "random image gradient" and precisely in which sense it generates a "gradient".

Furthermore, the "gradients" generated by this function are used to augment images. In particular, if x is an RGB image with shape (64, 80, 3), each channel i of x is augmented, in my case, in the following way

x[:, :, i] = (1 - alpha) * x[:, :, i] + alpha * grad

where alpha = np.random.uniform(0.05, 0.15) and grad is an object returned by make_random_gradient.

So, each channel is augmented with the grad using a convex combination. Why would one do this?

I am aware of certain image augmentation techniques, such as cropping, flipping, the addition of Gaussian noise, but I've never seen this technique. Has anyone ever seen it? If yes, why would one augment images in this way? I am looking for papers or, in general, works that I've used a similar technique and that provide a rationale behind it.

I've also quickly skimmed through the relevant pages of the paper A survey on Image Data Augmentation for Deep Learning, but the paper is a little bit too long.


It's not generating a gradient in the computer vision / image processing sense but in the graphics community sense. For example, if you use Gimp's gradient tool you will fill images with values interpolated between 2 ends.

My guess regarding the augmentation is that the author is trying to learn invariance to some shading effects (eg., when the sun makes an angle with object in the scene and you can see walls or other surfaces becoming gradually darker).

| improve this answer | |
  • $\begingroup$ I've noticed that the operation above of applying the "gradient" produces dark images. $\endgroup$ – nbro Feb 26 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.