Manual image analysis works, but not the macro/batch

imagej

#1

Hello everybody

I am not completely new to ImageJ, but to the board. I hope you can help me since I assume it is more a technical problem. My aim is to analyze the bright structures (focal adhesion) on the first image. I am interested how much of those cover the cell. I am going to describe how I do it manually, since this works fine. However, I have a problem that is does not work as batch nor as normal macro.

Unfortunately, I am restricted to upload only 1 image and 5 links so I tried to work around a bit.

Figure 1: This is the original image

Figure 2: Image is converted into 16 bit

Figure 3: The background is subtracted (sliding paraboloid, rolling ball radius = 50 pixel)

Figure 4: The CLAHE (Contrast Limited Adaptive Histogram Equalization) plugin is used to enhance the constrast (blocksize: 15, histogram bins: 256, maximum slope = 6.00)

Figure 5: The LoG3D plugin is used (Sigma X = 4, Sigma Y = 4)

Figure 6: Threshold is used using the Otsu method

Figure 7: Particles are analyzed according to their size

I used the macro recorder and got following code:

run("16-bit");
run("Subtract Background...", "rolling=50 sliding");
run("CLAHE ", "blocksize=15 histogram=256 maximum=6");
run("LoG 3D", "sigmax=4 sigmay=4");
//run("Threshold...");
setAutoThreshold("Otsu");
run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");

I had to add the sigma values to the LoG3D plugin since the recorder did not write it. The macro runs fine until the threshold method. I figured that out by running the macro with the first command line, then the second, etc.

As it is displayed above I get this image in the end with other measurements than the ones I get running it manually.

Figure 8: Outcome after running the macro

Alternatively, I tried to record the code in JavaScript, but it does not change the outcome. In Macro I also tried to change the code to following:

run("Threshold", "Otsu") 

I hope somebody can help me since I have over 300 images.

Thanks in advance!

Jens


#2

Here is a version of your macro that should work as expected:

run("16-bit");
run("Subtract Background...", "rolling=50 sliding");
run("CLAHE ", "blocksize=15 histogram=256 maximum=6");
title = getTitle;
run("LoG 3D", "sigmax=4 sigmay=4");
selectWindow("LoG of "+title);
setAutoThreshold("Otsu");
run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");

The selectWindow() call causes the macro to wait for the window opened the “LoG 3D” plugin to be displayed.


#3

Thanks a lot Wayne! Works great now.
I added some code of 2 other threads here in the forum which enable to choose the directory and to export the analyzed image and a spreadsheet. In case somebody wants to do something similar, here is the macro:

macro "Focal adhesion analysis" { 
	dir = getDirectory("Choose a Directory "); 
	list = getFileList(dir); 
	setBatchMode(true); 
	for (i=0; i<list.length; i++) { 
		path = dir+list[i]; 
		open(path);
		run("16-bit");
		run("Subtract Background...", "rolling=50 sliding");
		run("CLAHE ", "blocksize=15 histogram=256 maximum=6");
		title = getTitle;
		run("LoG 3D", "sigmax=4 sigmay=4");
		selectWindow("LoG of "+title);
		setAutoThreshold("Otsu");
		run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");
                path2 = dir+File.nameWithoutExtension; 
                saveAs("JPEG", path2+"-bin.jpeg"); 
                close(); 
                saveAs("results", path2+"-results.xls"); 
        }
}

However, I have one question. Does anybody now why Excel tells me that the file could be corrupted? File extension and format do not match, but Excel opens it and the formatting is correct.


#4

You could try saving in .csv (Comma Separated Values) format.


#5

Thanks for the suggestion. I tried it and .csv works without error message, but .xls is more convenient to me. Thanks again for the great support!


#6

For me Excel also complained for .csv files, but if you save it as .tsv (tab-separated value) file it works perfectly and Excel is not complaining anymore.


#7

Thanks biovoxxel, that did the trick! To be honest I did not about that file format.

Unfortunately, something else came to my mind, but it gets a bit too complicated for me.

Is it possible to reopen the original image and apply the resulting outlines from the ROI manager to measure the intensity of the structures? It could discard the first result file since the area would not change. I tried to change the code, but I am not sure how I open exactly the same image which was analysed.

