【问题标题】:Opencv-python: Type of input image should be CV_8UC3 or CV_8UC4! in function fastNlMeansDenoisingColoredOpencv-python:输入图像的类型应该是 CV_8UC3 或 CV_8UC4!在函数中 fastNlMeansDenoisingColored
【发布时间】:2017-06-17 06:37:21
【问题描述】:

我写了一个简单的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

我该如何克服这个问题?

【问题讨论】:

  • 您的链接是 jpeg 图片,而不是 png(我猜它已被转换)。所以很难调试出了什么问题。我会检查print img.shape 以查看通道数是3 还是4(同时检查图像是否读取成功)。

标签: python opencv ubuntu


【解决方案1】:

你需要像这样转换图像:

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

帮助你有点晚了,但我希望它对某人有所帮助。

干杯!

【讨论】:

  • 您好,我尝试了您的建议,但出现以下错误,错误:OpenCV(4.1.1) ..\modules\imgproc\src\color.cpp:182: error: (-215 :Assertion failed) !_src.empty() in function 'cv::cvtColor'
  • 您好,当图像文件不存在或无法打开时,我会出现此错误。
【解决方案2】:

不管是 *.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)

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 2017-08-27
    • 2020-05-09
    • 2013-02-26
    • 1970-01-01
    • 2016-01-03
    • 2021-02-01
    • 2015-01-26
    • 2018-09-25
    相关资源
    最近更新 更多