In [1]:
import os
import sys
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
from sklearn.model_selection import train_test_split
from itertools import chain
from skimage.io import imread, imshow, concatenate_images
from skimage.transform import resize
from skimage.morphology import label
from keras.models import Model, load_model
from keras.layers import Input,Dropout,BatchNormalization,Activation
from keras.layers.core import Lambda
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras import backend as K
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
In [2]:
basicpath = 'data/'
path_train = basicpath + 'train/'
path_test = basicpath + 'test/'
path_train_images = path_train + 'images/'
path_train_masks = path_train + 'masks/'
path_test_images = path_test + 'images/'
imgs = os.listdir(path_train_images)
ids = [i.replace('.png','') for i in imgs ]
ids
Out[2]:
In [3]:
for _id in ids:
image_gay = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
image_matplotlib = imread(path_train_images+_id+".png")
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Original Image - keras', fontsize=10)
ax2.imshow(image_matplotlib , cmap='gray')
ax2.set_title('Original Image - matplotlib', fontsize=10)
ax3.imshow(image_gay , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [4]:
hist, bins = np.histogram(image_matplotlib)
hist_k, bins_k = np.histogram(image_keras)
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 6))
f.tight_layout()
ax1.fill(hist)
ax1.set_title('image_matplotlib', fontsize=10)
f.tight_layout()
ax2.fill(hist_k)
ax2.set_title('image_keras', fontsize=10)
plt.show()
In [5]:
img = mpimg.imread(path_train_images+"1306fcee4c.png")
type(img) , img.shape
Out[5]:
In [6]:
mask = mpimg.imread(path_train_masks+"1306fcee4c.png")
mask
type(mask) , mask.shape
Out[6]:
In [7]:
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
image_matplotlib = imread(path_train_images+_id+".png")
dx = cv2.Sobel(np.array(image_keras), cv2.CV_64F, 1, 0)
dy = cv2.Sobel(np.array(image_keras), cv2.CV_64F, 0, 1)
f, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(dx)
ax2.set_title(r'$\frac{dI}{dx}$', fontsize=10)
ax3.imshow(dy)
ax3.set_title(r'$\frac{dI}{dy}$', fontsize=10)
ax4.imshow(image_gray , cmap='gray')
ax4.set_title('Gray Image - input', fontsize=10)
ax5.imshow(mask , cmap='gray')
ax5.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [8]:
def abs_sobel_thresh(img, orient='x', thresh_min=150, thresh_max=255):
img = np.array(img)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Apply x or y gradient with the OpenCV Sobel() function
# and take the absolute value
if orient == 'x':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
if orient == 'y':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
# Rescale back to 8 bit integer
scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
# Create a copy and apply the threshold
binary_output = np.zeros_like(scaled_sobel)
# Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
binary_output[(scaled_sobel >= thresh_min) & (scaled_sobel <= thresh_max)] = 1
# Return the result
return binary_output
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
dx = abs_sobel_thresh(image_keras,orient='x', thresh_min=40, thresh_max=300)
dy = abs_sobel_thresh(image_keras,orient='y', thresh_min=40, thresh_max=300)
f, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(dx, cmap='gray')
ax2.set_title(r'$\frac{dI}{dx}$', fontsize=10)
ax3.imshow(dy, cmap='gray')
ax3.set_title(r'$\frac{dI}{dy}$', fontsize=10)
ax4.imshow(image_gray , cmap='gray')
ax4.set_title('Gray Image - input', fontsize=10)
ax5.imshow(mask , cmap='gray')
ax5.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [9]:
# Default pipeline.
def pipeline_default(img, s_thresh=(170, 255), sx_thresh=(20, 100)):
img = np.copy(img)
# Convert to HSV color space and separate the V channel
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float)
l_channel = hsv[:,:,1]
s_channel = hsv[:,:,2]
# Sobel x
sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0) # Take the derivative in x
abs_sobelx = np.absolute(sobelx) # Absolute x derivative to accentuate lines away from horizontal
scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx))
# Threshold x gradient
sxbinary = np.zeros_like(scaled_sobel)
sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1
# Threshold color channel
s_binary = np.zeros_like(s_channel)
s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
# Stack each channel
# Note color_binary[:, :, 0] is all 0s, effectively an all black image. It might
# be beneficial to replace this channel with something else.
color_binary = np.dstack(( np.zeros_like(sxbinary), sxbinary, s_binary))
# Combine the two binary thresholds
combined_binary = np.zeros_like(sxbinary)
combined_binary[(s_binary == 1) | (sxbinary == 1)] = 1
return color_binary , combined_binary
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
color_binary , combined_binary = pipeline_default(np.array(image_keras))
f, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(color_binary, cmap='gray')
ax2.set_title('color_binary', fontsize=10)
ax3.imshow(~combined_binary, cmap='gray')
ax3.set_title('combined_binary', fontsize=10)
ax4.imshow(image_gray , cmap='gray')
ax4.set_title('Gray Image - input', fontsize=10)
ax5.imshow(mask , cmap='gray')
ax5.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [24]:
def gradmag(img, sobel_kernel=3, mag_thresh=(0, 255)):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Take both Sobel x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Calculate the gradient magnitude
gradmag = np.sqrt(sobelx**2 + sobely**2)
# Rescale to 8 bit
scale_factor = np.max(gradmag)/255
gradmag = (gradmag/scale_factor).astype(np.uint8)
return ~gradmag
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
image_matplotlib = imread(path_train_images+_id+".png")
dir_binary = gradmag(np.array(image_matplotlib))
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(dir_binary , cmap='gray')
ax2.set_title('dir_binary', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [17]:
dir_binary.shape
Out[17]:
In [11]:
# Define a function to threshold an image for a given range and Sobel kernel
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
# Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Calculate the x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Take the absolute value of the gradient direction,
# apply a threshold, and create a binary image result
absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
binary_output = np.zeros_like(absgraddir)
binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1
# Return the binary image
return binary_output
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
mag_binary = dir_threshold(np.array(image_keras), sobel_kernel=15, thresh=(0.7, 1.3))
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(mag_binary, cmap='gray')
ax2.set_title('mag_binary', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [18]:
mag_binary.shape
Out[18]:
In [12]:
KSIZE = 11
ALPHA = 2
kernel = cv2.getGaussianKernel(KSIZE, 0)
kernel = -ALPHA * kernel @ kernel.T
kernel[KSIZE//2, KSIZE//2] += 1 + ALPHA
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
filtered = cv2.filter2D(np.array(image_keras), -1, kernel)
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(filtered, cmap='gray')
ax2.set_title('filtered', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [13]:
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
fft = cv2.dft(cv2.imread(path_train_images+_id+".png", 0).astype(np.float32) / 255, flags=cv2.DFT_COMPLEX_OUTPUT)
shifted = np.fft.fftshift(fft, axes=[0, 1])
magnitude = cv2.magnitude(shifted[:, :, 0], shifted[:, :, 1])
magnitude = np.log(magnitude)
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(magnitude, cmap='gray')
ax2.set_title('magnitude', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [14]:
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))/255
mask0 = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
fft = cv2.dft(cv2.imread(path_train_images+_id+".png", 0).astype(np.float32) / 255, flags=cv2.DFT_COMPLEX_OUTPUT)
fft_shift = np.fft.fftshift(fft, axes=[0, 1])
sz = 25
mask = np.zeros(fft_shift.shape, np.uint8)
mask[mask.shape[0]//2-sz:mask.shape[0]//2+sz,
mask.shape[1]//2-sz:mask.shape[1]//2+sz, :] = 1
fft_shift *= mask
fft = np.fft.ifftshift(fft_shift, axes=[0, 1])
filtered = cv2.idft(fft, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
f, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(filtered, cmap='gray')
ax2.set_title('filtered', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask0 , cmap='gray')
ax4.set_title('Mask', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [15]:
from scipy.stats import itemfreq
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))
mask0 = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
adapt_mask = cv2.adaptiveThreshold(cv2.imread(path_train_images+_id+".png", 0), 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 11, 10)
merged = np.maximum(adapt_mask,np.array(image_gray))/255
f, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(9, 6))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(adapt_mask, cmap='gray')
ax2.set_title('adapt_mask', fontsize=10)
print("*****",_id)
print(itemfreq(adapt_mask.flatten()))
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask0 , cmap='gray')
ax4.set_title('Mask', fontsize=10)
ax5.imshow(merged , cmap='gray')
ax5.set_title('merged', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [16]:
def rle_encode(im):
'''
im: numpy array, 1 - mask, 0 - background
Returns run length as string formated
'''
pixels = im.flatten(order = 'F')
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x) for x in runs)
from scipy.stats import itemfreq
for _id in ids:
image_gray = np.array(load_img(path_train_images+_id+".png",grayscale=True))
mask0 = load_img(path_train_masks+_id+".png",grayscale=True)
image_keras = load_img(path_train_images+_id+".png")
#image_matplotlib = imread(path_train_images+_id+".png")
image = cv2.imread(path_train_images+_id+".png", 0)
_, binary = cv2.threshold(image, -1, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#print("*******************",_id)
#print(rle_encode(binary))
eroded = cv2.morphologyEx(binary, cv2.MORPH_ERODE, (3, 3), iterations=10)
dilated = cv2.morphologyEx(binary, cv2.MORPH_DILATE, (3, 3), iterations=10)
opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)),
iterations=5)
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)),
iterations=5)
grad = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT,
cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
f, (ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8) = plt.subplots(1, 8, figsize=(27, 18))
f.tight_layout()
ax1.imshow(image_keras)
ax1.set_title('Orig. Image - keras', fontsize=10)
ax2.imshow(binary, cmap='gray')
ax2.set_title('binary', fontsize=10)
ax3.imshow(image_gray , cmap='gray')
ax3.set_title('Gray Image - input', fontsize=10)
ax4.imshow(mask0 , cmap='gray')
ax4.set_title('Mask', fontsize=10)
ax5.imshow(eroded , cmap='gray')
ax5.set_title('eroded', fontsize=10)
ax6.imshow(opened , cmap='gray')
ax6.set_title('opened', fontsize=10)
ax7.imshow(closed , cmap='gray')
ax7.set_title('closed', fontsize=10)
ax8.imshow(grad , cmap='gray')
ax8.set_title('grad', fontsize=10)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [ ]: