@@ -461,11 +461,9 @@ def crop_section(img, im_name, resolution, minpixel, maxpixel, im_center):
461461def segment_section (crop_im , im_name , resolution , minpixel , maxpixel , im_center ):
462462 try :
463463 thresh = skimage .filters .threshold_minimum (crop_im )
464- bin_img = crop_im < thresh
465- # bin_img = skimage.segmentation.clear_border(crop_im < thresh)
466-
467- # seg_im_inv = skimage.segmentation.chan_vese(crop_im, max_iter=200, init_level_set=bin_img)
468- seg_im = skimage .segmentation .morphological_chan_vese (np .asarray (crop_im ), 40 , init_level_set = bin_img , smoothing = 4 )
464+ bin_ls_set = crop_im < thresh
465+
466+ seg_im = skimage .segmentation .morphological_chan_vese (np .asarray (crop_im ), 40 , init_level_set = bin_ls_set , smoothing = 4 )
469467
470468 seg_im_inv = np .asarray (seg_im != 0 )
471469
@@ -480,7 +478,7 @@ def segment_section(crop_im, im_name, resolution, minpixel, maxpixel, im_center)
480478 {'ID' : [np .nan ], 'area' : [np .nan ], 'eccentricity' : [np .nan ], 'min' : [np .nan ],
481479 'max' : [np .nan ]})
482480 thresh = skimage .filters .threshold_minimum (crop_im )
483- bin_im = skimage . segmentation . clear_border ( crop_im < thresh )
481+ bin_im = crop_im < thresh
484482
485483 return section_data , bin_im
486484
@@ -518,53 +516,57 @@ def section_seq(input_file, output_path, resolution, minsize, maxsize, save_img)
518516
519517 with tqdm (total = 3 , desc = "section analysis sequence" , unit = "steps" , position = 1 , leave = None ) as pbar :
520518 for i in [input_file ]:
519+
520+ try :
521521
522- # read in file
523- img , im_name = imread (input_file , use_skimage = True )
524-
525- # Gets the unique values in the image matrix. Since it is binary, there should only be 2.
526- unique , counts = np .unique (img , return_counts = True )
527-
528- # find center of image
529- im_center = list (np .divide (img .shape , 2 )) # returns array of two floats
530-
531- minpixel = np .pi * (((minsize / 2 ) * resolution ) ** 2 )
532- maxpixel = np .pi * (((maxsize / 2 ) * resolution ) ** 2 )
522+ # read in file
523+ img , im_name = imread (input_file , use_skimage = True )
533524
534- pbar .update (1 )
525+ # Gets the unique values in the image matrix. Since it is binary, there should only be 2.
526+ unique , counts = np .unique (img , return_counts = True )
535527
536- if len (unique ) == 2 :
537- seg_im = skimage .util .invert (img )
538- pbar .update (1 )
539- label_im , num_elem = skimage .measure .label (seg_im , connectivity = 2 , return_num = True )
540-
541- props = skimage .measure .regionprops (label_image = label_im , intensity_image = img )
542-
543- section_data , bin_im , bbox = section_props (props , im_name , resolution , minpixel , maxpixel , im_center )
544-
545- pad = 100
546- minr = bbox [0 ] - pad
547- minc = bbox [1 ] - pad
548- maxr = bbox [2 ] + pad
549- maxc = bbox [3 ] + pad
550- bbox_pad = [minc , minr , maxc , maxr ]
551- crop_im = Image .fromarray (img ).crop (bbox_pad )
552-
553- if save_img :
554- save_sections (output_path , im_name , crop_im , save_crop = True )
555- save_sections (output_path , im_name , bin_im , save_crop = False )
528+ # find center of image
529+ im_center = list (np .divide (img .shape , 2 )) # returns array of two floats
530+
531+ minpixel = np .pi * (((minsize / 2 ) * resolution ) ** 2 )
532+ maxpixel = np .pi * (((maxsize / 2 ) * resolution ) ** 2 )
556533
557534 pbar .update (1 )
558- else :
559- crop_im = crop_section (img , im_name , resolution , minpixel , maxpixel , im_center )
560- pbar .update (1 )
561-
562- section_data , bin_im = segment_section (crop_im , im_name , resolution , minpixel , maxpixel , im_center )
563535
564- if save_img :
565- save_sections (output_path , im_name , crop_im , save_crop = True )
566- save_sections (output_path , im_name , bin_im , save_crop = False )
567- pbar .update (1 )
536+ if len (unique ) == 2 :
537+ seg_im = skimage .util .invert (img )
538+ pbar .update (1 )
539+ label_im , num_elem = skimage .measure .label (seg_im , connectivity = 2 , return_num = True )
540+
541+ props = skimage .measure .regionprops (label_image = label_im , intensity_image = img )
542+
543+ section_data , bin_im , bbox = section_props (props , im_name , resolution , minpixel , maxpixel , im_center )
544+
545+ pad = 100
546+ minr = bbox [0 ] - pad
547+ minc = bbox [1 ] - pad
548+ maxr = bbox [2 ] + pad
549+ maxc = bbox [3 ] + pad
550+ bbox_pad = [minc , minr , maxc , maxr ]
551+ crop_im = Image .fromarray (img ).crop (bbox_pad )
552+
553+ if save_img :
554+ save_sections (output_path , im_name , crop_im , save_crop = True )
555+ save_sections (output_path , im_name , bin_im , save_crop = False )
556+
557+ pbar .update (1 )
558+ else :
559+ crop_im = crop_section (img , im_name , resolution , minpixel , maxpixel , im_center )
560+ pbar .update (1 )
561+
562+ section_data , bin_im = segment_section (crop_im , im_name , resolution , minpixel , maxpixel , im_center )
563+
564+ if save_img :
565+ save_sections (output_path , im_name , crop_im , save_crop = True )
566+ save_sections (output_path , im_name , bin_im , save_crop = False )
567+ pbar .update (1 )
568+ except :
569+ pass
568570
569571 return section_data
570572
0 commit comments