Calculate image gradient. Input: im: h x w x c numpy array
(im, pading_mode='mirror')
| 487 | |
| 488 | # -----------------------Covolution------------------------------ |
| 489 | def imgrad(im, pading_mode='mirror'): |
| 490 | ''' |
| 491 | Calculate image gradient. |
| 492 | Input: |
| 493 | im: h x w x c numpy array |
| 494 | ''' |
| 495 | from scipy.ndimage import correlate # lazy import |
| 496 | wx = np.array([[0, 0, 0], |
| 497 | [-1, 1, 0], |
| 498 | [0, 0, 0]], dtype=np.float32) |
| 499 | wy = np.array([[0, -1, 0], |
| 500 | [0, 1, 0], |
| 501 | [0, 0, 0]], dtype=np.float32) |
| 502 | if im.ndim == 3: |
| 503 | gradx = np.stack( |
| 504 | [correlate(im[:,:,c], wx, mode=pading_mode) for c in range(im.shape[2])], |
| 505 | axis=2 |
| 506 | ) |
| 507 | grady = np.stack( |
| 508 | [correlate(im[:,:,c], wy, mode=pading_mode) for c in range(im.shape[2])], |
| 509 | axis=2 |
| 510 | ) |
| 511 | grad = np.concatenate((gradx, grady), axis=2) |
| 512 | else: |
| 513 | gradx = correlate(im, wx, mode=pading_mode) |
| 514 | grady = correlate(im, wy, mode=pading_mode) |
| 515 | grad = np.stack((gradx, grady), axis=2) |
| 516 | |
| 517 | return {'gradx': gradx, 'grady': grady, 'grad':grad} |
| 518 | |
| 519 | def imgrad_fft(im): |
| 520 | ''' |