首页 > 解决方案 > 输入图像类型上的 opencv 错误应为 CV_8UC3 或 CV_8UC4

问题描述

我尝试了opencv 示例中的以下代码段,

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('die.png')
dst = cv.fastNlMeansDenoisingColored(img,None,10,10,7,21)
plt.subplot(121),plt.imshow(img)
plt.subplot(122),plt.imshow(dst)
plt.show()

但是我收到以下错误,他的根本原因是什么以及如何解决?

错误 Traceback (last last call last) in 3 from matplotlib import pyplot as plt 4 img = cv.imread('die.png') ----> 5 dst = cv.fastNlMeansDenoisingColored(img,None,10,10,7 ,21) 6 plt.subplot(121),plt.imshow(img) 7 plt.subplot(122),plt.imshow(dst)

error: OpenCV(4.1.1) ..\modules\photo\src\denoising.cpp:178: error: (-5:Bad argument) Type of input image should be CV_8UC3 or CV_8UC4! in function 'cv::fastNlMeansDenoisingColored'

标签: opencvimage-processingcomputer-vision

解决方案


cv::fastNlMeansDenoisingColored期望 3 或 4 个通道的 8 位图像。在调用 fastNlMeansDenoisingColored 函数之前尝试将图像转换为 uint8

img=np.uint8(img)

推荐阅读