第一種方式:
import cv2
image = cv2.imread(‘test.jpg’) #讀取影像
image_g = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #透過轉換函式轉為灰階影像
cv2.imwrite(‘test_g.jpg’, image_g) #儲存影像
第二種方式:
import cv2
image_g = cv2.imread(‘test.jpg’, 0) #直接以灰階方式讀取影像
cv2.imwrite(‘test_g.jpg’, image_g) #儲存影像
img = cv2.imread('gray.jpg',0)
0 for gray and 1 for color
cv2.imread
load an image with three channels unless CV_LOAD_IMAGE_GRAYSCALE
is set.
>>> import cv2
>>> image = cv2.imread('foo.jpg')
>>> print image.shape
(184, 300, 3)
>>> gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
>>> print gray_image.shape
(184, 300)
>>> cv2.imwrite('gray.jpg', gray_image)