我有一個與 cv2 相關的問題。我試圖將偶數行上的每個第 10 個像素和奇數行上的每個第 11 個像素的顏色更改為紅色。我正在嘗試選擇特定行,但我不能。請幫忙
import cv2
import matplotlib.pyplot as plt
# read image
image = cv2.imread('2161382.jpg')
im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# resize image
resized_img = cv2.resize(im_rgb,(500,500))
img_width = resized_img.shape[1]
img_height = resized_img.shape[0]
# Change individual pixel value (y,x)
resized_img[200, 10] = (255,0,0)
for row in resized_img:
if row.all() % 2 == 0:
resized_img[:,row 11] = (255,0,0)
# On your own create a cycle where you can change the color of every N-th pixel on the odd row
# and every M-th pixel on the even row to a different colour
%matplotlib notebook
plt.figure(figsize=(10,10))
plt.imshow(resized_img)
uj5u.com熱心網友回復:
您需要檢查行號。 row.all不這樣做。這有效:
# Change individual pixel value (y,x)
resized_img[200, 10] = (255,0,0)
for y,row in enumerate(resized_img):
if y % 2:
row[11] = (255,0,0)
else:
row[10] = (255,0,0)
uj5u.com熱心網友回復:
你根本不想要回圈,你想要索引。使用以下方法設定索引:
array[START:STOP:STEP]
所以你要:
# Make empty array of 4 rows, 30 columns
im = np.zeros((4,30), np.uint8)
# Start at row 0 and on every 2nd row, set every 10th pixel to 7
im[::2,::10] = 7
# Start at row 1 and on every 2nd row, set every 11th pixel to 9
im[1::2,::11] = 9
如果你有一個 3 通道的 RGB 影像,并且要設定 R、G 和 B,則需要添加第三個索引:
im[::2, ::10, :] = [255,0,0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/363520.html
