如何在python中使用opencv提取绿色通道?

时间:2019-01-07 06:08:04

标签: python opencv3.0 extraction

要从RGB图像中提取绿色通道,我已经完成了以下代码,但这不起作用

import cv2
green_image = cv2.imread('eye.jpg')
green_image[:,:,0] = 0
green_image[:,:,2] = 0
cv2.imshow('G-RGB', green)
cv2.waitKey(0)

代码已引发错误:

TypeError

   1 import cv2
   2 green_image = cv2.imread('eye.jpg')
-->3 green_image[:,:,0] = 0
   4 green_image[:,:,2] = 0
   5 cv2.imshow('G-RGB', green)

TypeError: 'NoneType' object does not support item assignment

2 个答案:

答案 0 :(得分:0)

更改此

Ctrl+C

cv2.imshow('G-RGB', green)

因此:

cv2.imshow('G-RGB', green_image)

输出: geen

答案 1 :(得分:0)

您可以使用cv2.split拆分蓝色,绿色和红色通道。

import cv2
img = cv2.imread('eye.jpg')
b, g, r = cv2.split(img)
cv2.imshow('green', g)
cv2.waitKey(0)