passing PIL image to OpenCV causes TypeError: Expected cv::UMat for argument 'src'
passing PIL image to OpenCV causes TypeError: Expected cv::UMat for argument 'src'
我正在尝试将使用 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()
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 库的 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()
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对象)。