yfukai / m2stitch

MIST-inspired microscope image stitching package
Other
63 stars 11 forks source link

Doesn't work with single row/column dataset #430

Open sTeADone opened 8 months ago

sTeADone commented 8 months ago

Works pretty well if the source contains multiple columns and rows. Gives error if only one column or row is selected for stitching.

test2.zip

import numpy as np
import os
import pandas as pd
import m2stitch
import cv2

props = pd.read_csv("list.csv", index_col=0)

# filter columns/rows

# this works
sub_props = props[props["col"].isin([0,1])]

# this will fail
# sub_props = props[props["col"].isin([1])]

print(sub_props.index.to_list())
print(sub_props["col"].to_list())
print(sub_props["row"].to_list())

images = []
for idx,row in sub_props.iterrows():
    path = os.path.join("./imgs",row["file"])
    print(path)
    grey = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    print(grey.shape)
    images.append(grey)

result_df, _ = m2stitch.stitch_images(
    images, sub_props["row"].to_list(), sub_props["col"].to_list(), row_col_transpose=False, ncc_threshold=0.2
)

# merge with original to get the file name
result_df = result_df.merge(sub_props, on=["row", "col"])

def output_image(images, result_df, filename):
    result_df["y_pos2"] = result_df["y_pos"] - result_df["y_pos"].min()
    result_df["x_pos2"] = result_df["x_pos"] - result_df["x_pos"].min()

    size_y = images[0].shape[0]
    size_x = images[0].shape[1]

    stitched_image_size = (
        result_df["y_pos2"].max() + size_y,
        result_df["x_pos2"].max() + size_x,
    )
    stitched_image = np.zeros_like(images, shape=stitched_image_size)
    for i, row in result_df.iterrows():
        stitched_image[
            row["y_pos2"] : row["y_pos2"] + size_y,
            row["x_pos2"] : row["x_pos2"] + size_x,
        ] = images[i]
        # write some text on the image
        cv2.putText(stitched_image, f"{row['file']}, R{row['row']}C{row['col']}", (row["x_pos2"] + 50, row["y_pos2"] + 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 0, 3, cv2.LINE_AA)
        cv2.putText(stitched_image, f"{row['file']}, R{row['row']}C{row['col']}", (row["x_pos2"] + 50, row["y_pos2"] + 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2, cv2.LINE_AA)

    cv2.imwrite(filename, stitched_image)

output_image(result_df=result_df, images=images, filename="result.webp")
BorisMuller commented 3 months ago

quick-fix.zip I had the same issue so I made a quick low effort fix by checking if only a single row/column of images is provided and then turning off all checks for top/left.