我试图使用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接受narray,您可以使用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对象)。