- import
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
- read
img = cv2.imread(imagename,cv2.IMREAD_COLOR)
// cv2.IMREAD_COLOR - load in bgr color mode
// cv2.IMREAD_GRAY - load in grayscale mode
// cv2.IMREAD_UNCHANGED - load original image (with alpha channel)
- convert and resize
img = cv2.cvtColor(semantic_mask, cv2.COLOR_BGR2RGB)
h,w, _ = img.shape
img_resize = cv2.resize(img, (w,h), interpolation=cv2.INTER_CUBIC)
- show
plt.figure()
plt.imshow(img)
img_gray= cv2.cvtColor(semantic_mask, cv2.COLOR_RGB2GRAY)// convert to gray image
plt.figure()
plt.imshow(img_gray,cmap='gray')
- inRange
color_lbound = np.array([2,100,200]) -20
color_hbound = np.array([2,100,200])+20
mask = cv2.inRange(semantic_mask, color_lbound, color_hbound)
image[mask>0] = new_color
- threshold and bit operation
ret, mask = cv2.threshold(img_gray, 1, 200, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img_bg = cv2.bitwise_and(bg,bg,mask = mask_inv)
img_fg = cv2.bitwise_and(fg,fg,mask = mask)
网友评论