In [221]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [222]:
img = cv2.cvtColor(cv2.imread("../videos/frames/frame_1.png"),cv2.COLOR_BGR2RGB)
plt.imshow(img)
Out[222]:
In [254]:
hsv = cv2.cvtColor(cv2.imread("../videos/frames/frame_1.png"),cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv,np.asarray([0,0,0]),np.asarray([180,255,60]))
out = cv2.bitwise_and(hsv,hsv,mask=mask)
m = mask[250:410,300:550]
median = cv2.medianBlur(m,3)
plt.imshow(median,cmap="gray")
Out[254]:
In [211]:
img = cv2.imread("../videos/frames/frame_1.png")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
rois = [(300,480,300,530),(400,470,210,300),(235,300,530,610)]
image = hsv.copy()
background = []
for roi in rois:
ymin,ymax,xmin,xmax = roi
cv2.rectangle(image,(xmin,ymin),(xmax,ymax),(255,0,0),1)
pixels = hsv[ymin:ymax,xmin:xmax]
w,h,d = pixels.shape
background.append(pixels.reshape(w*h,1,d))
fig, ax = plt.subplots(1,figsize=(8,8))
ax.imshow(image)
Out[211]:
In [219]:
img = cv2.imread("../videos/frames/frame_1.png")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
rois = [(300,480,300,530),(400,470,210,300),(235,300,530,610)]
image = hsv.copy()
background = []
for roi in rois:
ymin,ymax,xmin,xmax = roi
cv2.rectangle(image,(xmin,ymin),(xmax,ymax),(255,0,0),1)
pixels = hsv[ymin:ymax,xmin:xmax]
w,h,d = pixels.shape
background.append(pixels.reshape(w*h,1,d))
background = np.concatenate(background)
hist = cv2.calcHist([background],[0,1,2],mask = None, histSize = [8,8,2], ranges = [0, 180, 0, 255, 0, 255])
print background.shape[0],hist.sum()
print hist.max(),hist.min()
hist = cv2.normalize(hist,hist,0,1,cv2.NORM_MINMAX)
print hist.max(),hist.min()
np.prod(hist.shape)
Out[219]:
In [220]:
hist # we want to divide the new image into the same sets of bins, then for each
Out[220]:
In [ ]:
fig,ax = plt.subplots(1,2,figsize=(20,10))
print hist.dtype
# are probabilities being clipped inside calcbackproject?
# TODO write your own back project to compare ...
dst = cv2.calcBackProject([orig.astype('float32')],[0,1,2],hist,[0, 180, 0, 256,0,256],scale=1.0)
ax[0].imshow(dst,cmap="gray")
print dst.min(),dst.max(),dst.dtype
dst = cv2.normalize(dst,dst,0,255,cv2.NORM_MINMAX).astype('uint8')
#_,th3 = cv2.threshold(dst,254,255,cv2.THRESH_BINARY_INV)
ret3,th3 = cv2.threshold(dst,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
res = cv2.bitwise_and(hsv,hsv, mask=th3)
rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
#ax[0].imshow(rgb,interpolation="nearest")
#ax[1].imshow(cv2.bitwise_and(rgb,rgb,mask=th3))
In [175]:
original = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
rois = [(250,350,0,180),(168,250,0,40),(170,190,40,165),(25,35,15,180)]
image = original.copy()
background = []
for roi in rois:
ymin,ymax,xmin,xmax = roi
cv2.rectangle(image,(xmin,ymin),(xmax,ymax),(255,0,0),1)
pixels = fridge[ymin:ymax,xmin:xmax]
w,h,d = pixels.shape
background.append(pixels.reshape(w*h,1,d))
background = np.concatenate(background)
hist = cv2.calcHist([background],[0,1,2],mask = None, histSize = [8,8,2], ranges = [0, 180, 0, 255, 0, 255])
print background.shape[0]
print hist.sum()
In [174]:
np.prod([2,3])
Out[174]:
In [84]:
print hist.max(),hist.min()
hist = cv2.normalize(hist,hist,0,255,cv2.NORM_MINMAX)
print hist.max(),hist.min()
fig,ax = plt.subplots(1,2,figsize=(20,10))
print hist.dtype
# are probabilities being clipped inside calcbackproject?
# TODO write your own back project to compare ...
dst = cv2.calcBackProject([fridge.astype('float32')],[0,1,2],hist,[0, 180, 0, 256,0,255],scale=1.0)
ax[0].imshow(dst)
print dst.min(),dst.max(),dst.dtype
dst = cv2.normalize(dst,dst,0,255,cv2.NORM_MINMAX).astype('uint8')
#_,th3 = cv2.threshold(dst,254,255,cv2.THRESH_BINARY_INV)
ret3,th3 = cv2.threshold(dst,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
res = cv2.bitwise_and(fridge,fridge, mask=th3)
fig,ax = plt.subplots(1,2,figsize=(20,10))
rgb = cv2.cvtColor(fridge,cv2.COLOR_HSV2RGB)
ax[0].imshow(rgb,interpolation="nearest")
ax[1].imshow(cv2.bitwise_and(rgb,rgb,mask=th3))
Out[84]:
In [ ]:
rois = [(0,0,250,30),(0,150,250,180),(0,150,250,180),(0,150,250,180)]
# plot all the images with the regions we selected as containing grass (note they are all the same shape for convinient stacking)
fig,ax = plt.subplots(2,len(images),figsize = (20,5))
# make an hsv image containing all the grass regions we have selected.
grass = np.vstack((image[ymin:ymax,xmin:xmax] for ((xmin,ymin,xmax,ymax),image) in zip(rois,hsv_images) ))
hist = cv2.calcHist([grass],[0,1],mask = None, histSize = [8,8], ranges = [0, 180, 0, 256])
cv2.normalize(hist,hist,0,1,cv2.NORM_MINMAX)
for indx,image in enumerate(rgb):
hsv = hsv_images[indx]
xmin,ymin,xmax,ymax = rois[indx]
cv2.rectangle(image,(xmin,ymin),(xmax,ymax),(255,0,0),1)
ax[0,indx].imshow(image)
dst = cv2.calcBackProject([hsv],[0,1],hist,[0, 180, 0, 256],scale=255)
ax[1,indx].imshow(dst,cmap="gray")
fig,ax = plt.subplots()
ax.imshow(hist,interpolation="nearest",cmap="gray")