def motion_blur1(image, degree=10, angle=45): image = np.array(image) # 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高 M = cv.getRotationMatrix2D((degree / 2, degree / 2), angle, 1) motion_blur_kernel = np.diag(np.ones(degree)) motion_blur_kernel = cv.warpAffine(motion_blur_kernel, M, (degree, degree)) motion_blur_kernel = motion_blur_kernel / degree blurred = cv.filter2D(image, -1, motion_blur_kernel) # convert to uint8 cv.normalize(blurred, blurred, 0, 255, cv.NORM_MINMAX) blurred = np.array(blurred, dtype=np.uint8) 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 = cv.imread("./deblur/"+i) img_ = motion_blur1(img) cv.imwrite(outPath,img_),上述代码出现问题:cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'filter2D' > Overload resolution failed: > - src data type = 17 is not supported > - Expected Ptrcv::UMat for argument 'src'
时间: 2024-03-21 11:39:20 浏览: 104
weizhi.zip_blur image_距离 角度_距离模糊_运动 模糊_运动模糊
5星 · 资源好评率100%
在你的代码中,img变量的类型是一个普通的numpy数组,它的数据类型为uint8。为了解决这个问题,你可以将img变量转换为OpenCV的UMat类型,这是OpenCV中的一种特殊类型,它可以更好地与OpenCV的函数一起使用。
你可以使用cv2.UMat()函数将numpy数组转换为UMat类型。修改代码如下:
```
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 = cv.imread("./deblur/"+i)
img = cv.UMat(img) # 将numpy数组转换为UMat类型
img_ = motion_blur1(img)
cv.imwrite(outPath,img_)
```
这样应该就能解决上述错误了。如果还有问题,请提供更多的代码和错误信息,以便我们更好地帮助你解决问题。
阅读全文