Using DOG filter with imagej-ops

Same idea as my previous post : I am trying to use ops to run a DOG filter on a stack of images. The code works but the result is different than the DOG filter provided by the GDSC plugin (http://www.sussex.ac.uk/gdsc/intranet/microscopy/imagej/gdsc_plugins#install).

See the following code that will run DOG filter with ops and with GDSC plugin.

# @ImageJ ij
# @Dataset data
# @ImagePlus imp

from ij.plugin.filter import DifferenceOfGaussians
from ij.plugin import ContrastEnhancer
from ij.plugin import Duplicator

sigma1 = 4.2
sigma2 = 1.25

# IJ Ops version
pixel_type = data.getImgPlus().firstElement().__class__
dog = ij.op().create().img(data.getImgPlus(), pixel_type())

ij.op().run("filter.dog", dog, data.getImgPlus(), sigma1, sigma2)
ij.ui().show("dog", dog)

# GDSC Plugin version
out = Duplicator().run(imp)
f = DifferenceOfGaussians()
f.setup("", out)

for i in range(1, out.getNFrames()+1):
	out.setPosition(i)
	f.run(out.getProcessor(), sigma1, sigma2)

out.show()

Ideally I am looking for a solution to get a result similar to GDSC plugin.

Hi @hadim,

it’s again about the types :wink: Try the following:

converted = ij.op().convert().float32(data.getImgPlus())
dog = ij.op().run("filter.dog", converted, sigma1, sigma2)
ij.ui().show("dog", dog)

Best,

Christian

Thanks @dietzc it’s much better ! But still not ok I am afraid :smiley:

Here you have the modified script :

# @ImageJ ij
# @Dataset data
# @ImagePlus imp

from ij.plugin.filter import DifferenceOfGaussians
from ij.plugin import ContrastEnhancer
from ij.plugin import Duplicator

sigma1 = 4.2
sigma2 = 1.25

# IJ Ops version

converted = ij.op().convert().int32(data.getImgPlus())
dog = ij.op().run("filter.dog", converted, sigma1, sigma2)
ij.ui().show("dog", dog)

# GDSC Plugin version
out = Duplicator().run(imp)
f = DifferenceOfGaussians()
f.setup("", out)

for i in range(1, out.getNFrames()+1):
	out.setPosition(i)
	f.run(out.getProcessor(), sigma1, sigma2)

out.show()

If you test it on the Trackmate sample FakeTracks.tif, you shoud notice two (maybe not related) things :

  1. The shape of the spots appears round with the DOG result from the GDSC Plugin while they look not rounded for the result coming from the OPS. See the screenshot :

  1. Also the histogram does not look exactly the same as the GDSC Plugin. Probably because one is 8-bit while the other one is 32-bit. How could I convert the ops result image to the same type as the input image (with correct scaling if needed).

Hi @hadmin,

Can you try to run the algorithm per slice? I’m afraid you are doing a 3D dog at the moment.

Best,

Christian

Ahah indeed I was doing 3D dog !

Using slice made the trick but I still have type issue. The ouput is 32-bit and I would like to convert to 8-bit but using dog = ij.op().convert().int8(dog) does not help.

From left to right :

  1. Original stack
  2. DOG using ops
  3. DOG using IJ1 plugin

Script used :

# @ImageJ ij
# @Dataset data
# @ImagePlus imp

from ij.plugin.filter import DifferenceOfGaussians
from ij.plugin import ContrastEnhancer
from ij.plugin import Duplicator

from net.imagej.axis import Axes

sigma1 = 4.2
sigma2 = 1.25

# IJ Ops version

converted = ij.op().convert().float32(data.getImgPlus())

# Allocate output memory (wait for hybrid CF version of slice)
dog = ij.op().create().img(converted)

# Create the op
dog_op = ij.op().op("filter.dog", converted, sigma1, sigma2)

# Setup the fixed axis
t_dim = data.dimensionIndex(Axes.TIME)
fixed_axis = [d for d in range(0, data.numDimensions()) if d != t_dim]

# Run the op
ij.op().slice(dog, converted, dog_op, fixed_axis)

# Show it
ij.ui().show("dog", dog)

# GDSC Plugin version
out = Duplicator().run(imp)
f = DifferenceOfGaussians()
f.setup("", out)

for i in range(1, out.getNFrames()+1):
	out.setPosition(i)
	f.run(out.getProcessor(), sigma1, sigma2)

out.show()

I think the problem is that the current converters just copy the values over without normalizing or scaling, @awalter17 is this right?

Once again the solution was clipping the output image to the input type. For the record : http://imagej.net/ImageJ2_Python_Scripts#Apply_DOG_Filter

Thank you @dietzc for the help.