macro "FA area and intensity analysis" { 
  dir = getDirectory("Choose a Directory "); 
  list = getFileList(dir); 
  setBatchMode(true); 
  for (i=0; i<list.length; i++) { 
    path = dir+list[i]; 
    open(path);

    run("16-bit");
    run("Subtract Background...", "rolling=50 sliding");
    run("CLAHE ", "blocksize=15 histogram=256 maximum=6");
    title = getTitle;
    run("LoG 3D", "sigmax=4 sigmay=4");
    selectWindow("LoG of "+title);
    setAutoThreshold("Otsu");
    run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");

    path2 = dir+File.nameWithoutExtension; 
    saveAs("JPEG", path2+"-bin.jpg"); 
**  close();  ** 

**  open(path);  ** 
**  ?  **
**  selectWindow(+title);  ** 
**  run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");  **
**  saveAs("results", path2+"-results.tsv");  ** 
  } 
} 

Apologies that I expand my problem which was actually solved.


#9

You could exchange this line

run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 display clear add");

for the following lines

run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 clear add");    

// this will reload the original image in the same window
run("Revert");

// this measures all existing ROIs
roiManager("Measure");

// this empties the ROI manager so that you do not mix it up with ROIs from other images
roiManager("reset");

You could as well include a command to save your ROIs for each image for documentation and reuse

roiManager("Save", path);

Not sure is this really addresses your problem correctly but I hope it might help you in solving it.


#10

Note that you can branch this thread into a new thread by using the “Reply as linked topic” option that appears in the right margin upon mouseover.

That way, you can accept Wayne’s reply above as the solution, while posing a new question which will have its own solution.


#11

thanks for letting me now, but I cannot find that option. I wanted to send you a PM, but I also can’t find that.
It should look like in the .gif here, right? https://meta.discourse.org/t/reply-as-linked-topic-should-default-to-same-category/26033


#12

Thanks biovoxxel. That helped me a lot.

First it did not work, but then I realized the LoG3D plugin opens a new Windows which can not be reverted. However, it leaves the main window open on which the 16 bit conversion, bg subtraction and contrast enhancements was done. Selecting the main window first and reverting it did the trick. Now I get all the information I need, thanks!

Just for completion, the final code for focal adhesions

macro "Focal adhesion analysis" { 
	dir = getDirectory("Choose a Directory "); 
	list = getFileList(dir); 
	setBatchMode(true); 
	for (i=0; i<list.length; i++) { 
		path = dir+list[i]; 
		open(path);

		run("16-bit");
		run("Subtract Background...", "rolling=50 sliding");
		run("CLAHE ", "blocksize=15 histogram=256 maximum=6");
		title = getTitle;
		run("LoG 3D", "sigmax=4 sigmay=4");
		selectWindow("LoG of "+title);
		setAutoThreshold("Otsu");
		run("Analyze Particles...", "size=1-Infinity circularity=0.05-1.00 clear add");

       		path2 = dir+File.nameWithoutExtension; 
       		saveAs("JPEG", path2+"-bin.jpeg");
		selectWindow(title);
		run("Revert");
		roiManager("Measure");
                close(); 
                saveAs("results", path2+"-results.tsv"); 
                roiManager("reset");
        } 
}

#13

Unfortunately, you have to move your mouse near that area for the option to magically appear.


#14

Specifically you have to mouse over the particular post you want to link to, as each individual reply is a valid linking point… which I assume is why the “Reply as linked topic” links are not permanently visible (as they would fill your screen)

Anyway I updated the welcome page with some resource links for using Discourse in general, since it’s still new tech. Maybe there’s a better place for that info though? Open to suggestions…


#15

Unfortunately, it does no appear in my browser (chrome, also edge was tested).
That is the reason why I was looking for a link and where to expect. It just does not appear, hovering
does not help.


#16

Sorry to hear that! It works for me in Chrome 45.0.2454.101 on OS X.

Click the person’s name on any of their posts. A box will pop up with (among other things) a “Message” button. But I would encourage you to stay away from PMs unless your message needs to be private. From the Help page:

The ImageJ community firmly believes in public discussion of the software. In this way, each question benefits not only the one asking, but everyone in the community, including everyone who subsequently does a web search on the same topic.