Python 3.x 与以下openCV代码等效
我正在尝试将以下用openCV编写的代码模块转换为pillow,但我无法确定该怎么做? j是rgb图像Python 3.x 与以下openCV代码等效,python-3.x,opencv,python-imaging-library,Python 3.x,Opencv,Python Imaging Library,我正在尝试将以下用openCV编写的代码模块转换为pillow,但我无法确定该怎么做? j是rgb图像 img = cv2.imread(j,1) b,g,r = cv2.split(img) green = 2*g-r-b 在这里,我正在读取一个彩色图像,然后将其拆分为多个通道,然后在绿色通道上执行转换,然后进一步使用绿色通道进行进一步操作,但我无法计算上述代码的等效值。 我试过这个问题,但我无法得到2*g-r-b的结果。你可以这样使用PIL和Numpy来实现这一点-我倾向于使用Numpy,
img = cv2.imread(j,1)
b,g,r = cv2.split(img)
green = 2*g-r-b
在这里,我正在读取一个彩色图像,然后将其拆分为多个通道,然后在绿色通道上执行转换,然后进一步使用绿色通道进行进一步操作,但我无法计算上述代码的等效值。我试过这个问题,但我无法得到
2*g-r-b
的结果。你可以这样使用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、抱枕、颜色矩阵、颜色矩阵、变换、倍增通道、缩放通道、单独、单独、单独通道、波段、组件、单独、图像、图像处理。您可以这样使用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、枕头、颜色矩阵、颜色矩阵、变换、倍增通道、缩放通道、单独、单独、单独通道、波段、组件、单独、图像、,图像处理。可能重复的我尝试过上述问题,但无法对其执行操作
2*g-r-b
可能重复的我尝试过上述问题,但无法对其执行操作2*g-r-b
是,它解决了我的问题,但是否有任何方法,我不必每次都保存图像,并且可以使用特定的颜色通道带进行进一步处理。因为在openCV中,我可以使用单独的波段(假设在本例中为绿色),并且可以在不保存任何内容的情况下对其执行操作。你不必保存任何东西。我只是出于礼貌展示了如何重新组合频道,因为我将它们分开,并猜测您可能想要重新组合它们,但您不必这样做。还要注意,我使用的Numpy数组(imnp
、r
、g
和b
)完全相同,并且可以与OpenCVimg
、b
、g
和r
完全互换。您可以将我的数组与OpenCV代码一起使用,也可以将OpenCV数组与我的Numpy/PiIL代码一起使用!是的,我知道了。非常感谢。是的,它解决了我的问题,但是否有任何方法,我不必每次都保存图像,并可以使用特定的颜色通道带进行进一步处理。因为在openCV中,我可以使用单独的波段(假设在本例中为绿色),并且可以在不保存任何内容的情况下对其执行操作。你不必保存任何东西。我只是出于礼貌展示了如何重新组合频道,因为我将它们分开,并猜测您可能想要重新组合它们,但您不必这样做。还要注意,我使用的Numpy数组(imnp
、r
、g
和b
)完全相同,并且可以与OpenCVimg
、b
、g
和r
完全互换。您可以将我的数组与OpenCV代码一起使用,也可以将OpenCV数组与我的Numpy/PiIL代码一起使用!是的,我知道了。谢谢。