{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"using FileIO\n",
"using Images\n",
"using ImageSegmentation\n",
"using Plots\n",
"using GaussianMixtures\n",
"using Statistics"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"using Profile"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"text/plain": [
"embedxml"
]
},
"execution_count": 185,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"include(\"tiffxml.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"img3d1 = load(\"../mRNA_confocal_hamamatsu-60X-TIRF/20200316_result/s35-c1.tiff\");"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"img3d2 = load(\"../mRNA_confocal_hamamatsu-60X-TIRF/20200316_result/s3_c5.tiff\");"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"s3c2 = load(\"../mRNA_confocal_hamamatsu-60X-TIRF/20200316_result/s3_c2.tiff\");"
]
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"text/plain": [
"extract3dnucleus (generic function with 1 method)"
]
},
"execution_count": 252,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function create3dmask(zstack)\n",
" mask = zeros(size(zstack))\n",
" thresholds_z = [real(yen_threshold(zstack[:, :, i])) for i in 1:20]\n",
" threshold_3d = median(thresholds_z)\n",
" #print(threshold_3d)\n",
" mask = opening(zstack .> threshold_3d)\n",
" mask, threshold_3d\n",
"end\n",
"\n",
"function extract3dnucleus(stack)\n",
" z_depth = 20\n",
" t_length = size(stack)[3] ÷ z_depth\n",
" nucleus = zeros(size(stack))\n",
" thresholds = zeros(t_length)\n",
" nucleus_3dmask = zeros(size(stack)[1], size(stack)[2], z_depth)\n",
" for i in 1:t_length\n",
" nucleus_3dmask, thresholds[i] = create3dmask(stack[:, :,(i-1)*20+1:20*i])\n",
" nucleus[:,:,(i-1)*20+1:20*i] = nucleus_3dmask .* stack[:, :,(i-1)*20+1:20*i]\n",
" end\n",
" nucleus, thresholds\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 251,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0028534370.00284580750.0027771420.00278477140.00275425330.00269321720.00272373530.00265506980.00268558790.00263218120.00264744040.00259403370.0025406270.00256351570.0025101090.0024490730.0024490730.00244144350.00246433220.00246433220.00245670250.00248722060.00247196150.0024490730.0024338140.0024032960.00242618450.00238803680.00241092550.00241855490.0024338140.00241092550.00244144350.00244144350.00242618450.00246433220.00248722060.00245670250.0024338140.0024338140.00239566640.0023727780.00235751880.00230411230.00234988940.0023117420.0022812240.00232700070.00229648270.002342260.00234988940.00238040740.00245670250.0024490730.00235751880.00242618450.00238040740.00238040740.00228885330.00230411230.00236514840.00233463030.00241092550.00239566640.00242618450.00241092550.0024490730.00242618450.00242618450.00242618450.0024032960.00241855490.00238803680.0024795910.00239566640.00238040740.00235751880.002342260.00234988940.00233463030.002342260.00231937130.00231937130.00231937130.00231937130.0023117420.00231937130.00230411230.00233463030.00233463030.00230411230.00230411230.00232700070.00233463030.00231937130.00230411230.00232700070.00231937130.00231937130.00229648270.00230411230.00229648270.00228885330.00227359430.00227359430.00225070580.00223544660.00224307620.00223544660.00222781720.00222018780.00222781720.00220492850.00220492850.00220492850.00219729920.00217441050.0021057450.0021667810.00208285640.0020599680.0020599680.00214389250.0021515220.00215915170.00215915170.0021057450.002136263106.595743 seconds (808.42 k allocations: 28.513 GiB, 2.48% gc time)\n",
" 47.893488 seconds (71 allocations: 7.010 GiB, 0.08% gc time)\n",
"Embedding OME-XML 1 image files updated\n",
" 20.520845 seconds (5.90 k allocations: 532.328 KiB)\n"
]
}
],
"source": [
"@time nucleus_all, threshold_all = extract3dnucleus(s3c2);\n",
"@time save(File(format\"TIFF\", \"s5-c2_clear.ome.tiff\"), N0f16.(nucleus_all))\n",
"x, y, z_all = size(nucleus_all)\n",
"@time embedxml(x, y, 20, z_all÷20, \"s5-c2_clear.ome.tiff\")"
]
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(threshold_all)"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 81.481933 seconds (611.82 k allocations: 22.068 GiB, 2.28% gc time)\n",
" 33.984208 seconds (71 allocations: 5.421 GiB, 0.04% gc time)\n",
"Embedding OME-XML 1 image files updated\n",
" 16.292093 seconds (4.70 k allocations: 420.484 KiB)\n"
]
}
],
"source": [
"@time nucleus_all_, threshold_all_ = extract3dnucleus(img3d2);\n",
"@time save(File(format\"TIFF\", \"img3d2_clear.ome.tiff\"), N0f16.(nucleus_all_))\n",
"x, y, z_all = size(nucleus_all_)\n",
"@time embedxml(x, y, 20, z_all÷20, \"img3d2_clear.ome.tiff\")"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 255,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(threshold_all_)"
]
},
{
"cell_type": "code",
"execution_count": 258,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 87.367222 seconds (190.57 k allocations: 23.826 GiB, 1.66% gc time)\n",
" 37.714894 seconds (71 allocations: 5.860 GiB, 0.03% gc time)\n",
"Embedding OME-XML 1 image files updated\n",
" 18.094896 seconds (5.04 k allocations: 451.719 KiB)\n"
]
}
],
"source": [
"@time nucleus_all__, threshold_all__ = extract3dnucleus(img3d1);\n",
"@time save(File(format\"TIFF\", \"s35-c1_clear.ome.tiff\"), N0f16.(nucleus_all__))\n",
"x, y, z_all = size(nucleus_all__)\n",
"@time embedxml(x, y, 20, z_all÷20, \"s35-c1_clear.ome.tiff\")"
]
},
{
"cell_type": "code",
"execution_count": 259,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 259,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(threshold_all__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"Collapsed": "false"
},
"source": [
"# 2D Histogram"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n"
]
}
],
"source": [
"threshold = [real(yen_threshold(s3c2[:, :, i])) for i in 1:20 ];\n",
"threshold_2d = real(median(threshold));\n",
"plot()\n",
"for i in 1:20\n",
" edge, count = build_histogram(s3c2[:,:,i])\n",
" plot!(edge[2:end], count[2:end])\n",
"end\n",
"plot!(fill(threshold_2d, 2), [0, 1e4])\n",
"annotate!([threshold_2d, 1e4, \"$threshold_2d\"])"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(threshold, marker=:dot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"Collapsed": "false"
},
"outputs": [],
"source": [
"filtered = zeros(700, 700, 20);\n",
"filtered = opening(s3c2[:, :, 1:20].>median(threshold)).*s3c2[:, :, 1:20]\n",
"save(\"3dsegmentation.tiff\", N0f16.(filtered));\n",
"save(\"3dsegmentation3d_opeing.tiff\", N0f16.(filtered.*opening(filtered.>0)))"
]
},
{
"cell_type": "markdown",
"metadata": {
"Collapsed": "false"
},
"source": [
"# 3D Histogram "
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {
"Collapsed": "false"
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n",
"GKS: character ignored due to unicode error\n"
]
}
],
"source": [
"edge, count = build_histogram(s3c2[:,:,1:20]);\n",
"plot(edge[2:end], count[2:end])\n",
"#plot!(fill(median(threshold), 10), 0e5:2e4:1.9e5)\n",
"threshold_3d_ = real(yen_threshold(s3c2[:, :,1:20]))\n",
"plot!(fill(threshold_3d_, 2), [0, 1e5]) #, text=\"real(yen_threshold(s3c2[:, :,1:20]))\")\n",
"annotate!([threshold_3d_, 1e5, \"$threhold_3d_\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.4.0",
"language": "julia",
"name": "julia-1.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}