# -*- coding: utf-8 -*-
"""
Created on Mon Aug 21 12:03:49 2017

@author: Eliot

individual cells
"""

# method based upon cropping image into individual cells and determining shadow
# regions from them that way.


# 0. import necessary libraries

import os
import numpy as np
import cv2
import resize

# 1. Read in input image

thisfilepath = os.path.dirname(__file__)
loaddirpath = os.path.abspath(os.path.join(thisfilepath, "test_files/orig"))
savedirpath = os.path.abspath(os.path.join(thisfilepath, "test_files/indiv_cells/001/"))
resize.ensure_dir(savedirpath + "/111.png")
image = cv2.imread(loaddirpath + "/001CROP11-17-59.jpg")

# 2. Resize image (currently 2x increase)

resized = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_LANCZOS4)

# Crop

y, x, z = resized.shape

mod_dimensions = (8, 11)
mod_dim_yx = mod_dimensions[::-1]

yratio, xratio = y/mod_dim_yx[0], x/mod_dim_yx[1]
print(yratio, "\n", xratio)

for rows in range(mod_dim_yx[0]):
    for cols in range(mod_dim_yx[1]):
        cropped = resized[int(yratio * rows):int(yratio * (rows+1)),
                          int(xratio * cols):int(xratio * (cols+1))]
        cv2.imshow("cell", cropped)
        cv2.imwrite(savedirpath + "/001" + "_ROW" + str(rows) + "_COL" + str(cols) + ".png", cropped)
        if cv2.waitKey(0) & 0xff == 27:
            cv2.destroyAllWindows()