<jupyter_start><jupyter_text>Cell counting This notebook is a first draft of a human-in-the-loop cell counting system.It works by loading an image, then applying the following techniques:- Contrast stretching to remove extremely bright and dark patches- Top-hat filtering to remove generic background clutter- Blob detection to identify potential cellsThe parameters for the above are selected by the user using `ipywidgets` sliders.Once the user is satisfied with the marked locations, they can be exported to an XML file for manual corrections in Fiji.<jupyter_code>from skimage import exposure, feature, filters, io, morphology
from ipywidgets import widgets, interactive, fixed, interact_manual
from collections import Counter
import functions

%matplotlib inline
img, name = functions.load_and_get_name_of_image(cmap='viridis')<jupyter_output><empty_output><jupyter_text>Image preparation Tweak the sliders until you end up with bright blobs on a clean dark background. `lower` and `upper` are the cutoffs for contrast stretching. `selem_size` determines the maximum size, in pixels, of objects that will be removed by top-hat filtering. For our sample image, try values of around 80, 95 and 3 respectively.<jupyter_code>adjusted_image = interactive(functions.adjust_image,
                             image=fixed(img), 
                             cmap=fixed('viridis'),
                             lower_thresh=(0,100,5),
                             upper_thresh=(0,100,5),
                             filter_size=(0,10,1)
                            )
adjusted_image<jupyter_output><empty_output><jupyter_text>Blob detection This section takes the processed image from above and applies a blob detection technique to it. Tweak the two sliders until as many cells as possible are ringed, with as few false positives. `sigma` determines the upper limit for blob size and `threshold` effectively determines how bright something needs to be before it's marked as a blob.<jupyter_code>hotspots = interactive(functions.detect_blobs,
                       original_image=fixed(img),
                       processed_image=fixed(adjusted_image.result),
                       max_sigma=(0,100,5),
                       threshold=(0.01,1,0.01),
                       continuous_update=False
                      )
hotspots<jupyter_output><empty_output><jupyter_text>Some settings can produce blob markers of different sizes. This could be useful for identifying false positives in the future. Run the cell once you're happy with the slider positions to get a size breakdown.<jupyter_code>for val,cnt in Counter(hotspots.result[:,2]).most_common():
    print("{}: {}".format(round(val,2), cnt))
xy_coords = hotspots.result[:,:2].tolist() #dump out coordinates<jupyter_output><empty_output><jupyter_text>XML output This exports the coordinates in an XML file that can be loaded by the ImageJ cell counter plugin. Click `Run Interact` to save the file.<jupyter_code>interact_manual(functions.save_for_imagej,
                coords=fixed(xy_coords),
                name=fixed(name)
               )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06892150606253988, 'fraction_numerical': 0.010848755583918315, 'mean_word_length': 3.4279661016949152, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '365854', 'license': 'MIT', 'n_tokens_mistral': 837, 'n_tokens_neox': 787, 'n_words': 304, 'path': 'previous_work/counter.ipynb', 'repo_name': 'orangewaxcap/cellcounter', 'size': '3134'}