【问题标题】:passing PIL image to OpenCV causes TypeError: Expected cv::UMat for argument 'src'将 PIL 图像传递给 OpenCV 导致 TypeError: Expected cv::UMat for argument 'src'
【发布时间】:2021-12-09 19:14:43
【问题描述】:

我正在尝试将使用 OpenCV 库的 CLAHE 方法应用于已拆分的绿色通道,但随后出现此错误:

 cl = clahe.apply(multiBands[1])

TypeError: Expected cv::UMat for argument 'src'

我的代码:

# Example Python Program for contrast stretching

from PIL import Image
import cv2 as cv

 
# Method to process the green band of the image

def normalizeGreen(intensity):

    iI      = intensity
    minI    = 90
    maxI    = 225
    minO    = 0
    maxO    = 255
    iO      = (iI-minI)*(((maxO-minO)/(maxI-minI))+minO)

    return iO

# Create an image object
imageObject     = Image.open("input/IDRiD_02.jpg")

# Split the red, green and blue bands from the Image
multiBands      = imageObject.split()

# create a CLAHE object (Arguments are optional)
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl = clahe.apply(multiBands[1])

# Apply point operations that does contrast stretching on each color band
normalizedGreenBand    = cl.point(normalizeGreen)

# Display the image before contrast stretching
imageObject.show()
 
# Display the image after contrast stretching
normalizedGreenBand.show()

【问题讨论】:

  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
  • 您将 PIL 图像传递给 OpenCV 函数。不支持。
  • OpenCV 的 python 绑定经常吐出这个消息。对于给定的参数,可以接受各种类型,但“UMat”是该参数列表中的 last 类型......它只报告该参数,而不是其他选项。
  • PIL 和 OpenCV 有不同的图像格式。您必须从一种格式转换为另一种格式。
  • @DanMašek 感谢您的回答,顺便问一下。在opencv中,有没有像poin()这样的函数?

标签: python numpy opencv computer-vision python-imaging-library


【解决方案1】:

PIL 和 OpenCV 都有不同格式的图像对象,但它们可以互换,例如OpenCV 接受 ndarray,您可以使用 NumPy 将 PIL 图像更改为数组。同样,您可以使用 PIL.Image.fromarray() 方法从 numpy 数组中获取 PIL 图像对象。我已经修改了你的代码,我认为它会解决你的问题。

from PIL import Image
import cv2 as cv
import numpy as np

# Method to process the green band of the image
def normalizeGreen(intensity):
    minI, maxI = 90, 225
    minO, maxO = 0, 255
    iO = (intensity-minI)*(((maxO-minO)/(maxI-minI))+minO)
    return iO

# Create an image object
# imageObject = Image.open("<path-to-image>")
imageObject = Image.open("images/13/img-r1-000.jpg")

# Split the red, green and blue bands from the Image
r, g, b = imageObject.split()

# Apply point operations that does contrast stretching on each color band
normalizedGreenBand = g.point(normalizeGreen)

# convert the Pil Image object to OpenCv image object
normalizedGreenBand = np.array(normalizedGreenBand)

# create a CLAHE object (Arguments are optional)
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
normalizedGreenBand = clahe.apply(normalizedGreenBand)

# Display the image before contrast stretching
imageObject.show()

# convert the OpenCv image object to Pil Image object
normalizedGreenBand = Image.fromarray(normalizedGreenBand)
# Display the image after contrast stretching
normalizedGreenBand.show()

我首先在绿色通道上应用point() 方法,然后将归一化的绿色通道转换为 NumPy 数组,以便应用cv.createCLAHE() 操作。最后,我将 NumPy 数组重新转换为 PIL 图像对象进行显示(我们可以使用cv2.imshow() 而不转换为 PIL 对象)。

【讨论】:

  • 感谢您的回答!这对我有很大帮助。我可以再问一次吗?你知道OpenCV中像PIL这样的点操作功能吗?
猜你喜欢
  • 2019-06-12
  • 1970-01-01
  • 2020-10-03
  • 2019-08-03
  • 2021-05-29
  • 2021-04-15
  • 2021-01-29
  • 2019-12-13
相关资源
最近更新 更多