Skip to content

Instantly share code, notes, and snippets.

@ksoda
Created May 4, 2016 14:57
Show Gist options
  • Save ksoda/06efa6c1749747ba9b8343e3584c444c to your computer and use it in GitHub Desktop.
Save ksoda/06efa6c1749747ba9b8343e3584c444c to your computer and use it in GitHub Desktop.
OpenCV
import cv2, matplotlib
import numpy as np
import matplotlib.pyplot as plt
bgr_img = cv2.imread('images/landscape.jpg')
img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
blue_min = np.array([100, 0, 0], np.uint8)
blue_max = np.array([140, 255, 255], np.uint8)
wh_min = np.array([0, 0, 180], np.uint8)
wh_max = np.array([180, 60, 255], np.uint8)
blue = cv2.inRange(img, blue_min, blue_max)
white = cv2.inRange(img, wh_min, wh_max)
inv_mask = cv2.bitwise_or(white, blue)
mask = cv2.bitwise_not(inv_mask)
plt.imshow(mask, cmap=plt.get_cmap('gray'))
mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
masked_rgb = cv2.bitwise_and(cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB), mask_rgb)
plt.imshow(masked_rgb)
@ksoda
Copy link
Author

ksoda commented May 4, 2016

apt-get install python-opencv python-matplotlib 
ipython notebook --pylab=inline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment