Skip to content
Snippets Groups Projects
Commit 43b3b1d4 authored by rhaase's avatar rhaase
Browse files

add solutions for exercises 07

parent c6157d61
Branches master
No related tags found
1 merge request!5Tracking
File added
// blob segmentation of images in folders
//
// This script analyses folders full of image with blobs and measures their average size
//
// Author: Robert Haase, rhaase@mpi-cbg.de
// May 2019
//
///////////////////////////////////
folder = "C:/structure/teaching/lecture_applied_bioimage_analysis/07_examples/example_images/"
function main() {
run("Clear Results");
processFolder(folder + "condition_A/");
saveAs("Results", folder + "ResultsA.xls");
run("Clear Results");
processFolder(folder + "condition_B/");
saveAs("Results", folder + "ResultsB.xls");
run("Clear Results");
}
function processFolder(foldername) {
filelist = getFileList(foldername);
number_of_files_in_folder = lengthOf(filelist);
// go through all images, segment them and collect results in a table
for (i = 0; i < number_of_files_in_folder; i++) {
open(foldername + filelist[i]);
processImage();
close();
}
}
function processImage() {
// remoive noise
run("Gaussian Blur...", "sigma=5");
// segment
setAutoThreshold("Huang dark");
setOption("BlackBackground", true);
run("Convert to Mask");
// analyze
run("Set Measurements...", "area redirect=None decimal=3");
run("Analyze Particles...", "display");
}
main() {
}
\ No newline at end of file
File added
# blobs segmentation
#
# This script processes a folder with images containing blobs.
# It measures the area of all blobs and save them to disc.
#
# Author: Robert Haase, rhaase@mpi-cbg.de
# May 2019
#
#################################33
import os
from tifffile import imread
from skimage import filters
from skimage import measure
import pandas as pd
# set which directory should be processed
directory = 'C:/structure/teaching/lecture_applied_bioimage_analysis/07_examples/example_images/'
blur_sigma = 5
# setup an empty table
table = [[], [], [], [], []]
def main():
processFolder(directory + "condition_A/");
processFolder(directory + "condition_B/");
saveResults(table, directory + "output_py.csv")
# go through all tif files in a given folder and analyse them
def processFolder(folder):
file_list = os.listdir(folder)
for filename in file_list:
if filename.endswith(".tif"):
processImage(folder + filename)
# open a given image and process it
def processImage(filename):
# open image
image = imread(filename)
print(filename)
# Gaussian blur
blurred_image = filters.gaussian(image, blur_sigma)
# Thresholding
threshold = filters.threshold_otsu(blurred_image)
print(threshold)
thresholded_image = blurred_image >= threshold
# run connected components analysis
label_image = measure.label(thresholded_image)
# analyse objects
properties = measure.regionprops(label_image)
count = 1
# go through all regions and save results in a table
for property in properties:
y, x = property.centroid
table[0].extend([filename])
table[1].extend([count])
table[2].extend([x])
table[3].extend([y])
table[4].extend([property.area])
count = count + 1
# save a table as CSV file to disc
def saveResults(table, filename):
data_frame = pd.DataFrame(table, ["Filename", "Number", "X", "Y", "Area"])
data_frame = data_frame.transpose()
data_frame.to_csv(filename)
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment