我需要对opencv图像的每个元素进行四舍五入。这是我现在的代码:
for column in img:
for row in column:
for channel in row:
channel = math.floor(channel / 20) * 20
可以,但是非常慢。有没有更有效的方法?
编辑:讨论后
req = urllib.request.urlopen(submission.url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1)
加载图像后,我将对所有元素进行铺地板。
这是我在加载图像后尝试用于np.floor的代码:
img = np.floor(img)
cv2.imshow("Image", img)
但是每当我尝试使用np.floor时,都会出现以下错误:
TypeError: Expected Ptr<cv::UMat> for argument '%s'
req = urllib.request.urlopen(submission.url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1)
尝试
req = urllib.request.urlopen(submission.url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
arr = np.floor(arr) # This is the different line
img = cv2.imdecode(arr, -1)
你得到什么?错误可能是您正在尝试np.floor
numpy无法识别或不知道如何转换的数据格式