<commit_before># Watershed boundry detection function

import cv2
import numpy as np
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
from . import print_image
from . import plot_image
from plantcv.apply_mask import apply_mask
from plantcv.dev.color_palette import color_palette



def watershed_segmentation(device, img, mask, distance=10, filename=False, debug=None):
    """Uses the watershed algorithm to detect boundry of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    device = device number. Used to count steps in the pipeline
    img    = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask = binary image, single channel, object in white and background black
    distance = min_distance of local maximum
    filename = if user wants to output analysis images change filenames from false
    debug  = None, print, or plot. Print = save to file, Plot = print to screen.

    Returns:
    device = device number
    watershed_header    = shape data table headers
    watershed_data      = shape data table values
    analysis_images = list of output images

    :param img: numpy array
    :param marker: numpy array
    :param device: int
    :param debug: str
    :return device: int
    :return marker: numpy array
    """
    device += 1

    dist_transform = cv2.distanceTransform(mask, cv2.cv.CV_DIST_L2, maskSize=0)

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    #markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    #markers = ndi.label(localMax, structure=ndi.generate_binary_structure(2, 2))[0]
    markers = ndi.label(localMax)[0]
    markers=np.array(markers,dtype=np.int32)
    cv2.watershed(img, markers)

    img1 = np.copy(img)

    for x in np.unique(markers):
        rand_color = color_palette(len(np.unique(markers)))
        img1[markers == x] = rand_color[x]

    device, img2 =apply_mask(img1, mask, 'black', device, debug=None)
<commit_msg>changed to skimage watershed<commit_after># Watershed boundry detection function

import cv2
import numpy as np
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
from skimage import morphology
from skimage.morphology import watershed
from . import print_image
from . import plot_image
from plantcv.apply_mask import apply_mask
from plantcv.dev.color_palette import color_palette



def watershed_segmentation(device, img, mask, distance=10, filename=False, debug=None):
    """Uses the watershed algorithm to detect boundry of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    device = device number. Used to count steps in the pipeline
    img    = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask = binary image, single channel, object in white and background black
    distance = min_distance of local maximum
    filename = if user wants to output analysis images change filenames from false
    debug  = None, print, or plot. Print = save to file, Plot = print to screen.

    Returns:
    device = device number
    watershed_header    = shape data table headers
    watershed_data      = shape data table values
    analysis_images = list of output images

    :param img: numpy array
    :param marker: numpy array
    :param device: int
    :param debug: str
    :return device: int
    :return marker: numpy array
    """
    device += 1

    dist_transform = cv2.distanceTransform(mask, cv2.cv.CV_DIST_L2, maskSize=0)

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    #markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    markers = ndi.label(localMax, structure=ndi.generate_binary_structure(2, 2))[0]
    #markers = ndi.label(localMax)[0]
    labels = watershed(-dist_transform, markers, mask=mask)

    img1 = np.copy(img)

    for x in np.unique(labels):
        rand_color = color_palette(len(np.unique(labels)))
        img1[labels == x] = rand_color[x]

    device, img2 =apply_mask(img1, mask, 'black', device, debug=None)

{'fraction_non_alphanumeric': 0.08405525638023882, 'fraction_numerical': 0.010067899789276516, 'mean_word_length': 3.7519466073414907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807986', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1261, 'n_words': 446}