diff --git a/build.gradle.kts b/build.gradle.kts index d4e630e9..2f39a204 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -87,6 +87,9 @@ dependencies { implementation("net.imglib2:imglib2") implementation("net.imglib2:imglib2-roi") + // Needed for Albert's tutorials + implementation("sc.fiji:legacy-imglib1") + // Math dependencies // implementation(commons.math3) // implementation(misc.joml) diff --git a/src/main/kotlin/sc/iview/ImageJMain.kt b/src/main/kotlin/sc/iview/ImageJMain.kt index 05f0ba75..bc9bda5b 100644 --- a/src/main/kotlin/sc/iview/ImageJMain.kt +++ b/src/main/kotlin/sc/iview/ImageJMain.kt @@ -1,6 +1,8 @@ package sc.iview +import org.scijava.script.ScriptService import org.scijava.ui.UIService +import java.io.File object ImageJMain { @Throws(Exception::class) @@ -11,5 +13,11 @@ object ImageJMain { val uiService = context?.service(UIService::class.java) uiService?.showUI() + + val script: String = File("/Users/kharrington/git/scenerygraphics/sciview/src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py").readText() + + var scriptService = context?.service(ScriptService::class.java) as ScriptService + + scriptService.run("sample.py", script, true) } } \ No newline at end of file diff --git a/src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py b/src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py new file mode 100644 index 00000000..cc85353b --- /dev/null +++ b/src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py @@ -0,0 +1,102 @@ +#@ SciView sciview +# Based on https://syn.mrc-lmb.cam.ac.uk/acardona/fiji-tutorial/#find-peaks + +# Load an image of the Drosophila larval fly brain and segment +# the 5-micron diameter cells present in the red channel. + +from script.imglib.analysis import DoGPeaks +from script.imglib.color import Red +from script.imglib.algorithm import Scale2D +from script.imglib.math import Compute +from script.imglib import ImgLib + +from ij import IJ +from net.imglib2.img.display.imagej import ImageJFunctions +from org.scijava.util import ColorRGB +from org.joml import Vector3f + +from net.imglib2.img.array import ArrayImgFactory + +from net.imglib2 import RandomAccessibleInterval +from net.imglib2.type.numeric import ARGBType +from net.imglib2.type.numeric.integer import UnsignedByteType +from java.util import ArrayList + +from graphics.scenery import Sphere + +cell_diameter = 5 # in microns +minPeak = 40 # The minimum intensity for a peak to be considered so. +imp = IJ.openImage("http://samples.fiji.sc/first-instar-brain.zip") + +# Scale the X,Y axis down to isotropy with the Z axis +cal = imp.getCalibration() +scale2D = cal.pixelWidth / cal.pixelDepth +iso = Compute.inFloats(Scale2D(Red(ImgLib.wrap(imp)), scale2D)) + +# Find peaks by difference of Gaussian +sigma = (cell_diameter / cal.pixelWidth) * scale2D +peaks = DoGPeaks(iso, sigma, sigma * 0.5, minPeak, 1) +print "Found", len(peaks), "peaks" + +def split_channels(image): + channels = ArrayList() + num_channels = 4 # Assuming RGBA color model + + for channel_idx in range(num_channels): + channel = ArrayImgFactory(UnsignedByteType()).create(image) + channel_cursor = channel.cursor() + image_cursor = image.cursor() + + while channel_cursor.hasNext(): + image_cursor.next() + alpha = (image_cursor.get().get() >> 24) & 0xFF + red = (image_cursor.get().get() >> 16) & 0xFF + green = (image_cursor.get().get() >> 8) & 0xFF + blue = image_cursor.get().get() & 0xFF + + if channel_idx == 0: + channel_cursor.next().setReal(alpha) + elif channel_idx == 1: + channel_cursor.next().setReal(red) + elif channel_idx == 2: + channel_cursor.next().setReal(green) + elif channel_idx == 3: + channel_cursor.next().setReal(blue) + + channels.add(channel) + + return channels + +# Show the image data +channels = split_channels(ImageJFunctions.wrap(imp)) +scale = Vector3f([cal.getX(1), cal.getY(1), cal.getZ(1)]) + +ch1 = sciview.addVolume(channels[1]) +ch1.setScale(Vector3f(scale)) +sciview.setColormap(ch1, "Red.lut") + +ch2 = sciview.addVolume(channels[2]) +ch2.setScale(Vector3f(scale)) +sciview.setColormap(ch2, "Green.lut") + +ch3 = sciview.addVolume(channels[3]) +ch3.setScale(Vector3f(scale)) +sciview.setColormap(ch3, "Blue.lut") + +# Convert the peaks into points in calibrated image space and display +for peak in peaks: + radius = cal.pixelWidth * 1/scale2D + node = Sphere(radius, 20) + node.spatial().setPosition(Vector3f(peak).mul(scale)) + node.material().setDiffuse(Vector3f(1, 0, 0)) + ch1.addChild(node) + sciview.publishNode(node) + + +ImageJFunctions.show(channels[0]) +ImageJFunctions.show(channels[1]) +ImageJFunctions.show(channels[2]) +ImageJFunctions.show(channels[3]) + + +