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
import logging
from collections import defaultdict, namedtuple
from image_processing.img_util import show_image
DSIZE = 4
In [ ]:
fnames = glob.glob("/hackzurich2017/raw_data/segmentation_examples/*/*.jpg")
assert len(fnames) > 0
Label = namedtuple('Label', ['index', 'name'])
LABELS = {
0: Label(0, 'grain'),
1: Label(1, 'straw'),
2: Label(2, 'pumpking_seeds'),
3: Label(3, 'red_kidney_beans'),
4: Label(4, 'stones'),
5: Label(5, 'quinoa'),
6: Label(6, 'black_lentils'),
7: Label(7, 'dried_garden_beans'),
8: Label(8, 'fines'),
}
def path_to_label(fname):
dirname = os.path.dirname(fname)
label_dir = os.path.basename(dirname)
label = int(label_dir.split("_")[0])
return label
label_to_fnames = defaultdict(list)
for fname in fnames:
label = path_to_label(fname)
label_to_fnames[label].append(fname)
In [ ]:
def threshold_image(img, conversion, channel_idx, lower_thresh):
# lower is 120 for grain images?
img_lab = cv2.cvtColor(img, conversion)
channel = img_lab.copy()[:,:,channel_idx]
threshed = channel.copy()
cv2.threshold(channel, lower_thresh, 255, cv2.THRESH_BINARY, threshed)
return threshed
In [ ]:
def get_object_contours(img, mask):
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3, 3))
denoised = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# show_image(denoised, "denoised")
inverted = 255 - denoised
# show_image(inverted, "inverted")
_, contours, _ = cv2.findContours(inverted, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_overlay = img.copy()
contour_overlay[:] = [0, 0, 0]
cv2.drawContours(contour_overlay, contours, -1, (0,255,0), 3)
show_image(contour_overlay, side=DSIZE)
# todo: return contours of type X
In [ ]:
for fname in label_to_fnames[0]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2Lab, 2, 135)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[1]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2Lab, 2, 125)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[2]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2Lab, 2, 120)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[3]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2XYZ, 2, 120)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[4]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2HLS, 0, 75)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[5]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2YCrCb, 0, 130)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[6]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2Lab, 0, 80)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[7]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2XYZ, 2, 90)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
for fname in label_to_fnames[8]:
print fname
img = io.imread(fname)
show_image(fname, side=DSIZE)
mask = threshold_image(img, cv2.COLOR_RGB2HLS, 0, 90)
# show_image(mask, side=DSIZE)
get_object_contours(img, mask)
In [ ]:
if False:
for fname in label_to_fnames[8]:
img = io.imread(fname)
show_image(img, fname, side=6)
for i, ccv in enumerate([
cv2.COLOR_RGB2GRAY,
cv2.COLOR_RGB2HLS,
cv2.COLOR_RGB2HSV,
cv2.COLOR_RGB2Lab,
cv2.COLOR_RGB2Luv,
cv2.COLOR_RGB2XYZ,
cv2.COLOR_RGB2YCrCb,
cv2.COLOR_RGB2YUV,
]):
print "************"
converted = cv2.cvtColor(img, ccv)
if len(converted.shape) == 3:
for c in range(converted.shape[2]):
show_image(converted[:, :, c],
"conversion {}, channel {}".format(i, c),
6)
else:
show_image(converted, "conversion {}".format(i), 6)