Creating multiple parallel lines on image

Hi, I’m trying to create multiple vertical parallel lines on an image, each 50 pixels apart, in order to measure the pixel density along each line.

How do I do this?

@birdsnest

Welcome to the Forum!

So I am pretty sure all you have to do is modify the following code a bit:

roiManager("Add");
roiManager("Add");  //add an extra copy to preserve original line
pixelSize=2.18; // also might need to adjust this value depending on your own image scale (pixels per micron)
dist=50; // the space (in micron) between lines
pixelDist = dist*pixelSize; // again - you can change this depending how you want to set the distance between lines... (in pixels)
num = 5; // number of lines you want drawn
for ( i=1; i < num; i++) {
  roiManager("Select", i);
  roiManager("translate", pixelDist, 0); // can adjust things here depending on direction of ROI repeats...
  roiManager("Add");
}
  roiManager("Show All");

I personally got this from this past Forum thread/post. If you want to measure the intensity along those lines… you can take a look at this old Forum post for inspiration. :slight_smile:

Hope this helps at least give you a start!

eta

2 Likes

Good day!

Here is an ImageJ-macro that may get you started:

dir = getDirectory("Where to save the profiles?");
run("Bridge (174K)");
w = getWidth;
h = getHeight;
for ( i=0; i<h; i += 50 ) {
	makeLine( 0, i, w-1, i );
	p = getProfile();
	t = "";
	for (k=0; k<p.length; k++) {
		t += "" + k + "\t" + p[k] + "\n";
	}
	File.saveString( t, dir + "y" + i + ".txt" );
}

Paste the code to an empty macro window (Plugins >> New >> Macro) and run it.

The macro is stand-alone, i.e. it loads a test image from the web (open internet connection required) and writes the pixel values along each line as separate tab-delimited text-files to the chosen directory.

HTH

Herbie

Thank you so much! I tried the following macro, which seems to work somewhat:

run (“Select All”)
roiManager(“Add”);
roiManager(“Add”);
pixelDist = 50;
num = 50;
for ( i=1; i < num; i++) {
roiManager(“Select”, i);
roiManager(“translate”, pixelDist, 0);
roiManager(“Add”);
}
roiManager(“Show All”);
roiManager (“Deselect”);
roiManager (“Multi Plot”)

However this seems to make multiple rectangles overlaying my image; is there a way to have it just make vertical lines? I’d like to be able to select a small rectangular area within a picture, and have this macro make vertical lines every 50 pixels within my selected box, and print out a list of density plot values for each vertical line in that area.

If I delete “Select All” from the above macro and instead make a box myself, this makes multiple rectangles in different areas of the image (not just in the rectangle I selected). Also, it makes a specified number of boxes, instead of drawing lines every 50 pixels up until the end of the selected area. How do I fix this?

Thank you so much for any help… I’m new to this and am greatly appreciative of any advice!!

@birdsnest

Try this guy out:

waitForUser("Draw your rectangular ROI... then click 'OK'.");
roiManager("Add"); // add rectangle to ROI Manager
roiManager("Select", 0); // select it
Roi.getBounds(x, y, width, height); // get dimensions
roiManager("Deselect"); 
roiManager("Delete"); // delete rectangle ROI (we don't need it anymore)

makeLine(x, y, x, y+height); // draw line at beginning of rectangular ROI...
roiManager("Add");

pixelDist = 50;
xPosition = x+width; // end of rectangular ROI
i = 0;
while (x < xPosition) {
roiManager("Select", i);
roiManager("translate", pixelDist, 0);
roiManager("Add");
Roi.getBounds(x,y,width,height); // update x position
i++; // update ROI that is selected
}
roiManager("Show All");
roiManager ("Deselect");
roiManager ("Multi Plot")

It asks the user to draw a rectangular ROI first … then draws the lines inside according to those dimensions. You can also hard-code that rectangle ROI if you want - but just make sure to delete it before drawing the first line ROI - that was causing the issue you got previously…

Hope this helps!

eta :slight_smile:

Sorry for my misunderstanding (my previous macro draws horizontal lines covering the whole image).

The following macro draws vertical lines within a defined selection rectangle and stores the profile values in text files.

dir = getDirectory("Where to save the profiles?");
getSelectionBounds( x, y, w, h );
for ( i=0; i<w; i += 50 ) {
	makeLine( x+i, y, x+i, y+h-1 );
	p = getProfile();
	t = "";
	for (k=0; k<p.length; k++) {
		t += "" + k + "\t" + p[k] + "\n";
	}
	File.saveString( t, dir + "x" + i + ".txt" );
}

Paste the code to an empty macro window (Plugins >> New >> Macro) and run it after you’ve made the selection in your image. The profile values are saved to the directory you are asked for at the start.

HTH

Herbie

@birdsnest

Actually - my last code was a bit off. Check out this ‘better’ code below… it adds a check so you really only add lines within the rectangle (there was an error in my previous version).

waitForUser("Draw your rectangular ROI... then click 'OK'.");
roiManager("Add"); // add rectangle to ROI Manager
roiManager("Select", 0); // select it
Roi.getBounds(x, y, width, height); // get dimensions
roiManager("Deselect"); 
roiManager("Delete"); // delete rectangle ROI (we don't need it anymore)

makeLine(x, y, x, y+height); // draw line at beginning of rectangular ROI...
roiManager("Add");

pixelDist = 50;
xPosition = x+width; // end of rectangular ROI
i = 0;
while (x < xPosition) {
	if ((pixelDist + x) < xPosition){	 // only add ROI lines within rectangular ROI
		roiManager("Select", i);
		roiManager("translate", pixelDist, 0);
		roiManager("Add");
		Roi.getBounds(x,y,width,height); // update x position
		i++; // update ROI that is selected
	}
}
roiManager("Show All");
roiManager("Deselect");
roiManager("Multi Plot");

Note: YOU MAY STILL NEED TO REFINE THIS CODE A BIT TO GET WHAT YOU WANT IN THE END… but at least it’s a start! :slight_smile:

Hope this helps!

eta :slight_smile:

1 Like

Thank you so much! The last one worked perfectly - amazing. Thanks again!

1 Like

Hello toguether!

Ive been tried the codes here, but it is not exacly I am looking for. I will explain the problem. Sorry if it is too long:

I have to do the following work. I have several REM images about 700 (W) x 400 (H) pixels

What I need to do:

  1. Insert a stack
  2. Put horizontal lines in the stack. One per pixel line (each line is a ROI), i.e. 400 lines with 1 x 700 pixels

The first problem: I could not import a ROI table into ROI-Manager, with this amount of data, the best alternative is to do it in Excel and then export in zip.
The excel file has the following format:
1 columm 2 columm
0001 0356
0002 0356
0003 0356
0004 0356
0005 0356
0006 0356
0007 0356
0008 0356
0009 0356
… …
0400 0356

FIJI’s answer is “Line 2 is not the same length as the first line” (I don’t understand it)

The second problem is that I don’t know how to insert the stack (Z) information. Made manually in ROI the format is (0002 is the stack and I have just one):

0002-0001-0356
0002-0002-0356
0002-0003-0356
0002-0005-0356
0002-0006-0356
0002-0007-0356
0002-0008-0356
0002-0009-0356

Once this list is in ROI Manager, I need also

  1. For each ROI in Stack create a dynamic slice
  2. Convert each slice dynamics to binary
  3. Make the Histogram of the 400 binary slices
  4. Export the result of all histogramms preferably together.

If anyone can help me with these problems, it would be of great help. I am testing for days without success. Thanks for any help!

B01 OP 3.TIF (292.1 KB)