The usual way to modify a macro recorded with a specific image to make it work with any generic image normally involves two things:
- Identifying and storing the name of the generic image
- Replacing every instance in which the name of the original image appears with the generic name previously stored
The first step is accomplished by storing the title (name) of the current image in a variable:
In your macro, the second part has to take into account that Split Channels adds a different prefix to the original title for every channel: “C1-”, “C2-”, etc., so you have to concatenate these prefixes with the contents of your title variable. By inspecting the code provided, in this case you end up having only three channels (original RGB image).
There are many ways to perform parts 1 & 2, the simplest one being:
img_name=getTitle(); //stores the name of the current image
run("Split Channels"); // generates three new images: C1-[title], C2-[title], C3-[title]
run("Subtract Background…", "rolling=50");
run("Gaussian Blur…", "sigma=2");
run("Convert to Mask");
imageCalculator("AND create", "C2-"+img_name,"C3-"+img_name); // generates a "Resut of "+first image used
selectWindow("Result of C2-"+img_name);
run("Convert to Mask");
run("Analyze Particles…", "size=200-Infinity circularity=0.50-1.00 show=Outlines summarize");
In this case, I simply replaced every instance of
P4 10106 2.jpg" for
"+img_name (notice placement of quote marks in each case). Sometimes it can get more complicated than this, but you get the idea.
As a final remark, be aware of the following: in some cases, image titles are passed to commands/plugins as part of a string of parameters (usually names). In that case, it is important to notice that if the title contains spaces (as is the case for the particular image you used), the name in the parameter string must be enclosed in square brackets. Sometimes, when recording, square brackets are ommited because the image used does not have spaces:
run("Calculator Plus", "i1=image.tif i2=[other image.tif] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");
Notice the diference in the name of the two images provided. It is good practice to always include the square brackets in these cases:
run("Calculator Plus", "i1=["+name1+"] i2=["+name2+"] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");
PS: I noticed you are using a .jpg image in your macro. Be careful: jpeg compression is lossy, and adds artifacts to your original image. If possible, always perform the image analysis using the original formats in which the images are captured.