This repository has been archived on 2021-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
ipcv/python_scripts/chessboard.py
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

47 lines
1.1 KiB
Python

import cv2
import numpy as np
import scipy.ndimage as ndi
import matplotlib.pyplot as plt
# customized imshow function
def imshow(img, cap=None):
if np.amax(img) > 255:
img = img / (np.amax(img)) * 255
img.astype(np.uint8)
fig = plt.figure(figsize=(4, 4))
if cap is not None:
plt.title(cap)
plt.imshow(img, cmap="gray")
plt.axis("off")
plt.show()
def ilpf(m, n, r):
a = np.tile(np.arange(-n / 2, n / 2), (m, 1))
b = np.tile(np.arange(-m / 2, m / 2), (n, 1)).T
return (a * a + b * b < r * r).astype(np.uint8)
def apply(img, ker):
return np.abs(np.fft.ifft2(np.fft.fftshift(np.fft.fft2(img)) * ker).real)
# generate chess board with size NxN
N = 64
a = np.tile(np.array(1), (int(N / 4), int(N / 4)))
b = np.tile(np.array(0), (int(N / 4), int(N / 4)))
img1 = np.concatenate([a, b], axis=0)
img2 = np.concatenate([b, a], axis=0)
img = np.concatenate([img1, img2], axis=1)
img = np.tile(img, (4, 4))
r, c = img.shape
ker = ilpf(r, c, 30)
res = img(*ker)
imshow(img, 'Created Image')
imshow(ker, 'ILPF Kernel')
imshow(res, 'ILPF Result')