Creates and returns a function `fn` using crop region coordinates given in crop_points. `fn` receives two input points x and xn and returns all the crossing points between the line connecting x and xn, and the borders of the cropping rectangle.
(self, crop_points)
| 420 | return np.stack(final_points)[:,None] |
| 421 | |
| 422 | def get_border_functions(self, crop_points): |
| 423 | """ Creates and returns a function `fn` using crop region coordinates given in crop_points. |
| 424 | `fn` receives two input points x and xn and returns all the crossing points between the line connecting |
| 425 | x and xn, and the borders of the cropping rectangle. |
| 426 | """ |
| 427 | p = crop_points[:,0] |
| 428 | p_next = np.roll(p, (-1), axis=(0)) |
| 429 | def fn(x, xn): |
| 430 | output = [] |
| 431 | c_diff = p_next - p |
| 432 | x_diff = x - xn |
| 433 | for diff, c in zip(c_diff, p): |
| 434 | A = np.array([ |
| 435 | [diff[0], x_diff[0]], |
| 436 | [diff[1], x_diff[1]] |
| 437 | ]) |
| 438 | b = x - c |
| 439 | try: |
| 440 | lmbda = np.linalg.solve(A, b) |
| 441 | if 0 <= lmbda[0] <= 1 and 0 <= lmbda[1] <= 1: |
| 442 | output.append(lmbda[1] * xn + (1-lmbda[1]) * x) |
| 443 | except: |
| 444 | continue |
| 445 | return output |
| 446 | return fn |
| 447 | |
| 448 | def crop_sample(self, sample, crop_coords): |
| 449 | """ Crop the sample using crop coordinates. |