Get line profile with imgLib2 methods

Hi all,
I would like to get the grey values along a line defined by the two endpoints using imgLib2 methods. Something such as ImageProcessor has implemented with:

I think that Views.interpolate is necessary to get real value access, but I don’t know how to set the interval on a line that is aligned at any angle.

Many thanks in advance,
Helmut

The class DefaultWritableLine works, see example below. But number of grey value points is often extremely small (depending on the angle). I think, that this is because of the rastering in Views.raster or in Regions.sample.

For an 512x512 image I get:
0° 511 data points
1° only 3 data points!!!
2° only 2 data points!!!

double x1;
double y1;
double x2;
double y2;
float[] profile;
DefaultWritableLine line = new DefaultWritableLine(new double[]{x1, y1}, new double[]{x2,  y2}, true);
			
	IterableRegion<BoolType> onLine = Regions.iterable(
					           Views.interval(
					             Views.raster(Masks.toRealRandomAccessible(line)),
					             Intervals.smallestContainingInterval(line)));
									

					Cursor<FloatType> cursor = Regions.sample(onLine, iv).cursor();
					float = new float[(int) (onLine.size())];
					int s = 0;
					while (cursor.hasNext()) {
						cursor.fwd();			
						profile[s] = cursor.get().get();
						s += 1;
					}

In Fiji, “Analyze/Plot profile” does it right, but were can I find the source code for that functionality?

Any help is welcome,
Best wishes,
Helmut

That’s the ImageJ1 implementation. If you use the ImageJ search bar, type Plot Profile and then press the Source button, it will point you there:


Thanks for sharing your code example. I think the *Line classes in imglib2-roi aren’t well suited for the task of generating a line profile on a rastered image, unfortunately.

You might have more luck with BresenhamLine from imglib2-algorithm-gpl, there’s also an example in that repository:

But looking at how the Bresenham algorithm works (as it’s intended for drawing a line onto an image), I am not sure if it’s of any help for regularly sampling a line space…

Maybe @tinevez, @tpietzsch or @ctrueden can give better advice here.

2 Likes

Thank you so much @imagejan for your reply!
The Bresenham class solved that problem. I get now 511 grey values for every profile.

Thanks again,
Helmut

Cool! Would you mind sharing your final code? I would also be interested whether the values you get are a real line profile (i.e. interpolated on regularly sampled positions along the line), or you get the actual pixel positions “close to” that ideal line (but not necessarily equally sampled - if that makes a difference in practice at all).

3 Likes

Hi @imagejan,

Of course, here is the code:

import java.util.ArrayList;
import net.imglib2.Point;
import net.imglib2.img.Img;
import net.imglib2.algorithm.region.BresenhamLine;
import ij.gui.Plot;


private static Img<FloatType> image;

Point point1 = new Point(x1, y1);
Point point2 = new Point(x2, y2);

BresenhamLine<FloatType> line = new BresenhamLine<>(image, point1, point2);
ArrayList<long[]> coords = BresenhamLine.generateCoords(point1, point2);
					
float[] axisY = new float[(int)(coords.size())];
float[] axisX = new float[(int)(coords.size())];
					
int s = 0;
while (line.hasNext()) {
	line.fwd();			
	axisY[s] = line.get().get();
	axisX[s] = (float)(s +1);
	s += 1;
}

Pot profile  = new Plot("Grey value profile", "Pixel number", "Grey value");
profile.addPoints(axisX, axisY, 7);
profile.show();

@imagejan I would also be interested whether the values you get are a real line profile (i.e. interpolated on regularly sampled positions along the line), or you get the actual pixel positions “close to” that ideal line (but not necessarily equally sampled - if that makes a difference in practice at all).

I think interpolation is not used:

According to Bresenham algorithm : “Bresenham’s algorithm chooses the integer y corresponding to the pixel center that is closest to the ideal (fractional) y for the same x,…

Nevertheless, grey value profiles seem to be very good approximations to the real line.

Best wishes,
and thanks again,
Helmut

3 Likes