Advertisement
dan-masek

Untitled

Nov 19th, 2022
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from timeit import default_timer as timer
  4.  
  5. src = cv2.imread('test.png', cv2.IMREAD_UNCHANGED)
  6.  
  7. def test_old(src):
  8.     image = src.copy()
  9.     rows, cols, channels = image.shape
  10.     for row in range(rows):
  11.         for col in range(cols):
  12.             pixel = image[row, col]
  13.             for channel in range(channels):
  14.                 if pixel[channel] >= 245:
  15.                     pixel[channel] = 255
  16.                     if channel == 3:
  17.                         pixel[channel] = 0
  18.                 else:
  19.                     break
  20.     return image
  21.  
  22. def test_new(src):
  23.     image = src.copy()
  24.     mask = image >= 245
  25.  
  26.     mask_0 = mask[:,:,0]
  27.     mask_1 = mask_0 & mask[:,:,1]
  28.     mask_2 = mask_1 & mask[:,:,2]
  29.     mask_3 = mask_2 & mask[:,:,3]
  30.  
  31.     image[mask_0,0] = 255
  32.     image[mask_1,1] = 255
  33.     image[mask_2,2] = 255
  34.     image[mask_3,3] = 0
  35.    
  36.     return image
  37.  
  38. start = timer()
  39. image1 = test_old(src)
  40. mid = timer()
  41. image2 = test_new(src)
  42. end = timer()
  43.  
  44.  
  45. t1 = (mid - start)
  46. t2 = (end - mid)
  47. match = (image1 == image2).all()
  48.  
  49. print((t1, t2, t1/t2, match))
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement