【发布时间】:2014-10-18 22:23:19
【问题描述】:
我知道使用 OpenCV 和 C++ 遍历像素并访问它们的值。现在,我正在尝试自己学习 python,并且我尝试在 python 中做同样的事情。但是当我运行以下代码时,显示图像需要很长时间(~7-10 秒)。即使在显示图像后,脚本也会继续运行几秒钟。
我发现了一个类似的问题here at SO,但我无法理解在我的情况下如何使用 numpy(因为我是 python 的初学者)以及它是否真的需要?
代码说明:我只是想把黑色像素放在图像的左右两侧。
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)
【问题讨论】: