Commit 34b8bd09 authored by rhaase's avatar rhaase

added exercise solution

parent eb253d46
// This ImageJ macro analyses a folder of 2D MRI
// images showing a banana slice by slice. It measures
// the area of the banana and writes it in a CSV file.
//
// Usage: run it in Fiji (http://fiji.sc)
//
// Robert Haase, MPI CBG, rhaase@mpi-cbg.de
// April 2019
//
folder = "C:/structure/teaching/lecture_applied_bioimage_analysis_2020/03_Feature_extraction_and_ImageJ_Macro/example_images/banana/"
// access the folder
filelist = getFileList(folder);
for (i = 0; i < lengthOf(filelist); i++) {
// get the nth entry from the filelist array
file = filelist[i];
if (endsWith(file, ".tif")) {
// open the image
open(folder + file);
// segment the object in the image
setAutoThreshold("Otsu dark");
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
// measure the position of this one single object
run("Set Measurements...", "area redirect=None decimal=3");
run("Create Selection");
run("Measure");
// close the image
close();
}
}
// save the positions as CSV to disc
folder = "C:/structure/teaching/lecture_applied_bioimage_analysis_2020/10_Method_comparison/exercise_solution/"
result_filename = "otsu_imagej.csv"
saveAs("Results", folder + result_filename);
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[199.127197265625], [469.05517578125], [782.623291015625], [990.29541015625], [1100.15869140625], [1150.970458984375], [1173.553466796875], [1184.6923828125], [1190.338134765625], [1185.302734375], [1189.27001953125], [1192.169189453125], [1206.97021484375], [1242.98095703125], [1277.008056640625], [1309.814453125], [1334.8388671875], [1371.76513671875], [1375.1220703125], [1315.155029296875], [1172.79052734375], [917.66357421875], [587.005615234375], [224.30419921875], [128.47900390625]]\n"
]
}
],
"source": [
"from skimage.io import imread\n",
"from skimage import filters\n",
"from skimage import measure\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import os\n",
"directory = 'C:/structure/teaching/lecture_applied_bioimage_analysis_2020/03_Feature_extraction_and_ImageJ_Macro/example_images/banana/'\n",
"area_per_pixel = 0.3906250 * 0.3906250\n",
"\n",
"# get list of files in the given folder\n",
"file_list = os.listdir(directory)\n",
"\n",
"# create an empty table\n",
"table = []\n",
"\n",
"# go through the folder file by file\n",
"for filename in file_list:\n",
" if filename.endswith(\".tif\"):\n",
" # open image\n",
" image = imread(directory + filename)\n",
" \n",
" # determine threshold for the image\n",
" threshold = filters.threshold_otsu(image)\n",
" \n",
" # apply the threshold\n",
" thresholded_image = image >= threshold\n",
"\n",
" # area equals the sum of white pixels multiplied with pixel size\n",
" pixel_count = np.sum(thresholded_image)\n",
" area = pixel_count * area_per_pixel\n",
" \n",
" # make a row in our table\n",
" row = [area]\n",
" table = table + [row]\n",
"\n",
"print(table)\n",
"\n",
"np_table = np.asarray(table)\n",
"np.savetxt(\"otsu_python.csv\", np_table, delimiter=',', header=\"Area\")\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQEAAAD8CAYAAAB3lxGOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAATVUlEQVR4nO3de5BU5ZnH8e8zzcAIw+CMwACCgggSjIqIoGsumqwbpUzQZHUxlRRrSNRKTDSby6LZitnamEpiSLLJRlfcaNCYGIMmuimiC1SMGgkihigEgeGiIISrXIVhLs/+0aexAz1M05c50+f9faq6+vTbp6efl8tvzjl9+jzm7ohIuKriLkBE4qUQEAmcQkAkcAoBkcApBEQCpxAQCVzZQsDMLjOzlWbWZGYzyvU+IlIcK8d5AmaWAlYBlwIbgcXAte7+l5K/mYgUpVxbAhOBJndf6+6HgIeBKWV6LxEpQo8y/dyTgQ1ZjzcCkzpauaf18hr6lKkUEQHYy5vb3X3AkePlCgHLMfY3+x1mdj1wPUANvZlk7y9TKSICMN/nvJZrvFy7AxuBYVmPhwKbsldw91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EpvZNmA/sD3uWsqoP8meHyR/jpU+v1PdfcCRg90iBADM7MVc33VOiqTPD5I/x6TOT7sDIoFTCIgErjuFwKy4CyizpM8Pkj/HRM6v2xwTEJF4dKctARGJQewhYGaXRd2Lm8xsRtz1FCpqxbbVzJZljTWY2TwzWx3d12c9V1Gdm81smJn9zsxWRN2pb47GkzTHjjpwJ2aOObl7bDcgBawBTgN6An8GxsZZUxFzeQ8wHliWNfZtYEa0PAP4VrQ8NpprL2BE9GeQinsOncxvMDA+Wu4LrIrmkaQ5GlAbLVcDi4ALkjTHXLe4twQmAk3uvtbdDwEPk+5qXHHc/Rlg5xHDU4DZ0fJs4Mqs8Yrq3Ozum939pWh5L7CCdKPZJM3RPXcH7sTMMZe4QyDpHYwb3X0zpP8TAQOj8Yqet5kNB84l/ZsyUXPsoAN3ouZ4pLhDIK8OxglUsfM2s1rgUeAWd99zrFVzjHX7OXruDtwdqcg5HinuEMirg3EF25Jp3Brdb43GK3LeZlZNOgAecvfHouFEzTHDszpwk9A5ZsQdAouBUWY2wsx6AlNJdzVOiieAadHyNODxrPGK6txsZgb8GFjh7t/NeipJc8zZgZsEzTGnuI9MApNJH2leA3wl7nqKmMfPgc1AC+nfENOBk4AFwOroviFr/a9Ec14JXB53/XnM712kN3VfBpZGt8kJm+PZwJ+iOS4DvhqNJ2aOuW46Y1AkcHHvDohIzBQCIoFTCIgETiEgEjiFgEjgFAIigVMIiAROISASuP8Hdq2uaQQY8IsAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9012]\n"
]
},
{
"data": {
"text/plain": [
"9012"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from skimage.io import imread\n",
"from skimage import filters\n",
"from skimage import measure\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# load file\n",
"filename = \"https://git.mpi-cbg.de/rhaase/lecture_applied_bioimage_analysis_2020/raw/master/03_Feature_extraction_and_ImageJ_Macro/example_images/banana/banana0020.tif\"\n",
"image = imread(filename)\n",
"\n",
"# threshold it\n",
"threshold = filters.threshold_otsu(image)\n",
"thresholded_image = image >= threshold\n",
"\n",
"# visualise the thresholded image\n",
"plt.imshow(thresholded_image)\n",
"plt.show()\n",
"\n",
"# assuing white pixels have value 1 and black pixes have value 0,\n",
"# the area in a binary image is equal to the sum of the whole image\n",
"np.sum(thresholded_image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment