【发布时间】:2022-01-01 10:35:01
【问题描述】:
我有一个与 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)
【问题讨论】:
-
提示:您可以使用带有起始值和步长值的切片符号。
-
您的
row.all()没有按照您的想法执行。你需要for y, row in enumerate(resized_img):/if y % 2 == 0:/
标签: python opencv matplotlib