Unwanted image shape cv2

Question:

iam trying to generate images and train a neural network,
i make these images via making an empty np array
image = np.empty((400, 400), np.int32) to make the shape on it

image = cv2.circle(image, (CordX, CordY), rad, color, thickness)

I don’t know how to make it 3 dimensional (image.shape op currently is 400, 400) and the output should be 400, 400, 3

Asked By: redfface

||

Answers:

To convert gray image to RGB, you can simply repeat one channel 3 times:

# Create additional axis
image = image[:, :, None]
print(image.shape) # (400, 400, 1)

# Repeat 3 times along axis 2
image = image.repeat(3, 2)
print(image.shape) # (400, 400, 3)

Full example:

image = np.empty((400, 400), np.int32)
image = cv2.circle(image, (200, 200), 20, 100, 3)
image = image[:, :, None].repeat(3, 2)

import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()

Resulting image

Alternatively, you could create RGB image in the first place:

image = np.empty((400, 400, 3), np.int32)
image = cv2.circle(image, (200, 200), 20, (100, 100, 100), 3)

This is the same image as above.

Answered By: hocop
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.