我正在尝试将以下用openCV编写的代码模块转换为枕头,但我无法理解如何做到这一点?J是一个rgb图像
img = cv2.imread(j,1)
b,g,r = cv2.split(img)
green = 2*g-r-b在这里,我读取一个彩色图像,然后分裂成通道,然后对绿色通道执行转换,然后进一步使用绿色通道进行进一步操作,但我无法计算与上述代码等价的枕头。
我试过这个问题-- Python PIL image split to RGB,但我无法得到2*g-r-b的结果
发布于 2019-01-25 11:00:52
您可以这样使用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')关键词::Python,Numpy,PIL,枕头,颜色矩阵,颜色矩阵,变换,多通道,比例通道,单独的,单独的,个别的通道,带,分量,单独的,图像,图像处理。
https://stackoverflow.com/questions/54362314
复制相似问题