import cv2 import numpy as np import os def motion_blur1(image, degree=10, angle=45): image = np.array(image) # 生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高 M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1) motion_blur_kernel = np.diag(np.ones(degree)) motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree)) motion_blur_kernel = motion_blur_kernel / degree # 将UMat类型的图像转换为numpy数组类型的图像 blurred = cv2.filter2D(image.get(), -1, motion_blur_kernel) # convert to uint8 cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX) blurred = np.array(blurred, dtype=np.uint8) # 将numpy数组类型的图像转换为UMat类型的图像 blurred = cv2.UMat(blurred) return blurred filePath = r'D:\Users\Administrator\PycharmProjects\pythonProject\paper_need\blur' for i in os.listdir(filePath): outPath = r'D:\Users\Administrator\PycharmProjects\pythonProject\paper_need\blur.' + str(i) img = cv2.imread("./deblur/"+i) img = cv2.UMat(img) img_ = motion_blur1(img) cv2.imwrite(outPath,img_.get()),上述代码出现问题: 'numpy.ndarray' object has no attribute 'get'
时间: 2024-03-21 15:39:25 浏览: 77
这个问题是由于UMat类型的图像没有get()方法导致的。可以使用UMat类型的图像本身进行处理,无需调用get()方法。
这里是修改后的代码:
```python
import cv2
import numpy as np
import os
def motion_blur1(image, degree=10, angle=45):
image = np.array(image)
# 生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
motion_blur_kernel = np.diag(np.ones(degree))
motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
motion_blur_kernel = motion_blur_kernel / degree
# 将UMat类型的图像直接进行处理
blurred = cv2.filter2D(image, -1, motion_blur_kernel)
# convert to uint8
cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
blurred = np.array(blurred, dtype=np.uint8)
blurred = cv2.UMat(blurred)
return blurred
filePath = r'D:\Users\Administrator\PycharmProjects\pythonProject\paper_need\blur'
for i in os.listdir(filePath):
outPath = r'D:\Users\Administrator\PycharmProjects\pythonProject\paper_need\blur.' + str(i)
img = cv2.imread("./deblur/"+i)
img = cv2.UMat(img)
img_ = motion_blur1(img)
cv2.imwrite(outPath,img_.get())
```
这里直接对UMat类型的图像进行处理,无需调用get()方法。
阅读全文