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 namedtuple

from image_processing.img_util import show_image

load filenames


In [ ]:
single_grains = glob.glob("/hackzurich2017/raw_data/single_grains/*.jpg")
single_dirt = glob.glob("/hackzurich2017/raw_data/single_dirt/*.jpg")

remove background


In [ ]:
Label = namedtuple('Label', ['index', 'name', 'conversion', 'channel_idx', 'lower_thresh'])
LABELS = {
    0: Label(0, 'grain', cv2.COLOR_RGB2Lab, 2, 120),
    1: Label(1, 'straw', cv2.COLOR_RGB2Lab, 2, 120),
    2: Label(2, 'pumpking_seeds', cv2.COLOR_RGB2Lab, 1, 130),
    3: Label(3, 'red_kidney_beans', cv2.COLOR_RGB2Lab, 1, 130),
#     4: Label(4, 'stones', 2, 120),
    5: Label(5, 'quinoa', cv2.COLOR_RGB2Lab, 2, 120),
#     6: Label(6, 'black_lentils', 0, 70), # shitty
#     7: Label(7, 'dried_garden_beans', 2, 113),  # kind of shitty
    8: Label(8, 'fines', cv2.COLOR_RGB2Lab, 2, 120),
}

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 [ ]:
for fname in single_dirt:
    img = io.imread(fname)
    show_image(img, fname, 6)
    show_image(threshold_image(img), side=6)
    
for fname in single_grains:
    img = io.imread(fname)
    show_image(img, fname, 6)
    show_image(threshold_image(img), side=6)

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)


for fname in [single_dirt[0], single_dirt[4], single_dirt[7]] + single_grains:
    img = io.imread(fname)
    show_image(img, fname)
    get_object_contours(img)

In [ ]:
current = single_dirt[1]
img = io.imread(current)
show_image(img, side=6)

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)

show_image(th2)

In [ ]:
## black lentil
current = single_dirt[5]
img = io.imread(current)
show_image(img, side=6)

converted = cv2.cvtColor(img, cv2.COLOR_RGB2Lab)
single_channel = converted[:, :, 0]

th2 = cv2.adaptiveThreshold(single_channel, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                        cv2.THRESH_BINARY_INV, 83, 21)

show_image(th2, side=6)


# for t in range(0, 256, 10):
#     threshed = threshold_image(
#         img,
#         cv2.COLOR_RGB2Lab,
#         0,
#         t
#     )
#     show_image(threshed, str(t), 6)

In [ ]:
## garden bean
current = single_dirt[6]
print current
img = io.imread(current)
show_image(img, side=6)

converted = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
single_channel = converted[:, :, 2]

th2 = cv2.adaptiveThreshold(single_channel, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                        cv2.THRESH_BINARY_INV, 81, 14)
show_image(th2, "%d %d" % (b,c), side=6)

In [ ]:
current = single_dirt[3]
img = io.imread(current)
show_image(img)

converted = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
show_image(converted)



for b, c in itertools.product(range(3, 204, 10), range(5, 105, 10)):
    th2 = cv2.adaptiveThreshold(converted, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                        cv2.THRESH_BINARY_INV, 81, 14)
    show_image(th2, "%d %d" % (b,c), side=6)

In [ ]:
for tricky_label in [3, 5, 6]:
    current = single_dirt[tricky_label]
    img = io.imread(current)
    show_image(img, current, 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)

In [ ]: