簡體   English   中英

如何將 x 和 y 像素的顏色更改為藍色?

[英]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)

這給出了 output:

在此處輸入圖像描述

如何將這些黑色像素的 colors 更改為藍色? 如果我做

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

它給出了一個錯誤,說"setting an array element with a sequence."

您的circle不是 3D 數組,而是二維數組( (400, 400) ),因此沒有 RGB 維度。 嘗試使其成為 3D 陣列。

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

然后,你將能夠做到

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

因為你的陣列是二維的,所以你的 colors 只能是灰度的。 太修復它,為數組的形狀添加另一個維度:

import numpy as np
import cv2

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

使用 numpy arrays,您可以使用逗號進行嵌套切片:

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)

最后,不要做np.ones((400, 400, 3)) * 255 ,你可以做np.full((400, 400, 3), 255)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM