How to change the color of the x and y pixels to blue?

Viewed 44
import numpy as np
import cv2

img = np.ones((400, 400,3),np.uint8)*255

circle = np.ones((400, 400))*255  
for theta in np.arange(0, 2*np.pi, 0.01):
    for r in np.arange(150, 160, 1):
        x = 200 + r * np.cos(theta)
        y = 200 + r * np.sin(theta)

        circle[int(x)][int(y)] = 0
cv2.imshow('c',circle)

This gives the output:

enter image description here

How do I change the colors of those black pixels to blue? If I do

circle[int(x)][int(y)] = (255,0,0)

It gives an error saying "setting an array element with a sequence."

2 Answers

Your circle is not a 3D array but a 2D array ((400, 400)) so there is no RGB dimension. Try to make it a 3D array.

circle = np.ones((400, 400, 3))*255  

And then, you will be able to do

circle[int(x)][int(y)] = [255,0,0]

Because your array is 2D, your colors can only be gray-scale. Too fix it, add another dimension to the shape of the array:

import numpy as np
import cv2

img = np.ones((400, 400, 3), np.uint8) * 255
circle = np.ones((400, 400, 3)) * 255  

With numpy arrays, you can use a comma for nested slicing:

for theta in np.arange(0, 2*np.pi, 0.01):
    for r in np.arange(150, 160, 1):
        x = 200 + r * np.cos(theta)
        y = 200 + r * np.sin(theta)
        circle[int(x), int(y)] = 255, 0, 0

cv2.imshow('c',circle)

Finally, instead of doing np.ones((400, 400, 3)) * 255, you can do np.full((400, 400, 3), 255)

Related