In [56]:
%matplotlib inline

import cv2
import matplotlib.pyplot as plt

In [57]:
bgr = cv2.imread("image077.png")
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
plt.imshow(rgb)


Out[57]:
<matplotlib.image.AxesImage at 0x7fbb4f701f90>

In [58]:
r = rgb[:, :, 0]
g = rgb[:, :, 1]
b = rgb[:, :, 2]

In [59]:
field = (g > 50) & (r < 50) & (b < 100)
plt.imshow(field, cmap='gray')


Out[59]:
<matplotlib.image.AxesImage at 0x7fbb4f701510>

convert BGR image to HSV

In HSV, it is more easier to represent a color than RGB color-space.

For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].


In [60]:
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)

In [62]:
# define range of blue color in HSV
lower_green = np.array([50,50,50])
upper_green = np.array([100,255,255])

In [63]:
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_green, upper_green)

In [64]:
# Bitwise-AND mask and original image
res = cv2.bitwise_and(rgb, rgb, mask=mask)

In [65]:
plt.imshow(res)


Out[65]:
<matplotlib.image.AxesImage at 0x7fbb4f573bd0>

In [ ]: