xiaodelaoshi
xiaodelaoshi

Reputation: 631

How to generate random sine stripe on an image using python?

I have an image read in with python and I wish to add some different sine stripes on this image as a noise. I wish the frequency and the rotation degree of a sine is totally random. I tried numpy module but it might not be the model I need here.

Can anyone tell me any python module has such function to add random sine curve to a image?

The result should somewhat similar to this image below:

enter image description here

Upvotes: 2

Views: 1073

Answers (1)

xiaodelaoshi
xiaodelaoshi

Reputation: 631

Finally I find I can finish all of this using Numpy module:

def sineimg(img, color='black', linewidth=1.5, linestyle="-"):
'''
frequency = X / random.uniform(10.0, 20.0)
amplitude = randint(35, 50)
phase = random.uniform(1.0, 16.0)
rotation = random.uniform(-pi / 2, pi / 2)
'''
    #Random rotation angle
    rot = random.randint(-90, 90)
    x_ = img.shape[0] / np.cos(np.pi * rot / 180)
    X = np.linspace(-1 * x_, x_, 512)
    axes = plt.subplot(111)
    np.cos(np.pi * rot / 180)
    #Random amplitude
    amp1 = random.randint(35, 50)
    amp2 = random.randint(35, 50)
    #Random frequency
    frequency1 = X / random.uniform(10.0, 20.0)
    frequency2 = X / random.uniform(10.0, 20.0)
    #Random offset phase
    phase1 = np.pi / random.uniform(1.0, 16.0) + np.pi / 2
    phase2 = np.pi / random.uniform(1.0, 16.0) + np.pi / 3
    #random distance between line cluster
    distance = random.randint(90, 115)
    #I need roughly 8 times of them
    for i in range(8):
        Y1 = amp1 * np.sin(frequency1 + phase1) - 450 + i * distance
        Y2 = amp2 * np.sin(frequency2 + phase2) - 420 + i * distance
        x1_trans = X * np.cos(np.pi * rot / 180) - Y1* np.sin(np.pi * rot / 180)
        y1_trans = X * np.sin(np.pi * rot / 180) + Y1* np.cos(np.pi * rot / 180)
        x2_trans = X * np.cos(np.pi * rot / 180) - Y2* np.sin(np.pi * rot / 180)
        y2_trans = X * np.sin(np.pi * rot / 180) + Y2* np.cos(np.pi * rot / 180)
        #Remove label
        axes.set_xticks([])
        axes.set_yticks([])
        axes.spines['right'].set_color('none')
        axes.spines['top'].set_color('none')
        axes.spines['bottom'].set_color('none')
        axes.spines['left'].set_color('none')

        axes.plot(x1_trans, y1_trans, color = color, linewidth=linewidth, linestyle=linestyle)
        axes.plot(x2_trans, y2_trans, color = color, linewidth=linewidth, linestyle=linestyle)
    plt.imshow(img, zorder=0, extent=[148, -148, -225, 225])

Them:

img=mpimg.imread('me.jpg')
sineimg(img)

I get images like:

enter image description here

enter image description here

enter image description here

Upvotes: 2

Related Questions