[Python] Inverse Abel transform not working with abel package

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    from abel import transform
    # --- Voorbeeld 2D heatmap (voeg je eigen data hier in) ---
    # heatmap = np.load('heatmap.npy') # of laadt vanuit je script
    heatmap = CC # tijdelijke voorbeelddata
    heatmap_bg = heatmap - np.median(heatmap)
    heatmap_bg[heatmap_bg<0] = 0


    # --- Functie voor het verwerken van de geselecteerde ROI ---
    def onselect(eclick, erelease):
    # Get integer pixel bounds
    x1, y1 = int(eclick.xdata), int(eclick.ydata)
    x2, y2 = int(erelease.xdata), int(erelease.ydata)
    width = abs(x2 - x1)
    height = abs(y2 - y1)
    if width % 2 != 0:
    if x2 > x1:
    x2 += 1
    else:
    x1 += 1
    print(f"Breedte oneven ({width}), aangepast selectie naar even breedte.")
    if height % 2 != 0:
    if y2 > y1:
    y2 += 1
    else:
    y1 += 1
    print(f"Breedte oneven ({width}), aangepast selectie naar even breedte.")
    roi = heatmap_bg[min(y1, y2):max(y1, y2), min(x1, x2):max(x1, x2)]

    # --- Fix: controleer of ROI geldig is en minstens 2D ---
    if roi.ndim != 2 or roi.shape[0] == 0 or roi.shape[1] == 0:
    print("Fout: geselecteerde ROI is ongeldig of leeg.")
    return

    # Compute symmetry axis: center of the ROI in x
    center_x = roi.shape[1] // 2
    origin = (center_x, roi.shape[0] // 2)

    try:
    # Inverse Abel transform
    abel_transform_result = transform.Transform(
    roi,
    method='hansenlaw',
    symmetry_axis=center_x,
    direction='inverse',
    )
    except Exception as e:
    print("Fout tijdens Abel-transformatie:", e)
    return

    inv_abel = abel_transform_result.transform

    # Plot the ROI and its reconstruction
    fig2, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
    vmin = np.min(inv_abel)
    vmax = np.max(inv_abel)
    ax1.imshow(roi, cmap='viridis')
    cx = (min(x1, x2) + max(x1, x2)) / 2 # global column index of center
    ax.axvline(x=cx, color='red', linestyle='--')
    ax.set_title('Full image with detected symmetry axis')
    ax1.set_title('Selected ROI')
    ax2.imshow(inv_abel, cmap='viridis', vmin=vmin, vmax=vmax)
    ax2.set_title('Inverse Abel')
    plt.tight_layout()
    plt.show()

    # --- Main figure with selector ---
    fig, ax = plt.subplots()
    ax.imshow(heatmap, cmap='viridis')
    ax.set_title('Drag to select ROI')

    # Set up the RectangleSelector
    selector = RectangleSelector(
    ax, onselect, useblit=True,
    button=[1], # Left mouse only
    minspanx=5, minspany=5, # Ignore tiny slivers
    spancoords='pixels',
    interactive=True
    )

    plt.show()


    I am trying to make an inverse abel transform of my data, but it keeps returning the error;

    Traceback (most recent call last):
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\matplotlib\cbook.py", line 361, in process
    func(*args, **kwargs)
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\matplotlib\widgets.py", line 2276, in release
    self._release(event)
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\matplotlib\widgets.py", line 3297, in _release
    self.onselect(self._eventpress, self._eventrelease)
    File "C:\Users\Thim\PyCharmMiscProject\final\abeltrans.py", line 198, in onselect
    abel_transform_result = transform.Transform(
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\abel\transform.py", line 451, in __init__
    self._abel_transform_image(**transform_options)
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\abel\transform.py", line 506, in _abel_transform_image
    self._abel_transform_image_by_quadrant(**transform_options)
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\abel\transform.py", line 564, in _abel_transform_image_by_quadrant
    self.transform = tools.symmetry.put_image_quadrants(
    File "C:\Users\Thim\PyCharmMiscProject\.venv\lib\site-packages\abel\tools\symmetry.py", line 274, in put_image_quadrants
    Bottom = np.flipud(np.concatenate((np.fliplr(Q2), Q3), axis=1))
    ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 0 dimension(s)


    I already made it so that the width and height are always even, i heard that that helps. But I still get thesame error. The file used is a normal 2D array.

    I already made it so that the width and height are always even, i heard that that helps. But I still get thesame error. The file used is a normal 2D array. Somehow, when te library devides the image into quadrants, the third quadrant Q3 has dimentions 0.

    Continue reading...

Compartilhe esta Página