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 11 12 13def main(): 14 # Get all DAPI .tif file names in the directory 15 file_path = "/mnt/HDSCA_Development/data/0B58703" 16 file_names = [os.path.join(file_path, f"Tile{i:06d}.tif") for i in range(1, 4600)] 17 18 print(f"Number of files: {len(file_names)}") 19 # cv2 20 start = time.time() 21 cv2_results = [] 22 for file_name in file_names: 23 img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED) 24 # Check that we are getting the same results 25 cv2_results.append(np.mean(img)) 26 # Print on the same line over and over 27 # print(f"cv2: {os.path.basename(file_name)}") 28 end = time.time() 29 print(f"cv2: {end - start}") 30 31 # tifffile 32 start = time.time() 33 tifffile_results = [] 34 for file_name in file_names: 35 img = tifffile.imread(file_name) 36 # Check that we are getting the same results 37 tifffile_results.append(np.mean(img)) 38 # Print on the same line over and over 39 # print(f"tifffile: {os.path.dirname(file_name)}", end="\r") 40 end = time.time() 41 print(f"tifffile: {end - start}") 42 43 # Check that we are getting the same results 44 assert cv2_results == tifffile_results 45 46 47if __name__ == "__main__": 48 main()
def
main():
14def main(): 15 # Get all DAPI .tif file names in the directory 16 file_path = "/mnt/HDSCA_Development/data/0B58703" 17 file_names = [os.path.join(file_path, f"Tile{i:06d}.tif") for i in range(1, 4600)] 18 19 print(f"Number of files: {len(file_names)}") 20 # cv2 21 start = time.time() 22 cv2_results = [] 23 for file_name in file_names: 24 img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED) 25 # Check that we are getting the same results 26 cv2_results.append(np.mean(img)) 27 # Print on the same line over and over 28 # print(f"cv2: {os.path.basename(file_name)}") 29 end = time.time() 30 print(f"cv2: {end - start}") 31 32 # tifffile 33 start = time.time() 34 tifffile_results = [] 35 for file_name in file_names: 36 img = tifffile.imread(file_name) 37 # Check that we are getting the same results 38 tifffile_results.append(np.mean(img)) 39 # Print on the same line over and over 40 # print(f"tifffile: {os.path.dirname(file_name)}", end="\r") 41 end = time.time() 42 print(f"tifffile: {end - start}") 43 44 # Check that we are getting the same results 45 assert cv2_results == tifffile_results