diff --git a/python/ftl/ftlstream.py b/python/ftl/ftlstream.py index 183b0dc6b2f4c6bb693ce7cb1021d72644e32464..7ab436e0063b9e30e505398f359efd688aad1f68 100644 --- a/python/ftl/ftlstream.py +++ b/python/ftl/ftlstream.py @@ -8,20 +8,29 @@ from . libde265 import Decoder try: import cv2 as cv - def ycbcr2rgb(img): - raise NotImplementedError("TODO") + + def _ycrcb2rgb(img): + return cv.cvtColor(img, cv.COLOR_YCrCb2RGB) except ImportError: - import skimage.color - def ycbcr2rgb(img): - res = skimage.color.ycbcr2rgb(img.astype(np.float)) + def _ycrcb2rgb(img): + ''' YCrCb to RGB, based on OpenCV documentation definition. + + Note: It seems this implementation is not perfectly equivalent to OpenCV's + ''' + + rgb = np.zeros(img.shape, np.float) + + Y = img[:,:,0].astype(np.float) + Cr = img[:,:,1].astype(np.float) + Cb = img[:,:,2].astype(np.float) + delta = 128.0 - # clip - res[res > 1.0] = 1.0 - res[res < 0.0] = 0.0 + rgb[:,:,0] = Y + 1.403 * (Cr - delta) + rgb[:,:,1] = Y - 0.714 * (Cr - delta) - 0.344 * (Cb - delta) + rgb[:,:,2] = Y + 1.773 * (Cb - delta) - # skimage ycbcr2rgb() returns dtype float64, convert to uint8 - return (res * 255).astype(np.uint8) + return rgb.round().astype(np.uint8) # FTL definitions @@ -172,7 +181,7 @@ class FTLStream: img = decoder.get_next_picture() if img is not None: - self._frames[k] = ycbcr2rgb(img[:,:,(0,2,1)]) # note: channels BGR + self._frames[k] = _ycrcb2rgb(img) def _flush_decoders(self): for decoder in self._decoders.values():