Created
November 5, 2019 14:16
-
-
Save t-artikov/d960b0d324922120be294938d1065bcc to your computer and use it in GitHub Desktop.
DCT test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.fftpack import dct, idct | |
import matplotlib.pyplot as plt | |
from PIL import Image | |
import matplotlib.image | |
def dct_2d(x): | |
return dct(dct(x, axis=0, norm='ortho'), axis=1, norm='ortho') | |
def idct_2d(x): | |
return idct(idct(x, axis=0 , norm='ortho'), axis=1 , norm='ortho') | |
def compress(image, n): | |
w = np.zeros_like(image) | |
w[0:n, 0:n, :] = 1 | |
k = dct_2d(image) * w | |
return idct_2d(k) | |
def discrete(image): | |
r0 = (image[:, :, 0] > image[:, :, 1]).astype(float) * (image[:, :, 0] > image[:, :, 2]).astype(float) | |
r1 = (image[:, :, 1] > image[:, :, 0]).astype(float) * (image[:, :, 1] > image[:, :, 2]).astype(float) | |
r2 = (image[:, :, 2] > image[:, :, 0]).astype(float) * (image[:, :, 2] > image[:, :, 1]).astype(float) | |
return np.dstack([r0, r1, r2]) | |
image = matplotlib.image.imread("1.png") | |
for i in range(1, 200): | |
result = discrete(compress(image, i)) | |
matplotlib.image.imsave(f"v/{i:03}.png", result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment