<jupyter_start><jupyter_text>Example from [scikit-image plot-label](https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_label.html)- define data<jupyter_code>import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb
from skimage.transform import resize


image = data.coins()[50:-50, 50:-50]
image = resize(image, (256, 256))

# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))

# remove artifacts connected to image border
cleared = clear_border(bw)<jupyter_output><empty_output><jupyter_text>Running scikit-image<jupyter_code># label image regions
label_image = label(cleared.copy())
# to make the background transparent, pass the value of `bg_label`,
# and leave `bg_color` as `None` and `kind` as `overlay`
image_label_overlay = label2rgb(label_image, image=image, bg_label=0)<jupyter_output><empty_output><jupyter_text>Running cc_torch<jupyter_code>import torch
from cc_torch import connected_components_labeling

cleared_torch = torch.from_numpy(cleared.copy()).to("cuda", torch.uint8)

cc_out = connected_components_labeling(cleared_torch)
cc_out = cc_out.cpu().numpy()

cc_image_overlay = label2rgb(cc_out, image=image, bg_label=0)<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code>fig, axes = plt.subplots(1, 2, figsize=(10, 6))

def show_ax(ax, title, image, label):
    ax.set_title(title)
    ax.imshow(image)

    for region in regionprops(label):
        # take regions with large enough areas
        if region.area >= 100:
            # draw rectangle around segmented coins
            minr, minc, maxr, maxc = region.bbox
            rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                      fill=False, edgecolor='red', linewidth=2)
            ax.add_patch(rect)

    ax.set_axis_off()
    
show_ax(axes[0], "scikit-image", image_label_overlay, label_image)
show_ax(axes[1], "cc_torch", cc_image_overlay, cc_out)
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10717449069973428, 'fraction_numerical': 0.0141718334809566, 'mean_word_length': 5.07258064516129, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '14734', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 789, 'n_tokens_neox': 758, 'n_words': 169, 'path': 'example.ipynb', 'repo_name': 'zsef123/Connected_components_PyTorch', 'size': '2258'}