Search code examples
pythonlinejythonimagejroi

How to extract a line of pixels (roi) from video, combined lines in a single .tif image?


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


Solution

  • 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