import matplotlib.pyplot as plt
import numpy as np
import cv2 #OpenCv
Read in the display the image
# Read in the image
image = cv2.imread('image_name.jpg')
#print out the type of image data and its dimension
print('This image is :',type(image),
'with dimensions', image.shape)
%matplotlib inline
plt.imashow(image)
此时你会发现背景可能是红色,不是预料的蓝色,这是因为OpenCv会把彩色图像读取成BGR(蓝绿红)图像
image_copy = np.copy(image) #make a copy
# 改成RGB
image_copy = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB)
plt.imshow(image_copy)
定义颜色阈值
#defin![微信截图_20180804114738.png](https://img.haomeiwen.com/i8789591/7f216140fd83cc01.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
e our color selection boundaries in RGB value
lower_blue = np.array([0,0,230])
upper_blue = np.array([100,150,255])
Create mask #把我们选定的感兴趣区域分离出来以便进行操作
mask= cv2.inRange(image_copy, lower_blue,upper_blue) # mask 会显示出在范围内的像素
# Vizualize the mask
plt.imshow(mask, cmap ='gray')
# 展示主体
masked_image = np.copy(image_copy)
masked_image[mask != 0] = [0,0,0]
# Mask and add a background image
background_image = cv2.imread('image.jpg')
background_image = cv2.cvtColor(background_image,cv2.COLOR_BGR2RGB)
crop_background = background_image(0:514,0:816) #调整图片尺寸
crop_background[mask == 0]= [0,0,0]
complete_image = crop_background + masked_image
plt.imshow(complete_image)
网友评论