yfukai/m2stitch

"images" input format

lenkerdenker opened this issue · 10 comments

Hi @yfukai , thanks a lot for this package!
I'm having issues to get the images I want to import into the correct format.
np.load does not work on my path (permission issues) so I have to import the images seperately.
I have 4 images. How do I have to put together the images once i loaded them with np.load seperately?
i tried to create an imgaes = np.array([img1,img2,img3,img4]) but m2stitch.stitching(images) throws an empty AssertionError
Unfortunately this is not celar to me after reading the docs...

Thanks @lenkerdenker for your report!

throws an empty AssertionError

I feel sorry for this unorganized error handling. Would you mind posting a detailed error message, including the line number? Then I might be able to give some support to solve the issue.

@yfukai sure!

my code:

image1 = np.load(r'A61_20230123113809.npy')
image2 = np.load(r'A62_20230123113809.npy')
image3 = np.load(r'A63_20230123113810.npy')
image4 = np.load(r'A64_20230123113811.npy')

images = np.array([image1, image2, image3, image4])
rows = [0,1]
cols = [0,1]
result_df, _ = m2stitch.stitch_images(images, rows, cols, row_col_transpose=False)

Error:

AssertionError                            Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 result_df, _ = m2stitch.stitch_images(images, rows, cols, row_col_transpose=False)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\m2stitch\stitching.py:118, in stitch_images(images, rows, cols, position_indices, position_initial_guess, overlap_diff_threshold, pou, full_output, row_col_transpose, ncc_threshold)
    116         position_indices = np.array([rows, cols]).T
    117 position_indices = np.array(position_indices)
--> 118 assert images.shape[0] == position_indices.shape[0]
    119 assert position_indices.shape[1] == images.ndim - 1
    120 if position_initial_guess is not None:

AssertionError: 

Hm, that's interesting, considering the arguments for the stitch_images function.

def stitch_images(
images: Union[Sequence[NumArray], NumArray],
rows: Optional[Sequence[Any]] = None,
cols: Optional[Sequence[Any]] = None,
position_indices: Optional[NumArray] = None,
position_initial_guess: Optional[NumArray] = None,
overlap_diff_threshold: Float = 10,
pou: Float = 3,
full_output: bool = False,
row_col_transpose: bool = True,
ncc_threshold: Float = 0.5,
) -> Tuple[pd.DataFrame, dict]:

Can you update the package by pip install -U m2stitch and try again?

updated, no changes, I already had version 0.7.0 installed.
maybe I transform my source images (.bmp) not correctly into .npy images? could that cuase an issue?

import cv2 as cv
import numpy as np

for img in imagelist:   
    image = cv.imread(path + img)

    enhanced_img = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    
    np.save(path.replace('.bmp', '.npy'), enhanced_img)

Oh, I got the point!
You have four images but specified two rows and cols. The length of them should actually be the same, indicating each position of the tile.
For example, if you have four images at (0,0), (1,0), (0,1), (1,1), it should be rows=[0,1,0,1] and cols=[0,0,1,1].

Thanks, that was one of the issues. But the other issue still remains. I feel like doing np.load() on a directory like you do in your usage example does not yield the same result as doing np.load on multiple images and then putting them into an np.array...

I feel like doing np.load() on a directory like you do in your usage example does not yield the same result as doing np.load on multiple images and then putting them into an np.array...

Thanks, sorry it's difficult to provide advice with limited information. In principle the data-saving method shouldn't make a difference. Can you describe in more detail what you expected and what you obtained?

Yes of course and thanks a lot for your help with this, really appreciated!
My images:
4 .bmp microscope images with an overlap of about 20% positioned like:

1 2
3 4

import os
import m2stitch
from skimage import io
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import cv2 as cv

imagelist = os.listdir('.\\Test')

for img in imagelist:   
    path = '.\\Test\\' + img
    image = cv.imread(path)
    enhanced_img = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    np.save(path.replace('.bmp', '.npy'), enhanced_img)

image1 = np.load('.\\Test\\A1.npy')
image2 = np.load('.\\Test\\A2.npy')
image3 = np.load('.\\Test\\A3.npy')
image4 = np.load('.\\Test\\A4.npy')

images = np.array([image2, image1, image4, image3])
print(images.shape)

# result: (4, 480, 640, 3)

cols=[0,1,0,1]
rows=[0,0,1,1]

result_df, _ = m2stitch.stitch_images(images, rows, cols, row_col_transpose=False, ncc_threshold=0.5)

Error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 result_df, _ = m2stitch.stitch_images(images, rows, cols, row_col_transpose=False, ncc_threshold=0.5)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\m2stitch\stitching.py:119, in stitch_images(images, rows, cols, position_indices, position_initial_guess, overlap_diff_threshold, pou, full_output, row_col_transpose, ncc_threshold)
    117 position_indices = np.array(position_indices)
    118 assert images.shape[0] == position_indices.shape[0]
--> 119 assert position_indices.shape[1] == images.ndim - 1
    120 if position_initial_guess is not None:
    121     position_initial_guess = np.array(position_initial_guess)

AssertionError: 

Hi, the images should be single-channeled with the shape (M, Y, X) (M is the mosaic position dimension. Thanks!

yfukai commented

Closing for now, but please feel free to reopen @lenkerdenker.