Writing Synthetic Data generator using Opencv

Source: Deep Learning on Medium

We will start coding by importing necessary modules —

# author - Vipul Vaibhawimport cv2
import numpy as np
import random

Now we need a blank image, basically a numpy array which we will be using to generate images.

height = 500
width = 500
blank_image = np.zeros((height,width,3), np.uint8)

Okay, now if you do a cv2.imshow here. You will see a black image because all the pixel values are zero.

blank image.

Great! Our drawing board is ready. We now need to tinker with pixel values and channels to get the type of image we want.

Obviously here, I cannot show the images we got from the plant. The images were close-up shots of a crankshaft which looked green under some lights and chemicals. The scratches on those crankshafts looked a bit like silverish lines.

Okay, then let’s pick the second channel of the image (RGB or BGR, green is always the second channel) and modify it.

There were two types of images which we needed, one was with grainy background and the other one was with smooth background.

for row in blank_image[:,:width,1]:
for element in range(len(row)):
row[element] = random.choice([100,255])

We iterated through the second channel and randomly assign pixel values to 100(dark green) and 255(light green) to get grainy effect.

grainy background

To get the clean background, we can simply draw a circle with center at the center of the image and radius big enough to encompass all the image.

cv2.circle(blank_image, (250,250), 400, (0,100,0), thickness=-1, lineType=8, shift=0)
clean background

Now, we observed that on grainy background the scratch used to look whitish however on clean background scratches were silverish.

num_scratches=random.randint(0,5)for _ in range(num_scratches):
row_random = random.randint(150,400)
blank_image[row_random: (row_random+1),row_random(row_random+random.randint(25,75))] = (192,192,192)
clean background images with scratches

To generate scratches on noisy background, we generated white scratches.

noisy background with scratches

Great! Now we were able to generate data. We separated the data into two folders i.e defected and good. Now we had an ability to generate as much data as we want. This data was then used to do the training of neural networks to do the classification whether an image has scratches or not.

Tip — use cv2.createCLAHE() to get luminous effects to your images.