我只是想知道这是否是一种加快Python中for循环性能的方法。
for i in range (0,img.shape[0],new_height):
for j in range(0,img.shape[1],new_width):
cropped_image = img[i:i+new_height,j:j+new_width]
yuv_image = cv2.cvtColor(cropped_image,cv2.COLOR_BGR2YUV)
Y,U,V = cv2.split(yuv_image)
pixel_image_y = np.array(Y).flatten()
答案 0 :(得分:2)
将整个图像转换为YUV空间后,我们可以简单地重塑成较小的块-
m,n = img.shape[:2]
yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
yuv4D = yuv[...,0].reshape(m//new_height,new_height,n//new_width,new_width)
out = yuv4D.swapaxes(1,2).reshape(-1,new_height*new_width)
在1024x1024 RGB图像上的时间-
In [157]: img = np.random.randint(0,256,(1024,1024,3)).astype(np.uint8)
...: new_height,new_width = 32,32
In [158]: %%timeit
...: out = []
...: for i in range (0,img.shape[0],new_height):
...: for j in range(0,img.shape[1],new_width):
...: cropped_image = img[i:i+new_height,j:j+new_width]
...: yuv_image = cv2.cvtColor(cropped_image,cv2.COLOR_BGR2YUV)
...: Y,U,V = cv2.split(yuv_image)
...: pixel_image_y = np.array(Y).flatten()
...: out.append(pixel_image_y)
11.9 ms ± 991 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [159]: %%timeit
...: m,n = img.shape[:2]
...: yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
...: yuv4D = yuv[...,0].reshape(m//new_height,new_height,n//new_width,new_width)
...: out1 = yuv4D.swapaxes(1,2).reshape(-1,new_height*new_width)
1.48 ms ± 5.23 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)