一:ROI ROI就是region of interest,感兴趣的区域,有时候需要去看看某个特定的区域
import cv2
def cv_show_image(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('images/naruto.jpg')
part = img[0:300, 80:300]
cv_show_image('naruto', part)
part = img[200:500, 280:500]
cv_show_image('naruto', part)
b, g, r = cv2.split(img)
print(b.shape)
print(g.shape)
print(r.shape)
cv_show_image('gray image, only show B Channel', b)
cv_show_image('gray image, only show G Channel', g)
cv_show_image('gray image, only show R Channel', r)
curr_img = img.copy()
curr_img[:, :, 0] = 0
curr_img[:, :, 1] = 0
cv_show_image('colorful image, only keep R Channel data', curr_img)
curr_img = img.copy()
curr_img[:, :, 0] = 0
curr_img[:, :, 2] = 0
cv_show_image('colorful image, only keep G Channel data', curr_img)
curr_img = img.copy()
curr_img[:, :, 1] = 0
curr_img[:, :, 2] = 0
cv_show_image('colorful image, only keep B Channel data', curr_img)
二:图像填充 有时候需要对图像的边界进行填充
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('images/saoge.jpg')
cv2.imshow('img', img)
cv2.waitKey(0)
top_size, bottom_size, left_sze, right_size = (50, 50, 50, 50)
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_sze, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_sze, right_size, borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_sze, right_size, borderType=cv2.BORDER_REFLECT101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_sze, right_size, borderType=cv2.BORDER_WRAP)
content = cv2.copyMakeBorder(img, top_size, bottom_size, left_sze, right_size, borderType=cv2.BORDER_CONSTANT, value=0)
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('original image')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('replicate padding image')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('reflect padding image')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('reflect101 padding image')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('wrap padding image')
plt.subplot(236), plt.imshow(content, 'gray'), plt.title('content padding image')
plt.show()
|