Opencv-python:输入图像的类型应该是CV_8UC3或CV_8UC4!在函数fastNlMeansDenoisingColored

时间:2017-06-17 06:37:21

标签: python opencv ubuntu

我写了一个简单的opencv程序,它可以减少图像的噪音。

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('stream/horse.png')

dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)

cv2.imshow(dst)
plt.subplot(121), plt.imshow(img)
plt.subplot(122), plt.imshow(dst)
plt.show()

horse.png

当我运行该程序时,我给出了一个错误。这...

OpenCV Error: Bad argument (Type of input image should be CV_8UC3 or CV_8UC4!) in fastNlMeansDenoisingColored, file /home/govinda/github_repos/opencv/modules/photo/src/denoising.cpp, line 176
Traceback (most recent call last):
  File "/home/govinda/workspace-python/opencv/src/Smle.py", line 7, in <module>
    dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
cv2.error: /home/govinda/github_repos/opencv/modules/photo/src/denoising.cpp:176: error: (-5) Type of input image should be CV_8UC3 or CV_8UC4! in function fastNlMeansDenoisingColored

我该如何过来?

2 个答案:

答案 0 :(得分:3)

你需要像这样转换图像:

converted_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

为你提供帮助有点晚,但我希望能帮助别人。

干杯!

答案 1 :(得分:0)

它是* .png还是* .jpg文件都没有关系。 Matplotlib也给您不同的结果。尝试以下代码:

# fastNlMeansDenoisingColored
# Wait for result, takes time to respond
import cv2
from tkinter import filedialog
from tkinter import *

root = Tk()
# Do not show graphics window
root.withdraw()

# Load the original color image
origColorImage = cv2.imread(filedialog.askopenfilename(),1)

# Image must have 3 channels
print("Shape of image ", origColorImage.shape)

dest = cv2.fastNlMeansDenoisingColored(origColorImage,None,10,10,7,21)

cv2.imshow('Original image',origColorImage)
cv2.imshow('fastNlMeansDenoisingColored',dest)