Input: im: h x w x 3 array qf: compress factor, (0, 100] chn_in: 'rgb' or 'bgr' Return: Compressed Image with channel order: chn_in
(im, qf, chn_in='rgb')
| 364 | return flag |
| 365 | |
| 366 | def jpeg_compress(im, qf, chn_in='rgb'): |
| 367 | ''' |
| 368 | Input: |
| 369 | im: h x w x 3 array |
| 370 | qf: compress factor, (0, 100] |
| 371 | chn_in: 'rgb' or 'bgr' |
| 372 | Return: |
| 373 | Compressed Image with channel order: chn_in |
| 374 | ''' |
| 375 | # transform to BGR channle and uint8 data type |
| 376 | im_bgr = rgb2bgr(im) if chn_in.lower() == 'rgb' else im |
| 377 | if im.dtype != np.dtype('uint8'): im_bgr = img_as_ubyte(im_bgr) |
| 378 | |
| 379 | # JPEG compress |
| 380 | flag, encimg = cv2.imencode('.jpg', im_bgr, [int(cv2.IMWRITE_JPEG_QUALITY), qf]) |
| 381 | assert flag |
| 382 | im_jpg_bgr = cv2.imdecode(encimg, 1) # uint8, BGR |
| 383 | |
| 384 | # transform back to original channel and the original data type |
| 385 | im_out = bgr2rgb(im_jpg_bgr) if chn_in.lower() == 'rgb' else im_jpg_bgr |
| 386 | if im.dtype != np.dtype('uint8'): im_out = img_as_float32(im_out).astype(im.dtype) |
| 387 | return im_out |
| 388 | |
| 389 | # ------------------------Augmentation----------------------------- |
| 390 | def data_aug_np(image, mode): |