Backto OpenCV Index
引用的 package
首先, 必须引的包
# must
import cv2
import numpy as np
# highly used
from matplotlib import pyplot as plt
图像文件操作
# READ
img = cv2.imread('messi5.jpg')
# WRITE
# Shape of image is accessed by img.shape.
# It returns a tuple of number of rows, columns and channels (if image is color)
>>> print img.shape
(342, 548, 3)
# Total number of pixels is accessed by img.size:
>>> print img.size
562248
# Image datatype is obtained by img.dtype:
>>> print img.dtype
uint8
像素级操作
# accessing pixel
>>> px = img.item(10, 10)
>>> px
[101, 38, 59] # BGR order
# accessing RED value
>>> img.item(10,10,2)
59
# modifying RED value
>>> img.itemset((10,10,2),100)
>>> img.item(10,10,2)
100
# pick the blue channel
>>> b = img[:,:,0]
# remove the red channel
>>> img[:,:,2] = 0
ROI 操作
# ROI is again obtained using Numpy indexing.
# Here I am selecting the ball and copying it to another region in the image:
>>> ball = img[280:340, 330:390]
>>> img[273:333, 100:160] = ball