我知道如何使用C++和OpenCV迭代遍历像素并访问它们的值。现在,我正在尝试学习Python,并尝试用Python做同样的事情。但是当我运行以下代码时,显示图像需要很长时间(约7-10秒),即使在显示图像后,脚本仍然持续运行几秒钟。
我在SO上找到了一个类似的问题(在这里),但我不知道在我的情况下如何使用numpy(因为我是Python的初学者),也不知道是否真的需要使用numpy?
代码解释:我只是试图在图像的左右两侧放置黑色像素。
import numpy as np
import cv2 as cv
#reading an image
img = cv.imread('image.jpg')
height, width, depth = img.shape
for i in range(0, height):
for j in range(0, (width/4)):
img[i,j] = [0,0,0]
for i in range(0, height):
for j in range(3*(width/4), width):
img[i,j] = [0,0,0]
cv.imshow('image',img)
cv.waitKey(0)
img[:,:width/4,:] = 0
,对于另一边缘,img[:,-width/4:,:] = 0
。 - Ericimg[:,np.r_[:width/4,-width/4:],:] = 0
。 - Ericimg[0:height, 0:width//4, 0:depth] = 0
,0
可以是一个自定义函数来应用于每个像素吗? - Alaa M.