|
| 1 | +from argparse import Namespace |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from app.image.image import Image |
| 5 | +from app.operation.bgr2rgb import BGR2RGB |
| 6 | +from app.operation.flip import Flip |
| 7 | +from app.operation.grayscale import Grayscale |
| 8 | +from app.operation.rotate90 import Rotate90 |
| 9 | + |
| 10 | + |
| 11 | +def test_bgr2rgb(): |
| 12 | + input_image = Image(np.array([ |
| 13 | + [[10, 20, 30], [15, 25, 35], [16, 26, 36]], |
| 14 | + [[20, 30, 40], [21, 31, 41], [22, 32, 42]], |
| 15 | + [[30, 40, 50], [31, 41, 51], [32, 42, 52]] |
| 16 | + ])) |
| 17 | + |
| 18 | + expected_image = Image(np.array([ |
| 19 | + [[30, 20, 10], [35, 25, 15], [36, 26, 16]], |
| 20 | + [[40, 30, 20], [41, 31, 21], [42, 32, 22]], |
| 21 | + [[50, 40, 30], [51, 41, 31], [52, 42, 32]] |
| 22 | + ])) |
| 23 | + |
| 24 | + output_image = BGR2RGB()(args=None, input_image=input_image) |
| 25 | + |
| 26 | + assert (output_image.data == expected_image.data).all() |
| 27 | + |
| 28 | + |
| 29 | +def test_flip(): |
| 30 | + input_image = Image(np.array([ |
| 31 | + [[10, 20, 30], [15, 25, 35], [16, 26, 36]], |
| 32 | + [[20, 30, 40], [21, 31, 41], [22, 32, 42]], |
| 33 | + [[30, 40, 50], [31, 41, 51], [32, 42, 52]] |
| 34 | + ])) |
| 35 | + |
| 36 | + expected_image = Image(np.array([ |
| 37 | + [[32, 42, 52], [31, 41, 51], [30, 40, 50]], |
| 38 | + [[22, 32, 42], [21, 31, 41], [20, 30, 40]], |
| 39 | + [[16, 26, 36], [15, 25, 35], [10, 20, 30]], |
| 40 | + ])) |
| 41 | + |
| 42 | + output_image = Flip()(args=Namespace(horizontal=True, vertical=True), input_image=input_image) |
| 43 | + |
| 44 | + assert (expected_image.data == output_image.data).all() |
| 45 | + |
| 46 | + |
| 47 | +def test_grayscale(): |
| 48 | + # Try for already grayscale image |
| 49 | + input_image = Image(np.array([ |
| 50 | + [[5], [123], [123]], |
| 51 | + [[12], [12], [12]], |
| 52 | + [[12], [12], [12]] |
| 53 | + ])) |
| 54 | + expected_output = input_image |
| 55 | + |
| 56 | + output_image = Grayscale()(args=None, input_image=input_image) |
| 57 | + |
| 58 | + assert (expected_output.data == output_image.data).all() |
| 59 | + |
| 60 | + # Try for coloured image |
| 61 | + input_image = Image(np.array([ |
| 62 | + [[15, 10, 12], [120, 33, 20]], |
| 63 | + [[30, 0, 2], [10, 45, 22]] |
| 64 | + ])) |
| 65 | + |
| 66 | + output_image = Grayscale()(args=None, input_image=input_image) |
| 67 | + |
| 68 | + assert output_image.data.shape[-1] == 3 |
| 69 | + for row in output_image.data: |
| 70 | + for pixel in row: |
| 71 | + assert np.all(pixel == pixel[0]) |
| 72 | + |
| 73 | + |
| 74 | +def test_rotate90(): |
| 75 | + input_image = Image(np.array([ |
| 76 | + [[15, 10, 12], [120, 33, 20]], |
| 77 | + [[30, 0, 2], [10, 45, 22]] |
| 78 | + ])) |
| 79 | + |
| 80 | + for rotation in range(-5, 5): |
| 81 | + expected_output = np.rot90(input_image.data, rotation) |
| 82 | + output_image = Rotate90()(args=Namespace(rotations=rotation), input_image=input_image) |
| 83 | + assert (expected_output == output_image.data).all() |
0 commit comments