In [ ]:
import glob
import os
import matplotlib.pyplot as plt
import skimage
import numpy as np
import itertools
import scipy.misc
from skimage import io
import time
import cv2
def show_image(image, desc=None, side=12):
if desc:
print desc
plt.figure(figsize=(side * 4.0 / 3, side), dpi=80)
io.imshow(image)
plt.show()
In [ ]:
clean_photos = glob.glob("/hackzurich2017/raw_data/clean/*.jpg")
dirty_photos = glob.glob("/hackzurich2017/raw_data/dirt/*.jpg")
In [ ]:
def get_images(photos, show=False):
images = {}
for i, photo_fname in enumerate(photos):
description = "{}: {}".format(i, photo_fname)
photo = cv2.imread(photo_fname)
if show:
show_image(photo, description, side=3)
images[photo_fname] = photo
return images
In [ ]:
clean = get_images(clean_photos)
dirty = get_images(dirty_photos)
In [ ]:
img = dirty['/hackzurich2017/raw_data/dirt/20170916-112421.jpg']
show_image(img, 'test', side=10)
In [ ]:
# this mask is good for counting hay
mask = np.argmax(img, axis=2) == 1
idx = (mask==0)
cimg = np.copy(img)
cimg[idx]=0
show_image(cimg, 'green', side=10)
In [ ]:
grey_straw = cv2.cvtColor(cimg, cv2.COLOR_RGB2GRAY)
ret, th_straw = cv2.threshold(grey_straw, 120, 255, cv2.THRESH_BINARY)
th_straw = -th_ste
# denoise
th_lentils = cv2.fastNlMeansDenoising(th_lentils, None, 70, 21, 11)
show_image(th_straw, 'straw', side=10)
In [ ]:
# greyscale
grey = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
# adaptive threshold
th2 = cv2.adaptiveThreshold(grey,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY_INV,15,3)
# denoise
th2 = cv2.fastNlMeansDenoising(th2,None,100, 21, 11)
# binary thresh after denoising
ret, th2 = cv2.threshold(th2, 90, 255, cv2.THRESH_BINARY)
# make all borders white. why? because otherwise you cant detect edge blobs
# this is the most fucking annoying behavior ever
th2[:, 0:1] = 255
th2[:, th2.shape[1] - 1:] = 255
th2[0:1, :] = 255
th2[th2.shape[0] - 1:, :] = 255
show_image(th2, 'thresh', side=21)
In [ ]:
# params object for blob detector
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
params.maxArea = 500000
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.2
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.2
# Filter by color
params.filterByColor = False
# Filter by inertia
params.filterByInertia = False
#params.minThreshold = 5
#params.thresholdStep = 500
params.minDistBetweenBlobs = 0
params.minRepeatability = 5
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(th2)
len(keypoints)
In [ ]:
# show points on thresh
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 10), dpi=80)
cimg = cv2.cvtColor(th2,cv2.COLOR_GRAY2RGB)
for k in keypoints:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),5)
cimg[rr, cc] = [255, 0, 0]
io.imshow(cimg)
plt.show()
In [ ]:
# on original image
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 10), dpi=80)
cimg = np.copy(img)
for k in keypoints:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),7)
cimg[rr, cc] = [0,0, 255]
io.imshow(cimg)
plt.show()
In [ ]:
show_image(grey, 'intens', side=21)
In [ ]:
# quinoa
ret, th_quinoa = cv2.threshold(grey, 150, 230, cv2.THRESH_BINARY)
# denoise
th_quinoa = cv2.fastNlMeansDenoising(th_quinoa,None, 51, 21, 11)
# binary thresh after denoising
ret, th_quinoa = cv2.threshold(th_quinoa, 90, 255, cv2.THRESH_BINARY)
th_quinoa = 255 - th_quinoa
show_image(th_quinoa, 'quinoa', side=21)
In [ ]:
# params object for blob detector
params_quinoa = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params_quinoa.filterByArea = True
params_quinoa.minArea = 70
params_quinoa.maxArea = 200
# Filter by Circularity
params_quinoa.filterByCircularity = True
params_quinoa.minCircularity = 0.7
# Filter by Convexity
params_quinoa.filterByConvexity = False
params_quinoa.minConvexity = 0.2
# params.minThreshold = 5
# params.thresholdStep = 500
params_quinoa.minDistBetweenBlobs = 10
params_quinoa.minRepeatability = 0
detector_quinoa = cv2.SimpleBlobDetector_create(params_quinoa)
keypoints_quinoa = detector_quinoa.detect(th_quinoa)
len(keypoints_quinoa)
In [ ]:
# show points on thresh
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 10), dpi=80)
cimg = cv2.cvtColor(th_quinoa, cv2.COLOR_GRAY2RGB)
for k in keypoints_quinoa:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),3)
cimg[rr, cc] = [255, 0, 0]
io.imshow(cimg)
plt.show()
In [ ]:
# on original image
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 15), dpi=80)
cimg = np.copy(img)
for k in keypoints_quinoa:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),3)
cimg[rr, cc] = [255 ,0, 0]
io.imshow(cimg)
plt.show()
In [ ]:
# black lentils
#th_lentils = np.clip(grey, 0, 50)
ret, th_lentils = cv2.threshold(-grey, 180, 255, cv2.THRESH_BINARY)
# denoise
th_lentils = cv2.fastNlMeansDenoising(th_lentils, None, 70, 21, 11)
# binary thresh after denoising
ret, th_lentils = cv2.threshold(th_lentils, 190, 255, cv2.THRESH_BINARY)
th_lentils = (255 - th_lentils).astype('uint8')
show_image(th_lentils, 'lentils', side=21)
In [ ]:
# params object for blob detector
params_lentils = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params_lentils.filterByArea = True
params_lentils.minArea = 100
params_lentils.maxArea = 1000
# Filter by Circularity
params_lentils.filterByCircularity = True
params_lentils.minCircularity = 0.3
# Filter by Convexity
params_lentils.filterByConvexity = False
params_lentils.minConvexity = 0.2
# params.minThreshold = 5
# params.thresholdStep = 500
# params_lentils.minDistBetweenBlobs = 0
# params_lentils.minRepeatability = 100
detector_lentils = cv2.SimpleBlobDetector_create(params_lentils)
keypoints_lentils = detector_lentils.detect(th_lentils)
len(keypoints_lentils)
In [ ]:
# show points on thresh
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 10), dpi=80)
cimg = cv2.cvtColor(th_lentils, cv2.COLOR_GRAY2RGB)
for k in keypoints_lentils:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),3)
cimg[rr, cc] = [255, 0, 0]
io.imshow(cimg)
plt.show()
In [ ]:
# on original image
from skimage.draw import circle
plt.figure(figsize=(10 * 4.0 / 3, 10), dpi=80)
cimg = np.copy(img)
for k in keypoints_lentils:
rr, cc = circle(int(k.pt[1]), int(k.pt[0]),3)
cimg[rr, cc] = [255, 0, 0]
io.imshow(cimg)
plt.show()
In [ ]: