Recognize image and crop in other images



Hi friends,
I have a problem, I need recognize images in this image
I need a macro for this is the image

For example in this image have got 12 image
thanks you



Welcome to the Forum!

Would you be able to be a bit more specific with what you wish detect? I’m assuming you want to delineate those pink objects. But what is it you need to do with them then after?

Please just describe with a bit more detail what you need for your analysis workflow and then we can better help you.



Hi @etarena
I want recognize this image. this image represent to part of human embryo, well i have150 images same this where each image have some “pink object”.
The final goal is to get an new image for each pink object then I must select a file (image) load in imageJ and to get other image, for example in this case 12 image new.
I try this code:
*This code is of this forum
extra_pixels = 9;
initial_blur = 2;
median = 10;
threshold = “Default”;

// Segment image

run(“Duplicate…”, “title=detector”);
run(“Median…”, “radius=2”); // remove line artifacts…
run(“Gaussian Blur…”, “sigma=”+initial_blur);
run(“Find Edges”);
run(“Median…”, “radius=”+median);
setAutoThreshold(threshold+" dark");

// Extract detected particles
run(“Analyze Particles…”, “add”);

// Convert from shape to bounding box and enlarge
c = roiManager(“count”);

for(i=0; i<c;i++) {
roiManager(“Select”, i);
run(“To Bounding Box”);
run(“Enlarge…”, “enlarge=”+extra_pixels+" pixel");
Roi.setName(“Region #”+IJ.pad(i+1,2));

// delete original ROIs
roiManager(“Select”, Array.getSequence©);
run(“Select None”);

roiManager(“Show All with labels”);

I hope your help
thank you


As I’m here anyway, I’ll take a shot. I think the sample code you posted is working along the right lines:

  • blur the image to remove smaller features
  • intensity threshold
  • analyse particles to extract connected components
  • enlarge the objects to make new (square) selections

It’s usually at this point that I would recommend acquiring better images (more even illumination, no edge effects &c) but people don’t usually want to hear that so here goes with an adapted piece of code (which does basically the same as above):

//-- Get image name
run("Duplicate...", "title=mask");

//-- Just work on the green channel
run("Split Channels");
close("mask (red)");
close("mask (blue)");
selectWindow("mask (green)");

//-- pre-filter. If this doesn't work for other images, consider adding a gaussian or similar
run("Median...", "radius=12"); //-- equivalent to the median parameter in your code

//-- Find objects in a reasonably reliable way
run("Convert to Mask");

//-- Erode to cut edge connections
run("Options...", "iterations=5 count=1 black do=Erode");
run("Analyze Particles...", "size=1000-Infinity add");
setOption("Show All", false);

//-- Go back to original image
close("mask (green)");

//-- Turn all the ROIs into boxes then duplicate to new windows
roiNum = roiManager("count");
for (i = 0; i < roiNum; i++) {
	roiManager("Select", i);
	run("To Bounding Box");
	run("Enlarge...", "enlarge=30"); //-- equivalent to the extra_pixels parameter in your code
	run("Duplicate...", "title="+title+"_"+IJ.pad(i,3));
    //-- If you want to save the duplicates do it here with something like this:
    //saveAs("Tiff", OUTPUT_PATH+File.separator+title+"_"+IJ.pad(i,3)+".tif");

Main differences over the one you posted:

  • Segmentation occurs on just the green channel (this has the best contrast)
  • I’ve not been too careful about filtering out small objects: the Analyse Particles now has a long-pass size filter.
  • The images are duplicated out of the original. It’s trivial to automatically save these somewhere (there’s commented code which needs a path).
  • It works (…on this image - see caveat below)

Having only one image, this is likely not a robust solution for anything other than this single image. This is where you will have to play around with various prefiltering steps to make it work across your entire dataset or acquire ‘nicer’ images. If you have problems, you may want to consider a colour deconvolution step to better separate out the tissue bits.

Finally, depending upon what you want to do after separation and how many images you have, you may want to look into something like Ilastik which would make short work of segmenting the tissue sections and can run in batch mode. You would have a more robust segmentation step then can use imageJ to do the rest (if needed).

Semi-automatic way to select and crop parts of images

Thank you, but i have a problem when I try with format .tiff not work
thank you of new


Ok, but your example image was a jpg so that’s kind of predictable.

Please try to give more details when you ask for help. What is the problem. Do you get error messages? What images are you left with?

Most importantly, upload an proper example file!


Sorry but I can´t upload the example correct because the size is very big, the problem is that when analize particles the image close and the error message is "there isn´t image open "


I’m still trying, but I can not find the problem, I think it could be due to big size of the images
Could be the problem @dnmason ?


Yup, as I mentioned in the OP, this works for this image. If you’re changing the size or colour space, you will need to play around with some of the parameters.

For example, the following lines are dependent upon the image size:

So a good place to start would be to scale these up relative to the new image size.


Dear @pedro_lozano_santerv,
I saw your request for help in another post where I had contributed.
It is clear that @dnmason has spent considerable time providing code and advice.

As he mentions, the code works on the image you have provided and we cannot help you unless you provide the actual raw data you are using.

So if you would like further help, please provide a file and a protocol that follows exactly the way you are loading it into ImageJ, as this is not as trivial as it seems. Just use a filesharing service if you cannot upload it

My first thought is that you have a composite image rather than an RGB image.

Also let’s please keep the discussion on this thread and not revive old ones (Even though this gives me more hits :slight_smile:)


@oburri and @dnmason
First thank you, but I think the problem is in .tiff format


this is the image @dnmason , I didn´t get solved the problem with this image, I tried with some changes, but it not work…
This photo is the original with scale 0,3

Thank you for you help


I think uploading the file into the forum converts it into JPG. Can you upload the file in a zip archive or put in an online file hosting service.


Hi @dnmason ,
I found the problem, I was working with inches then not recognize elements, but I changed to pixels, and it work now
Thank you for you help
this was the problem
run(“Analyze Particles…”, “size=1000-Infinity pixels add”