Read image. chn: 'rgb', 'bgr' or 'gray' out: im: h x w x c, numpy tensor
(path, chn='rgb', dtype='float32')
| 310 | |
| 311 | # ------------------------Image I/O----------------------------- |
| 312 | def imread(path, chn='rgb', dtype='float32'): |
| 313 | ''' |
| 314 | Read image. |
| 315 | chn: 'rgb', 'bgr' or 'gray' |
| 316 | out: |
| 317 | im: h x w x c, numpy tensor |
| 318 | ''' |
| 319 | im = cv2.imread(str(path), cv2.IMREAD_UNCHANGED) # BGR, uint8 |
| 320 | try: |
| 321 | if chn.lower() == 'rgb': |
| 322 | if im.ndim == 3: |
| 323 | im = bgr2rgb(im) |
| 324 | else: |
| 325 | im = np.stack((im, im, im), axis=2) |
| 326 | elif chn.lower() == 'gray': |
| 327 | assert im.ndim == 2 |
| 328 | except: |
| 329 | print(str(path)) |
| 330 | |
| 331 | if dtype == 'float32': |
| 332 | im = im.astype(np.float32) / 255. |
| 333 | elif dtype == 'float64': |
| 334 | im = im.astype(np.float64) / 255. |
| 335 | elif dtype == 'uint8': |
| 336 | pass |
| 337 | else: |
| 338 | sys.exit('Please input corrected dtype: float32, float64 or uint8!') |
| 339 | |
| 340 | return im |
| 341 | |
| 342 | def imwrite(im_in, path, chn='rgb', dtype_in='float32', qf=None): |
| 343 | ''' |