# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import cv2
import numpy as np
from matplotlib import pyplot as plt
from reprep.graphics import scale, posneg;

def show(x):
    print('Image shape: %s' % str(x.shape))
    plt.imshow(x, interpolation='nearest')
    plt.show()



# -

filename = 'data/funky_rgb.png'
im1_bgr = cv2.imread(filename)
im1_rgb_full = cv2.cvtColor(im1_bgr, cv2.COLOR_BGR2RGB)
show(im1_rgb_full)

# downsample to make operations obvious
H, W = 128,128
im1_rgb = cv2.resize(im1_rgb_full, (W, H)) 
show(im1_rgb)

n = 3
kernel = np.ones((n, n), np.float32)/ (n*n)
print kernel

np.ones((3,3)) / 9

smoothed = cv2.filter2D(im1_rgb, -1, kernel)

show(smoothed)

dx = np.array([
    [-1, 0, +1],
    [-2, 0, +2],
    [-1, 0, +1],
])
print dx

dy = dx.transpose()
print dy


smoothed = smoothed.astype('float32')
gx = cv2.filter2D(smoothed, -1, dx)
gy = cv2.filter2D(smoothed, -1, dy)

print gx.shape

gx_red = gx[:,:,2]

show(posneg(gx_red))

grayscale = np.sum(smoothed, axis=2)


show(grayscale)

grayscale_gx = cv2.filter2D(grayscale, -1, dx)
grayscale_gy = cv2.filter2D(grayscale, -1, dy)

show( np.hstack((grayscale_gy, grayscale_gx)))

strength = grayscale_gx * grayscale_gx + grayscale_gy * grayscale_gy

show(strength) 

strength_y =  grayscale_gy * grayscale_gy

show(strength_y)

gx_squashed = np.sum(gx, axis=2)
gy_squashed = np.sum(gy, axis=2)
print gx_squashed.shape

show(posneg(np.hstack((gx_squashed, gy_squashed))))

strength = gx*gx + gy*gy

show(scale(strength))



StarcoderdataJupyterScriptsDedupFiltered