简体   繁体   中英

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

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:

在此处输入图像描述

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."

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM