如何对大型数组的每个元素进行铺底?

问题描述 投票:0回答:1

我需要对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'
python arrays opencv
1个回答
0
投票
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无法识别或不知道如何转换的数据格式

© www.soinside.com 2019 - 2024. All rights reserved.