tests.compare_cv2_tifffile

A script that compares reading images with cv2 and tifffile for speed.

 1#!/usr/bin/env python
 2"""
 3A script that compares reading images with cv2 and tifffile for speed.
 4"""
 5
 6import os
 7import time
 8import cv2
 9import numpy as np
10import tifffile
11import imageio.v3 as imageio
12import skimage.io
13
14
15def main():
16    # Get all DAPI .tif file names in the directory
17    file_path = "/mnt/HDSCA_Development/data/0B58703"
18    file_names = [os.path.join(file_path, f"Tile{i:06d}.tif") for i in range(100, 200)]
19
20    print(f"Number of files: {len(file_names)}")
21    # cv2
22    start = time.time()
23    cv2_results = []
24    for file_name in file_names:
25        img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED)
26        # Check that we are getting the same results
27        cv2_results.append(np.mean(img))
28        # Print on the same line over and over
29        # print(f"cv2: {os.path.basename(file_name)}")
30    end = time.time()
31    print(f"cv2: {end - start}")
32
33    # tifffile
34    start = time.time()
35    tifffile_results = []
36    for file_name in file_names:
37        img = tifffile.imread(file_name)
38        # Check that we are getting the same results
39        tifffile_results.append(np.mean(img))
40        # Print on the same line over and over
41        # print(f"tifffile: {os.path.dirname(file_name)}", end="\r")
42    end = time.time()
43    print(f"tifffile: {end - start}")
44
45    # imageio
46    start = time.time()
47    imageio_results = []
48    for file_name in file_names:
49        img = imageio.imread(file_name)
50        # Check that we are getting the same results
51        imageio_results.append(np.mean(img))
52        # Print on the same line over and over
53        # print(f"imageio: {os.path.dirname(file_name)}", end="\r")
54    end = time.time()
55    print(f"imageio: {end - start}")
56
57    # skimage
58    start = time.time()
59    skimage_results = []
60    for file_name in file_names:
61        img = skimage.io.imread(file_name)
62        # Check that we are getting the same results
63        skimage_results.append(np.mean(img))
64        # Print on the same line over and over
65        # print(f"skimage: {os.path.dirname(file_name)}", end="\r")
66    end = time.time()
67    print(f"skimage: {end - start}")
68
69    # Check that we are getting the same results
70    assert cv2_results == tifffile_results
71    assert cv2_results == imageio_results
72    assert cv2_results == skimage_results
73
74
75if __name__ == "__main__":
76    main()
def main():
16def main():
17    # Get all DAPI .tif file names in the directory
18    file_path = "/mnt/HDSCA_Development/data/0B58703"
19    file_names = [os.path.join(file_path, f"Tile{i:06d}.tif") for i in range(100, 200)]
20
21    print(f"Number of files: {len(file_names)}")
22    # cv2
23    start = time.time()
24    cv2_results = []
25    for file_name in file_names:
26        img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED)
27        # Check that we are getting the same results
28        cv2_results.append(np.mean(img))
29        # Print on the same line over and over
30        # print(f"cv2: {os.path.basename(file_name)}")
31    end = time.time()
32    print(f"cv2: {end - start}")
33
34    # tifffile
35    start = time.time()
36    tifffile_results = []
37    for file_name in file_names:
38        img = tifffile.imread(file_name)
39        # Check that we are getting the same results
40        tifffile_results.append(np.mean(img))
41        # Print on the same line over and over
42        # print(f"tifffile: {os.path.dirname(file_name)}", end="\r")
43    end = time.time()
44    print(f"tifffile: {end - start}")
45
46    # imageio
47    start = time.time()
48    imageio_results = []
49    for file_name in file_names:
50        img = imageio.imread(file_name)
51        # Check that we are getting the same results
52        imageio_results.append(np.mean(img))
53        # Print on the same line over and over
54        # print(f"imageio: {os.path.dirname(file_name)}", end="\r")
55    end = time.time()
56    print(f"imageio: {end - start}")
57
58    # skimage
59    start = time.time()
60    skimage_results = []
61    for file_name in file_names:
62        img = skimage.io.imread(file_name)
63        # Check that we are getting the same results
64        skimage_results.append(np.mean(img))
65        # Print on the same line over and over
66        # print(f"skimage: {os.path.dirname(file_name)}", end="\r")
67    end = time.time()
68    print(f"skimage: {end - start}")
69
70    # Check that we are getting the same results
71    assert cv2_results == tifffile_results
72    assert cv2_results == imageio_results
73    assert cv2_results == skimage_results