123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- # main imports
- import numpy as np
- import sys
- # image transform imports
- from PIL import Image
- from skimage import color
- from sklearn.decomposition import FastICA
- from sklearn.decomposition import IncrementalPCA
- from sklearn.decomposition import TruncatedSVD
- from numpy.linalg import svd as lin_svd
- from scipy.signal import medfilt2d, wiener, cwt
- import pywt
- import cv2
- from ipfml.processing import transform, compression, segmentation
- from ipfml import utils
- # modules and config imports
- sys.path.insert(0, '') # trick to enable import of main folder module
- import custom_config as cfg
- from modules.utils import data as dt
- def get_svd_data(data_type, block):
- """
- Method which returns the data type expected
- """
- if 'filters_statistics' in data_type:
- img_width, img_height = 200, 200
- lab_img = transform.get_LAB_L(block)
- arr = np.array(lab_img)
- # compute all filters statistics
- def get_stats(arr, I_filter):
- e1 = np.abs(arr - I_filter)
- L = np.array(e1)
- mu0 = np.mean(L)
- A = L - mu0
- H = A * A
- E = np.sum(H) / (img_width * img_height)
- P = np.sqrt(E)
- return mu0, P
- stats = []
- kernel = np.ones((3,3),np.float32)/9
- stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
- kernel = np.ones((5,5),np.float32)/25
- stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 0.5)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1.5)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 0.5)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1)))
- stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1.5)))
- stats.append(get_stats(arr, medfilt2d(arr, [3, 3])))
- stats.append(get_stats(arr, medfilt2d(arr, [5, 5])))
- stats.append(get_stats(arr, wiener(arr, [3, 3])))
- stats.append(get_stats(arr, wiener(arr, [5, 5])))
- wave = w2d(arr, 'db1', 2)
- stats.append(get_stats(arr, np.array(wave, 'float64')))
- data = []
- for stat in stats:
- data.append(stat[0])
- for stat in stats:
- data.append(stat[1])
-
- data = np.array(data)
- return data
- def w2d(arr, mode='haar', level=1):
- #convert to float
- imArray = arr
- np.divide(imArray, 255)
- # compute coefficients
- coeffs=pywt.wavedec2(imArray, mode, level=level)
- #Process Coefficients
- coeffs_H=list(coeffs)
- coeffs_H[0] *= 0
- # reconstruction
- imArray_H = pywt.waverec2(coeffs_H, mode)
- imArray_H *= 255
- imArray_H = np.uint8(imArray_H)
- return imArray_H
- def _get_mscn_variance(block, sub_block_size=(50, 50)):
- blocks = segmentation.divide_in_blocks(block, sub_block_size)
- data = []
- for block in blocks:
- mscn_coefficients = transform.get_mscn_coefficients(block)
- flat_coeff = mscn_coefficients.flatten()
- data.append(np.var(flat_coeff))
- return np.sort(data)
|