1

I am new to programming and I am trying to use ImageJ and Jython to extract a single line from a video and combine the lines into a time progression. I am trying to create a videokymogram (i.e. http://www.kymography.com/supp_demo.html)!

My attempt goes like this:

from ij import ImagePlus, IJ
from ij.process import FloatProcessor

img = IJ.getImage() 
roi = img.getRoi()
StackSize = 100  #img.getImageStackSize() 

pixels = roi.getPixels()
Length = len(pixels)
Width = 1

total_pixels = [[0] *len(pixels)] * StackSize
t_pixels = []


for j in range (1, StackSize):
    img.setSlice (j)
    roi = img.getRoi()
    pixels = roi.getPixels()
    for i in xrange (len(pixels)):
        pixels [i] = pixels [i] 
    total_pixels[j-1] = pixels 

fp = FloatProcessor (Length, StackSize,total_pixels)
imp = ImagePlus ("White Noise", fp) 
imp.show()

However it returns: TypeError: ij.process.FloatProcessor(): 3rd arg can't be coerced to double[], int[], float[]

Any tips on how to fix that. I could maybe iterate a text file appending the pixels variable but I don't know how to do it. Any help is welcomed. BTW, if you want to try it, you can use Fiji's Fly Brain sample.

Thank you very much

Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51

1 Answers1

0

The error you get suggests that total_pixels is not an array of type double[], int[] or float[], which is needed for any of the constructors of the FloatProcessor class.

A nice way to build Java arrays in Python is using jarray, the Jython module for Java arrays, as outlined in the Jython Scripting documentation of the Fiji wiki.

I modified your code, which also needed some fixing within the for loops:

from ij import ImagePlus, IJ
from ij.process import FloatProcessor
from jarray import zeros

img = IJ.getImage() 
roi = img.getRoi()
stackSize = img.getImageStackSize() 

pixels = roi.getPixels()
length = len(pixels)

total_pixels = zeros(length * stackSize, 'f')

for j in xrange (stackSize):
    img.setSlice (j+1)
    roi = img.getRoi()
    pixels = roi.getPixels()
    for i in xrange (length):
        total_pixels[j*length+i] = pixels [i]

fp = FloatProcessor (length, stackSize, total_pixels)
imp = ImagePlus ("White Noise", fp) 
imp.show()

FWIW, other people have used ImageJ to work with kymographs: you can find a tutorial on the Fiji wiki as well as some plugins producing kymographs

Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
  • Thank you very much for fixing the script. Also thanks for the tip!! – Always_learning Jul 16 '14 at 11:06
  • Hi Jan, I am trying to save the new image of the kymograms into a folder according to [http://www.ini.uzh.ch/~acardona/fiji-tutorial/index.html#s1] using: `imp = IJ.getImage() fs = FileSaver (imp) folder = "C:/Users/myfolder/Desktop/ImageJ_files" filepath = folder + "/" + "Videokymogram.tiff" fs.saveAsTiff(filepath)' It produces an empty image with nothing inside. Is it possible to fix it? thanks once again – Always_learning Jul 16 '14 at 11:52
  • Try `IJ.saveAsTiff(imp, path)` (see the [javadoc for Filesaver.saveAsTiff](http://jenkins.imagej.net/job/ImageJ1-javadoc/javadoc/ij/io/FileSaver.html#saveAsTiff%28java.lang.String%29)). If that doesn't work, just open a new question with more details. Also, consulting the [ImageJ mailing list](http://imagej.net/list.html) often helps, since there are many more ImageJ expert readers than on stackoverflow. – Jan Eglinger Jul 16 '14 at 13:37