def test_translationpositive1_2(self): image = np.array(io.imread('images/sample1024x1024.jpg'), dtype=np.float) tf_image = idct2(translation(dct2(image), 100, axis=1)) image[:, 100:] = image[:, :-100] image[:, :100] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_translationnegative1_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) tf_image = idct2(translation(dct2(image), -100, axis=1)) image[:, :-100] = image[:, 100:] image[:, -100:] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_translationnegative0_2(self): image = np.array(io.imread('images/sample1024x1024.jpg'), dtype=np.float) tf_image = idct2(translation(dct2(image), -100, axis=0)) image[:-100, :] = image[100:, :] image[-100:, :] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_mask_small_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) ulx, lrx, uly, lry = 104, 112, 104, 112 tf_image = idct2(mask(dct2(image), ulx=ulx, lrx=lrx, uly=uly, lry=lry)) image[ulx:lrx, uly:lry] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_mask_horizontal_2(self): image = np.array(io.imread('images/sample1024x1024.jpg'), dtype=np.float) ulx, lrx, uly, lry = 105, 108, 200, 304 tf_image = idct2(mask(dct2(image), ulx=ulx, lrx=lrx, uly=uly, lry=lry)) image[ulx:lrx, uly:lry] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_dct2_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) dct_image = dct2(image) blocks = skimage.util.view_as_blocks(image, (8, 8)) blocks[:] = dctn(blocks, axes=(2, 3), norm='ortho') self.assertTrue(np.max(np.abs(image - dct_image)) < 1e-10)
def test_mask_small_1(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) for _ in range(100): original_image = image.copy() ulx = random.randint(0, image.shape[0] - 1) uly = random.randint(0, image.shape[1] - 1) lrx = random.randint(ulx + 1, image.shape[0]) lry = random.randint(uly + 1, image.shape[1]) tf_image = idct2( mask(dct2(image), ulx=ulx, lrx=lrx, uly=uly, lry=lry)) original_image[ulx:lrx, uly:lry] = 0 self.assertTrue(np.max(np.abs(original_image - tf_image)) < 1e-10)
def _translation(image, sigma): im = image.copy() if im.shape[1] <= np.abs(sigma): im[:] = 0 return im im[:, sigma // 8 * 8:] = im[:, :im.shape[1] - sigma // 8 * 8] im[:, :sigma // 8 * 8] = 0 sigma = sigma % 8 # Vの定義 V1 = np.zeros((8, 8), dtype=np.float) # V[sigma] V2 = np.zeros((8, 8), dtype=np.float) # V[w-sigma] for y in range(8 - sigma): V1[y, sigma + y] = 1 V1 = dct2(V1) for y in range(sigma): V2[8 - sigma + y, y] = 1 V2 = dct2(V2) im[:, :8] = np.dot(im[:, :8], V1) for j in range(im.shape[1] // 8 - 1, 0, -1): im[:, j * 8:j * 8 + 8] = np.dot(im[:, j * 8:j * 8 + 8], V1) + np.dot( im[:, j * 8 - 8:j * 8], V2) return im
def test_rotate90_1(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) tf_image = idct2(rotate90(dct2(image))) image = np.rot90(image, k=1) self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_rotate270_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) tf_image = idct2(rotate270(dct2(image))) image = np.rot90(image, k=3) self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_rotate180_2(self): image = np.array(io.imread('images/sample1024x1024.jpg'), dtype=np.float) tf_image = idct2(rotate180(dct2(image))) image = np.rot90(image, k=2) self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_reflecion0_1(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) tf_image = idct2(reflection(dct2(image), axis=0)) image = np.flipud(image) self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_reflecion1_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) tf_image = idct2(reflection(dct2(image), axis=1)) image = np.fliplr(image) self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_mask_vertical_4(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) ulx, lrx, uly, lry = 200, 304, 104, 112 tf_image = idct2(mask(dct2(image), ulx=ulx, lrx=lrx, uly=uly, lry=lry)) image[ulx:lrx, uly:lry] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_translationpositive0_1(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) tf_image = idct2(translation(dct2(image), 100, axis=0)) image[100:, :] = image[:-100, :] image[:100, :] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_translationzero_2(self): image = np.array(io.imread('images/sample512x512.jpg'), dtype=np.float) tf_image = idct2(translation(dct2(image), image.shape[1] * 2, axis=0)) image[:] = 0 self.assertTrue(np.max(np.abs(image - tf_image)) < 1e-10)
def test_dctidct_3(self): image = np.array(io.imread('images/sample2048x2048.jpg'), dtype=np.float) dct_image = idct2(dct2(image)) self.assertTrue(np.max(np.abs(image - dct_image)) < 1e-10)
def reflection(image, axis=0): ''' 8x8のブロックごとに離散コサイン変換された画像(以下DCT画像)を鏡像変換する. Parameters ---------- image:幅と高さが8の倍数である画像を表す2次元配列. 8の倍数でない場合の動作は未定義. axis:変換する軸. defalutは`axis=0` Returns ------- `image`を鏡像変換したDCT画像を表す2次元配列を返す. `image`の値は変わらない. Examples -------- >>> import numpy as np >>> a = np.arange(64).reshape((8,8)) >>> a array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30, 31], [32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47], [48, 49, 50, 51, 52, 53, 54, 55], [56, 57, 58, 59, 60, 61, 62, 63]]) >>> dct_image_transform.reflection.reflection(a,axis=0) array([[ 5.77395663e-15, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00, 7.00000000e+00], [-8.00000000e+00, -9.00000000e+00, -1.00000000e+01, -1.10000000e+01, -1.20000000e+01, -1.30000000e+01, -1.40000000e+01, -1.50000000e+01], [ 1.60000000e+01, 1.70000000e+01, 1.80000000e+01, 1.90000000e+01, 2.00000000e+01, 2.10000000e+01, 2.20000000e+01, 2.30000000e+01], [-2.40000000e+01, -2.50000000e+01, -2.60000000e+01, -2.70000000e+01, -2.80000000e+01, -2.90000000e+01, -3.00000000e+01, -3.10000000e+01], [ 3.20000000e+01, 3.30000000e+01, 3.40000000e+01, 3.50000000e+01, 3.60000000e+01, 3.70000000e+01, 3.80000000e+01, 3.90000000e+01], [-4.00000000e+01, -4.10000000e+01, -4.20000000e+01, -4.30000000e+01, -4.40000000e+01, -4.50000000e+01, -4.60000000e+01, -4.70000000e+01], [ 4.80000000e+01, 4.90000000e+01, 5.00000000e+01, 5.10000000e+01, 5.20000000e+01, 5.30000000e+01, 5.40000000e+01, 5.50000000e+01], [-5.60000000e+01, -5.70000000e+01, -5.80000000e+01, -5.90000000e+01, -6.00000000e+01, -6.10000000e+01, -6.20000000e+01, -6.30000000e+01]]) ''' R = np.zeros((8, 8), dtype=np.float) for i in range(8): R[i, 7 - i] = 1 R = dct2(R) if axis == 0: return np.vstack( list( map(lambda m: np.dot(R, m), np.flip(np.vsplit(image, range(8, image.shape[1], 8)), 0)))) elif axis == 1: return np.hstack( list( map(lambda m: np.dot(m, R), np.flip(np.hsplit(image, range(8, image.shape[1], 8)), 0))))