import numpy as np
import cv2
import scipy.linalg as la
import matplotlib.pyplot as plt
import sys
import tqdm
# Good resources:
# https://docs.opencv.org/3.4/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de
# https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html
# https://theailearner.com/tag/hysteresis-thresholding/
noise
30 / 80
thresh
22 / 100
sigma
10 / 100
img = cv2.cvtColor(cv2.imread("/work/2x2Rubiks_2.png"), cv2.COLOR_BGR2GRAY)[400:850:4, 300:750:4]
fig, ax = plt.subplots(2, 3, figsize=(7*3,7*2))
ax[0,0].imshow(img, cmap='gray')
# https://docs.opencv.org/3.4/d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d
imgf = img.astype(np.float32)/255
Gx = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32))
Gy = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32).T)
Gmag = np.sqrt(Gx**2 + Gy**2)
ax[0,1].imshow(Gmag, cmap='gray')
ax[0,2].imshow(Gmag > (thresh/100), cmap='gray')
gx_ax = ax[1,0].imshow(Gx)
fig.colorbar(gx_ax, ax=ax[1,0], orientation='vertical')
gy_ax = ax[1,1].imshow(Gy)
fig.colorbar(gy_ax, ax=ax[1,1], orientation='vertical')
img_noise = (255*np.clip(imgf + (noise/255)*np.random.rand(*imgf.shape), 0, 1)).astype(np.uint8)
fig, ax = plt.subplots(2, 3, figsize=(7*3,7*2))
ax[0,0].imshow(img_noise, cmap='gray')
imgf = img_noise.astype(np.float32)/255
Gx = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32))
Gy = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32).T)
Gmag = np.sqrt(Gx**2 + Gy**2)
ax[0,1].imshow(Gmag, cmap='gray')
ax[0,2].imshow(Gmag > (thresh/100), cmap='gray')
gx_ax = ax[1,0].imshow(Gx)
fig.colorbar(gx_ax, ax=ax[1,0], orientation='vertical')
gy_ax = ax[1,1].imshow(Gy)
fig.colorbar(gy_ax, ax=ax[1,1], orientation='vertical')
print(sigma/100)
img_filt = cv2.GaussianBlur(img_noise, (5, 5), (sigma/10))
fig, ax = plt.subplots(2, 3, figsize=(7*3,7*2))
ax[0,0].imshow(img_filt, cmap='gray')
imgf = img_filt.astype(np.float32)/255
Gx = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32))
Gy = cv2.filter2D(imgf, cv2.CV_32F, np.array([[-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0], [-1.0, 0.0, 1.0]], dtype=np.float32).T)
Gmag = np.sqrt(Gx**2 + Gy**2)
ax[0,1].imshow(Gmag, cmap='gray')
ax[0,2].imshow(Gmag > (thresh/100), cmap='gray')
gx_ax = ax[1,0].imshow(Gx)
fig.colorbar(gx_ax, ax=ax[1,0], orientation='vertical')
gy_ax = ax[1,1].imshow(Gy)
fig.colorbar(gy_ax, ax=ax[1,1], orientation='vertical')
thresh1
31 / 255
thresh2
176 / 255
img = cv2.cvtColor(cv2.imread("/work/2x2Rubiks_2.png"), cv2.COLOR_BGR2GRAY)[400:850:4, 300:750:4]
fig, ax = plt.subplots(1, 3, figsize=(7*3,11))
ax[0].imshow(img, cmap='gray')
# https://docs.opencv.org/3.4/d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d
imgf = img.astype(np.float32)/255
Gx = cv2.Sobel(imgf, cv2.CV_32F, 1, 0)
Gy = cv2.Sobel(imgf, cv2.CV_32F, 0, 1)
ax[1].imshow(np.sqrt(Gx**2 + Gy**2), cmap='gray')
ax[2].imshow(cv2.Canny(img, thresh1, thresh2), cmap='gray')