图像切片
python
%matplotlib inline
import cv2
import matplotlib.pyplot as plt
def imshow(img,gray=False,bgr_mode=False):
if gray:
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
plt.imshow(img,cmap="gray")
else:
if not bgr_mode:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()

python
import cv2
img = cv2.imread("zju.png")
imshow(img)
得到结果如图所示:

python
img.shape # shape 维度信息
img=cv2.resize(img,dsize=(225,225))
crop_img = img[70:170,60:165,:]
imshow(crop_img)

浅拷贝,是指当图像之间进行赋值时,图像数据并未发生复制,而是两个对象都指向同一块内存块。
深拷贝,是指新创建的图像拥有原始图像的崭新拷贝,即拷贝图像和原始图像在内存中存放在不同地方
python
import cv2
img = cv2.imread("zju.png")
# 浅拷贝
copy1 = img
# 深拷贝
copy2 = img.copy()
# 对源img进行修改,查看copy1中的图像是否有发生变化
img[200:400,200:400] = (255,0,0)
result1 = cv2.hconcat([img,copy1]) # 水平拼接两个图片
result2 = cv2.hconcat([img,copy2])
imshow(result1)
imshow(result2)
得到结果如下图:

小练习:截取car.jpg的完整车辆图片和车牌图片

python
import cv2
car = cv2.imread("car.jpg")
imshow(car)

python
imshow(car[0:350,460:1000,:])

python
imshow(car[270:300,670:790,:])
根据图中标尺得知车牌坐标:得到如下所示图像切片。
