|
4 | 4 | from enum import Enum |
5 | 5 | from pySNOM.defaults import Defaults |
6 | 6 |
|
| 7 | +from skimage.transform import warp |
| 8 | +from skimage.registration import optical_flow_tvl1, phase_cross_correlation |
| 9 | +from scipy.ndimage import fourier_shift |
| 10 | + |
7 | 11 | MeasurementModes = Enum( |
8 | 12 | "MeasurementModes", |
9 | 13 | ["None", "AFM", "PsHet", "WLI", "PTE", "TappingAFMIR", "ContactAFM"], |
@@ -347,3 +351,196 @@ def mask_from_booleans(bool_mask, bad_values = False): |
347 | 351 | def mask_from_datacondition(condition): |
348 | 352 | mshape = np.shape(condition) |
349 | 353 | return np.where(condition,np.nan*np.ones(mshape),np.ones(mshape)) |
| 354 | + |
| 355 | + |
| 356 | +class CalculateOpticalFlow(Transformation): |
| 357 | + """Calculates the pixel coordiate drifts between reference and template image""" |
| 358 | + |
| 359 | + def __init__(self, image_ref): |
| 360 | + self.image_ref = image_ref |
| 361 | + |
| 362 | + def transform(self, image): |
| 363 | + v, u = optical_flow_tvl1( |
| 364 | + self.image_ref / np.nanmax(self.image_ref), image / np.nanmax(image) |
| 365 | + ) |
| 366 | + return v, u |
| 367 | + |
| 368 | + |
| 369 | +class WrapImage(Transformation): |
| 370 | + """Applies the pixel-by-pixel drift correction calculated by OpticalFlow""" |
| 371 | + |
| 372 | + def __init__(self, v, u): |
| 373 | + self.v = v |
| 374 | + self.u = u |
| 375 | + |
| 376 | + def transform(self, image): |
| 377 | + nr, nc = image.shape |
| 378 | + row_coords, col_coords = np.meshgrid( |
| 379 | + np.arange(nr), np.arange(nc), indexing="ij" |
| 380 | + ) |
| 381 | + return warp( |
| 382 | + image, np.array([row_coords + self.v, col_coords + self.u]), mode="edge" |
| 383 | + ) |
| 384 | + |
| 385 | + |
| 386 | +class CalculateXCorrDrift(Transformation): |
| 387 | + """Calculates the drift between reference and template image""" |
| 388 | + |
| 389 | + def __init__(self, image_ref): |
| 390 | + self.image_ref = image_ref |
| 391 | + |
| 392 | + def transform(self, image): |
| 393 | + shift, _, _ = phase_cross_correlation(self.image_ref, image) |
| 394 | + return shift |
| 395 | + |
| 396 | + |
| 397 | +class CorrectImageDrift(Transformation): |
| 398 | + """Rearranges image pixels to correct image shift calculated by cross-correlation""" |
| 399 | + |
| 400 | + def __init__(self, shift): |
| 401 | + self.shift = shift |
| 402 | + |
| 403 | + def transform(self, image): |
| 404 | + offset_phase = fourier_shift(np.fft.fftn(image), self.shift) |
| 405 | + offset_phase = np.fft.ifftn(offset_phase) |
| 406 | + return offset_phase.real |
| 407 | + |
| 408 | + |
| 409 | +class AlignImageStack(Transformation): |
| 410 | + """Calculates the drift between the given images and organize the comman areas into an aligned stack""" |
| 411 | + |
| 412 | + def __init__(self): |
| 413 | + pass |
| 414 | + |
| 415 | + def calculate(self, images): |
| 416 | + shifts = [] |
| 417 | + crossrect = [0, 0, np.shape(images[0])[0], np.shape(images[0])[1]] |
| 418 | + if len(images) > 1: |
| 419 | + xcorr = CalculateXCorrDrift(images[0]) |
| 420 | + for i in range(len(images)): |
| 421 | + if i > 0: |
| 422 | + shifts.append(xcorr.transform(images[i])) |
| 423 | + crossrect = shifted_cross_section( |
| 424 | + rect1=crossrect, |
| 425 | + rect2=[ |
| 426 | + -shifts[-1][0], |
| 427 | + shifts[-1][1], |
| 428 | + np.shape(images[i])[0], |
| 429 | + np.shape(images[i])[1], |
| 430 | + ], |
| 431 | + ) |
| 432 | + return shifts, crossrect |
| 433 | + else: |
| 434 | + return None |
| 435 | + |
| 436 | + def transform(self, images, shifts, crossrect): |
| 437 | + aligned_stack = [] |
| 438 | + for i in range(len(images)): |
| 439 | + if i > 0: |
| 440 | + shifter = CorrectImageDrift(shifts[i - 1]) |
| 441 | + aligned_stack.append(shifter.transform(images[i])) |
| 442 | + aligned_stack[i] = cut_image(aligned_stack[i], crossrect) |
| 443 | + else: |
| 444 | + aligned_stack.append(cut_image(images[i], crossrect)) |
| 445 | + return aligned_stack |
| 446 | + |
| 447 | + |
| 448 | +def sort_image_stack(images, wns): |
| 449 | + """Sort the image stack based on the wavenumber list""" |
| 450 | + |
| 451 | + idxs = np.argsort(np.asarray(wns)) |
| 452 | + images = [images[i] for i in idxs] |
| 453 | + wns = [wns[i] for i in idxs] |
| 454 | + |
| 455 | + return images, wns |
| 456 | + |
| 457 | + |
| 458 | +def create_nparray_stack(measlist): |
| 459 | + """Creates a numpy array stack from a list of measurements, organized as [ rows, columns, wavelengths ] (compatible with quasar io utils)""" |
| 460 | + |
| 461 | + stack = np.zeros( |
| 462 | + (np.shape(measlist[0])[0], np.shape(measlist[0])[1], len(measlist)) |
| 463 | + ) |
| 464 | + |
| 465 | + for i, meas in enumerate(measlist): |
| 466 | + stack[:, :, i] = meas |
| 467 | + |
| 468 | + return stack |
| 469 | + |
| 470 | + |
| 471 | +def dict_from_imagestack(X, channelname, wn=None, is_interferogram = True): |
| 472 | + """Converts the image stack into a pySNOM spectra or interferograms compatible dictionary""" |
| 473 | + final_dict = {} |
| 474 | + params = {} |
| 475 | + |
| 476 | + X = np.asarray(X) |
| 477 | + |
| 478 | + params["PixelArea"] = [X.shape[1], X.shape[2], X.shape[0]] |
| 479 | + params["Averaging"] = 1 |
| 480 | + params["Scan"] = "Fourier Scan" |
| 481 | + |
| 482 | + final_dict[channelname] = flatten_stack(X) |
| 483 | + |
| 484 | + y_loc = np.repeat(np.arange(X.shape[1]), X.shape[2]) |
| 485 | + x_loc = np.tile(np.arange(X.shape[2]), X.shape[1]) |
| 486 | + |
| 487 | + final_dict["Row"] = np.repeat(y_loc, X.shape[0]) |
| 488 | + final_dict["Column"] = np.repeat(x_loc, X.shape[0]) |
| 489 | + |
| 490 | + if is_interferogram: |
| 491 | + depth_channel_name = "M" |
| 492 | + else: |
| 493 | + depth_channel_name = "Wavenumber" |
| 494 | + |
| 495 | + if wn is not None: |
| 496 | + final_dict[depth_channel_name] = np.tile(wn, X.shape[1] * X.shape[2]) |
| 497 | + else: |
| 498 | + final_dict[depth_channel_name] = np.tile( |
| 499 | + np.arange(X.shape[0]), X.shape[1] * X.shape[2] |
| 500 | + ) |
| 501 | + |
| 502 | + return final_dict, params |
| 503 | + |
| 504 | +def flatten_stack(imagestack): |
| 505 | + """ Flatten out values in an image stack to be aneble to add it to spectral dictionaries """ |
| 506 | + imagestack = np.asarray(imagestack) |
| 507 | + flattened_values = imagestack.reshape((imagestack.shape[0], imagestack.shape[1] * imagestack.shape[2])) |
| 508 | + return np.ravel(flattened_values, order="F") |
| 509 | + |
| 510 | +def shifted_cross_section(rect1: list, rect2: list): |
| 511 | + """Calculates the cross-section of two rectangle shifted to each other""" |
| 512 | + x1 = rect1[1] |
| 513 | + x2 = rect2[1] |
| 514 | + y1 = rect1[0] |
| 515 | + y2 = rect2[0] |
| 516 | + W1 = rect1[3] |
| 517 | + W2 = rect2[3] |
| 518 | + H1 = rect1[2] |
| 519 | + H2 = rect2[2] |
| 520 | + |
| 521 | + if y2 > y1: |
| 522 | + Hn = H1 - (y2 - y1) |
| 523 | + yn = y2 |
| 524 | + elif (y2 < y1) and (y1 + H1 > y2 + H2): # Negative shift and higher than H2 |
| 525 | + Hn = H2 + (y2 - y1) |
| 526 | + yn = y1 |
| 527 | + else: |
| 528 | + Hn = H1 |
| 529 | + yn = y1 |
| 530 | + |
| 531 | + if x2 > x1: # Positive shift |
| 532 | + Wn = W1 - (x2 - x1) |
| 533 | + xn = x2 |
| 534 | + elif (x2 < x1) and (x1 + W1 > x2 + W2): # Negative shift and higher than W2 |
| 535 | + Wn = W2 + (x2 - x1) |
| 536 | + xn = x1 |
| 537 | + else: |
| 538 | + Wn = W1 |
| 539 | + xn = x1 |
| 540 | + |
| 541 | + return int(yn), int(xn), int(Hn), int(Wn) |
| 542 | + |
| 543 | + |
| 544 | +def cut_image(image, rect): |
| 545 | + """Cuts the part of the image array defined by rectangle""" |
| 546 | + return image[-(rect[2]) : -(rect[0] + 1), rect[1] : rect[1] + rect[3]] |
0 commit comments