Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from timeit import default_timer as timer
- src = cv2.imread('test.png', cv2.IMREAD_UNCHANGED)
- def test_old(src):
- image = src.copy()
- rows, cols, channels = image.shape
- for row in range(rows):
- for col in range(cols):
- pixel = image[row, col]
- for channel in range(channels):
- if pixel[channel] >= 245:
- pixel[channel] = 255
- if channel == 3:
- pixel[channel] = 0
- else:
- break
- return image
- def test_new(src):
- image = src.copy()
- mask = image >= 245
- mask_0 = mask[:,:,0]
- mask_1 = mask_0 & mask[:,:,1]
- mask_2 = mask_1 & mask[:,:,2]
- mask_3 = mask_2 & mask[:,:,3]
- image[mask_0,0] = 255
- image[mask_1,1] = 255
- image[mask_2,2] = 255
- image[mask_3,3] = 0
- return image
- start = timer()
- image1 = test_old(src)
- mid = timer()
- image2 = test_new(src)
- end = timer()
- t1 = (mid - start)
- t2 = (end - mid)
- match = (image1 == image2).all()
- print((t1, t2, t1/t2, match))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement