简体   繁体   中英

cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) vs cv2.merge([gray,gray,gray])

There are these two options to turn a single channel image into a 3 channel image:

cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) vs cv2.merge([gray, gray, gray])

What's the implementation differences? Which one is faster? Which should be used?

The below seems to give consistent results that cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY) is faster than cv2.merge([gray, gray, gray])

Given that cvtColor COLOR_BGR2GRAY is also arguably more legible/explicit in its function and we have a clear winner.

cvtColor should be used over merge

import time

import cv2
import numpy as np

image = (np.random.standard_normal([20000, 20000, 3]) * 255).astype(np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


start = time.time()
gray_BGR = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
print("cvtColor COLOR_GRAY2BGR took", time.time() - start, "seconds")

start = time.time()
gray_three = cv2.merge([gray, gray, gray])
print("merge took", time.time() - start, "seconds")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM