Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Starting to port Albert's scripts #505

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ dependencies {
implementation("net.imglib2:imglib2")
implementation("net.imglib2:imglib2-roi")

// Needed for Albert's tutorials
implementation("sc.fiji:legacy-imglib1")

// XDG support
implementation("dev.dirs:directories:26")

Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/sc/iview/ImageJMain.kt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
}
102 changes: 102 additions & 0 deletions src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py
Original file line number Diff line number Diff line change
@@ -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)
new_pos = Vector3f(peak).mul(Vector3f([1.0/scale2D, 1.0/scale2D, 1.0]))
node.spatial().setPosition(new_pos)
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])