def test_as_8_bit_BGR_image(self): """ Defines :func:`colour_checker_detection.detection.segmentation.\ as_8_bit_BGR_image` definition unit tests methods. """ image_i = read_image(PNG_FILES[0]) image_o = as_8_bit_BGR_image(image_i) self.assertEqual(image_o.dtype, np.uint8) np.testing.assert_almost_equal(image_o[16, 16, ...], (cctf_encoding(image_i[16, 16, ::-1]) * 255).astype(np.uint8))
def test_as_8_bit_BGR_image(self): """ Defines :func:`colour_checker_detection.detection.segmentation.\ as_8_bit_BGR_image` definition unit tests methods. """ # Skipping unit test when "png" files are missing, e.g. when testing # the distributed "Python" package. if len(PNG_FILES) > 0: return image_i = read_image(PNG_FILES[0]) image_o = as_8_bit_BGR_image(image_i) self.assertEqual(image_o.dtype, np.uint8) np.testing.assert_almost_equal(image_o[16, 16, ...], (cctf_encoding(image_i[16, 16, ::-1]) * 255).astype(np.uint8))
def as_8_bit_BGR_image(image): """ Converts and encodes given linear float *RGB* image to 8-bit *BGR* with *sRGB* reverse OETF. Parameters ---------- image : array_like Image to convert. Returns ------- ndarray Converted image. """ image = np.asarray(image) if image.dtype == np.uint8: return image return cv2.cvtColor((cctf_encoding(image) * 255).astype(np.uint8), cv2.COLOR_RGB2BGR)
def as_8_bit_BGR_image(image: ArrayLike) -> NDArray: """ Convert and encodes given linear float *RGB* image to 8-bit *BGR* with *sRGB* reverse OETF. Parameters ---------- image Image to convert. Returns ------- :class:`numpy.ndarray` Converted image. Notes ----- - In the eventuality where the image is already an integer array, the conversion is by-passed. Examples -------- >>> from colour.algebra import random_triplet_generator >>> prng = np.random.RandomState(4) >>> image = list(random_triplet_generator(8, random_state=prng)) >>> image = np.reshape(image, [4, 2, 3]) >>> print(image) [[[ 0.96702984 0.25298236 0.0089861 ] [ 0.54723225 0.43479153 0.38657128]] <BLANKLINE> [[ 0.97268436 0.77938292 0.04416006] [ 0.71481599 0.19768507 0.95665297]] <BLANKLINE> [[ 0.69772882 0.86299324 0.43614665] [ 0.2160895 0.98340068 0.94897731]] <BLANKLINE> [[ 0.97627445 0.16384224 0.78630599] [ 0.00623026 0.59733394 0.8662893 ]]] >>> image = as_8_bit_BGR_image(image) >>> print(image) [[[ 23 137 251] [167 176 195]] <BLANKLINE> [[ 59 228 251] [250 122 219]] <BLANKLINE> [[176 238 217] [249 253 128]] <BLANKLINE> [[229 112 252] [239 203 18]]] >>> as_8_bit_BGR_image(image) array([[[ 23, 137, 251], [167, 176, 195]], <BLANKLINE> [[ 59, 228, 251], [250, 122, 219]], <BLANKLINE> [[176, 238, 217], [249, 253, 128]], <BLANKLINE> [[229, 112, 252], [239, 203, 18]]], dtype=uint8) """ image = np.asarray(image)[..., :3] if image.dtype == np.uint8: return image return cv2.cvtColor( cast(NDArray, cctf_encoding(image) * 255).astype(np.uint8), cv2.COLOR_RGB2BGR, )