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

load filenames


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)

remove background

helper functions


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

0: grain


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)

1: straw


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)

2. pumpkin seed


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)

3. red kidney beans


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)

4. stone


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)

5. quinoa


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)

6. black lentil


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)

7. dried garden bean


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)

8. fines


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)

prototyping: display all channels


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)