Skip to content

Commit 567a13e

Browse files
committed
Refactor image reading in iread function to use cv.imdecode for better compatibility; add unit tests for iread functionality.
1 parent 8b4399c commit 567a13e

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

src/machinevisiontoolbox/base/imageio.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,10 @@ def iread(filename, *args, **kwargs):
830830
images = []
831831
pathlist.sort()
832832
for p in pathlist:
833-
image = cv.imread(p, -1) # default read-in as BGR
833+
image = cv.imdecode(
834+
np.fromfile(Path(p).as_posix(), dtype=np.uint8), cv.IMREAD_UNCHANGED
835+
)
836+
# image = cv.imread(p, -1) # default read-in as BGR
834837
images.append(convert(image, **kwargs))
835838
return images, pathlist
836839

@@ -840,6 +843,9 @@ def iread(filename, *args, **kwargs):
840843

841844
# read the image
842845
# TODO not sure the following will work on Windows
846+
image = cv.imdecode(
847+
np.fromfile(path.as_posix(), dtype=np.uint8), cv.IMREAD_UNCHANGED
848+
)
843849
image = cv.imread(path.as_posix(), -1) # default read-in as BGR
844850
if image is None:
845851
# TODO check ValueError

tests/base/test_io.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
3+
# test for Image input/output
4+
5+
import numpy as np
6+
import os
7+
import numpy.testing as nt
8+
import unittest
9+
import contextlib
10+
import io
11+
12+
# import machinevisiontoolbox as mvt
13+
from machinevisiontoolbox import Image, ImageCollection
14+
from machinevisiontoolbox.base import iread
15+
from pathlib import Path
16+
from collections.abc import Iterable
17+
18+
19+
class TestBaseImageIO(unittest.TestCase):
20+
21+
def test_iread(self):
22+
# greyscale image
23+
im = iread("wally.png")
24+
self.assertIsInstance(im[0], np.ndarray)
25+
self.assertIsInstance(im[1], str)
26+
self.assertEqual(im[0].shape, (25, 21))
27+
28+
# color image
29+
im = iread("monalisa.png")
30+
self.assertIsInstance(im[0], np.ndarray)
31+
self.assertIsInstance(im[1], str)
32+
self.assertEqual(im[0].shape, (700, 677, 3))
33+
34+
# greyscale image sequence
35+
im = iread("seq/im*.png")
36+
self.assertIsInstance(im[0], list)
37+
self.assertEqual(len(im[0]), 9)
38+
self.assertIsInstance(im[0][0], np.ndarray)
39+
self.assertEqual(im[0][0].shape, (512, 512), self.assertIsInstance(im[1], list))
40+
self.assertEqual(len(im[1]), 9)
41+
self.assertIsInstance(im[1][0], str)
42+
43+
# color image sequence
44+
im = iread("campus/holdout/*.png")
45+
self.assertIsInstance(im[0], list)
46+
self.assertEqual(len(im[0]), 5)
47+
self.assertIsInstance(im[0][0], np.ndarray)
48+
self.assertEqual(im[0][0].shape, (426, 640, 3))
49+
self.assertIsInstance(im[1], list)
50+
self.assertEqual(len(im[1]), 5)
51+
self.assertIsInstance(im[1][0], str)
52+
53+
# URL image
54+
im = iread("https://petercorke.com/files/images/monalisa.png")
55+
self.assertIsInstance(im[0], np.ndarray)
56+
self.assertIsInstance(im[1], str)
57+
self.assertEqual(im[0].shape, (700, 677, 3))
58+
59+
# TODO unit tests:
60+
# test the various options to iread()
61+
# test writing
62+
# test_isimage - make sure Image rejects/fails with invalid input
63+
# test_imtypes - test Image works on different Image types?
64+
# test_getimage - make sure Image returns the same array but with valid
65+
# typing?
66+
# test_imwrite - test write/successfully save file?
67+
68+
def tearDown(self):
69+
# Cleanup code if needed
70+
pass
71+
72+
73+
# ------------------------------------------------------------------------ #
74+
if __name__ == "__main__":
75+
76+
unittest.main()

tests/test_image_io.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@
1818

1919
class TestImage(unittest.TestCase):
2020

21-
def test_iread(self):
22-
# see ioTest.m
23-
# test image:
24-
im = iread("wally.png")
25-
self.assertIsInstance(im[0], np.ndarray)
26-
self.assertIsInstance(im[1], str)
27-
self.assertEqual(im[0].shape, (25, 21))
28-
29-
im = iread("monalisa.png")
30-
self.assertIsInstance(im[0], np.ndarray)
31-
self.assertIsInstance(im[1], str)
32-
self.assertEqual(im[0].shape, (700, 677, 3))
33-
3421
def test_isimage(self):
3522

3623
# create mini image (Bayer pattern)

0 commit comments

Comments
 (0)