In [ ]:
# Basic routines to display numpy array as image
from io import BytesIO
import PIL.Image as PILImage 
from IPython.display import display
from IPython.display import Image as displayImage
import cv2
import numpy as np
from ipython_slideatlas import display_img_array

In [ ]:
scale = 15
delta = 0
kernel_size = 5
blur_scale = 5
blur = False

ddepth = cv2.CV_16S


inp = cv2.imread('/home/dhan/Downloads/muscle_matrix.png')
assert inp is not None 
if blur:
    img = cv2.GaussianBlur(img,(blur_scale,blur_scale),0)

In [ ]:
display_img_array(inp)
gray = cv2.cvtColor(inp,cv2.COLOR_BGR2GRAY)

# Gradient-X
grad_x = cv2.Sobel(gray,ddepth,1,0,ksize = kernel_size, scale = scale, delta = delta,borderType = cv2.BORDER_DEFAULT)
#grad_x = cv2.Scharr(gray,ddepth,1,0)

# Gradient-Y
grad_y = cv2.Sobel(gray,ddepth,0,1,ksize = kernel_size, scale = scale, delta = delta, borderType = cv2.BORDER_DEFAULT)
#grad_y = cv2.Scharr(gray,ddepth,0,1)
from numpy import square, sqrt
import numpy

def gamma_correction(img, correction):
    img = img/255.0
    img = cv2.pow(img, correction)
    return np.uint8(img*255)

grad_x = grad_x.astype(numpy.float32)
grad_y = grad_y.astype(numpy.float32)

data = sqrt(grad_x * grad_x + grad_y * grad_y)
#data
# Try gamma correction
rescaled = (255.0 / data.max() * (data - data.min()))
#gamma = gamma_correction(rescaled, 0.5)
display_img_array(rescaled)
from math import atan2
#print grad_x.shape
orientation = numpy.arctan(grad_x, grad_y)
#print orientation.size
hue = (128.0 / orientation.max() * (orientation - orientation.min()))
# print type(hue)
# saturation = numpy.array(hue.shape)
saturation = numpy.full(hue.shape, 230)
value = rescaled
img = numpy.dstack((hue,saturation, value))
img2 = img.astype(numpy.uint8)
img3 = cv2.cvtColor(img2, cv2.COLOR_HSV2RGB)
display_img_array(img3)

In [ ]: