我正在尝试将以下用openCV编写的代码模块转换为枕头,但我不知道该怎么做? j是一个RGB图片
img = cv2.imread(j,1)
b,g,r = cv2.split(img)
green = 2*g-r-b
在这里,我正在读取彩色图像,然后分成多个通道,然后在绿色通道上执行转换,然后进一步使用绿色通道进行进一步的操作,但是我无法计算出与上述代码等效的枕头。< br />
我尝试了这个问题Python PIL image split to RGB,但无法获得2*g-r-b
答案 0 :(得分:2)
您可以像这样使用PIL和Numpy做到这一点-我倾向于使用Numpy,因为它更快,更灵活:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Split into 3 constituent bands
r = imnp[:, :, 0]
g = imnp[:, :, 1]
b = imnp[:, :, 2]
# Process
g = 2*g - r - b
# Recombine to single image and save
merged = np.dstack((r, g, b))
Image.fromarray(merged).save('result.png')
或者您可以不太明确地将其分割并在整个图像上进行分割:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Process
imnp[:,:,1] = 2*imnp[:,:,1] - imnp[:,:,0] - imnp[:,:,2]
# Save
Image.fromarray(imnp).save('result2.png')