MouseLand / cellpose

a generalist algorithm for cellular segmentation with human-in-the-loop capabilities
https://www.cellpose.org/
BSD 3-Clause "New" or "Revised" License
1.37k stars 393 forks source link

[BUG] Error in OpenCV when using CellposeDenoiseModel #990

Closed ABCarvalho closed 1 month ago

ABCarvalho commented 3 months ago

Describe the bug My understanding this error is due to how OpenCV is trying to resize the image. The issue may be with the CellposeDenoiseModel, because when I just run the segmentation model CellposeModel, there are no errors. In the code below, if you make the "Segmentation" module run first, everything goes smoothly for the 1st image. Fails after the CellposeDenoiseModel tries to be run.

To Reproduce

import os
import numpy as np
import tifffile as tiff
import matplotlib.pyplot as plt
#import matplotlib as mpl
import tkinter as tk
from tkinter import filedialog
from cellpose import models, io, transforms, plot, denoise
#from cellpose.io import imread

# Function to segment images using Cellpose
def segment_images(input_folder):

    # Create output folder
    output_folder = input_folder
    os.makedirs(output_folder, exist_ok=True)

    # Load all .tiff images from the subfolder
    #image_files = [f for f in os.listdir(subfolder_path) if f.endswith('.tif')]
    image_files = io.get_image_files(input_folder, mask_filter=".npy")

    # Initialize Cellpose model
    model = models.CellposeModel(gpu=True, model_type='cyto3')
    dn = denoise.CellposeDenoiseModel(restore_type="denoise_cyto3", model_type="cyto3", gpu=True)

    for image_file in image_files:
        # Load image
        image_path = os.path.join(input_folder, image_file)
        image = io.imread(image_path)

#Denoising and Segmentation          
        masks_dn, flows_dn, styles_dn, imgs_dn = dn.eval(image, channels=[0,0], 
                            diameter=40, stitch_threshold=0.25, min_size=25,
                            rescale=None)

        #Save Denoised Images
        imgs_dn_path = os.path.join(output_folder, f"{os.path.splitext(image_file)[0]}_dn.tif")
        tiff.imwrite(imgs_dn_path, imgs_dn)

        # Save masks as .tif files
        mask_path = os.path.join(output_folder, f"{os.path.splitext(image_file)[0]}_dn.tif")
        io.save_masks(imgs_dn, masks_dn, flows_dn, mask_path, tif=True)

        # Save masks as .npy files
        seg_path = os.path.join(output_folder, f"{os.path.splitext(image_file)[0]}_dn.npy")
        io.masks_flows_to_seg(imgs_dn, masks_dn, flows_dn, seg_path)
        print("Denoising & Segmentation Completed")     

#Segmentation        
        # Perform segmentation
        masks, flows, styles = model.eval(image, diameter=40, channels=[0,0], 
                            stitch_threshold=0.25, min_size=25)

        # Save masks as .tif files
        mask_path = os.path.join(output_folder, f"{os.path.splitext(image_file)[0]}.tif")
        io.save_masks(image, masks, flows, mask_path, tif=True)

        # Save masks as .npy files
        seg_path = os.path.join(output_folder, f"{os.path.splitext(image_file)[0]}.npy")
        io.masks_flows_to_seg(image, masks, flows, seg_path)
        print("Cellpose Segmentation Completed")

# Prompt the user to define input folder

root = tk.Tk()
root.withdraw()  # Hide the root window

folder_path = filedialog.askdirectory(title="Select the folder with the ZStacks")

segment_images(folder_path)

Run log 100%|██████████| 21/21 [00:00<00:00, 9712.25it/s] Traceback (most recent call last): File ~\anaconda3\envs\cellpose3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File e:\1.crest\1. cell_rounding\phd_cellpose3_batch_seg.py:87 segment_images(folder_path) File e:\1.crest\1. cell_rounding\phd_cellpose3_batch_seg.py:47 in segment_images masks_dn, flows_dn, styles_dn, imgs_dn = dn.eval(image, channels=[0,0], File ~\anaconda3\envs\cellpose3\lib\site-packages\cellpose\denoise.py:551 in eval masks, flows, styles = self.cp.eval(img_restore, batch_size=batch_size, channels=channels_new, channel_axis=-1, File ~\anaconda3\envs\cellpose3\lib\site-packages\cellpose\models.py:410 in eval masks, styles, dP, cellprob, p = self._run_cp( File ~\anaconda3\envs\cellpose3\lib\site-packages\cellpose\models.py:480 in _run_cp img = transforms.resize_image(img, rsz=rescale) File ~\anaconda3\envs\cellpose3\lib\site-packages\cellpose\transforms.py:727 in resize_image imgs = cv2.resize(img0, (Lx, Ly), interpolation=interpolation) error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4155: error: (-215:Assertion failed) inv_scale_x > 0 in function ‘cv::resize’

BioinfoTongLI commented 1 month ago

experiencing the same error but with default cyto3 model, not the denoised one. Also, it is not occuring to all my image. But just some of them. I assume it is about the image content? e.g. all black? Update: actually it behaves the same for cyto2 model as well.

carsen-stringer commented 1 month ago

what are your image sizes? it might also be helpful to tell cellpose your channel_axis as an input to eval. I've made some updates to the auto-channel_axis detection, but it's useful to directly specify z_axis and channel_axis, especially with stacks

BioinfoTongLI commented 1 month ago

Hi @carsen-stringer, thanks for the suggestion. And indeed I can confirm that the error is gone when specifying channel_axis.

carsen-stringer commented 1 month ago

great! I will close this issue for now but will reopen if there are more problems