Re: Choose a region automatically and quantify black pixels

Posted by Romain Guiet on
URL: http://imagej.273.s1.nabble.com/Choose-a-region-automatically-and-quantify-black-pixels-tp5012430p5012432.html

Hi Ram,

below you will find some code stat does the job (as I understood it).

You need to use a crop version of your image (containing only the 4 wings).
If you need the ruler for pixel size and color balance, you should do it in a previous step.

The macro code below  will create :
-a results table containing,
# the label as imageTitle:ROIname (you can split it it in Excel to simpliy analysis),
# the Area (of the considered Wing) ,
# the %Area (the percent of coverage by black spot),
# and the center of mass coordinates (XM and YM, that is used for ROIs naming)
-an RGB image, corresponding to each mask, Red is the Wing, Green is the black Spots, (yellow because green overlap with red)

Using Process>Batch>Macro, you can copy the code, specify the folder of the cropped images and a different folder to save the RGB masks created.

Feel free to contact me if needed,

Cheers,

Romain

//-------- start of code
/////////////////////////////////////////////////////////////// You should use a crop images that contains only the 4 wings
title = getTitle();
/////////////////////////////////////////////////////////////// clean the ROI manager between each image
roiManager("Reset");
/////////////////////////////////////////////////////////////// duplicate and convert to HSB, and retrieve some informations
run("Duplicate...", "title=HSB");
//rename("HSB");
run("HSB Stack");
getDimensions(width, height, channels, slices, frames);
xHalf = width /2;
yHalf = height /2;

/////////////////////////////////////////////////////////////// select the Hue channel, duplicate it and apply a threshold
selectImage("HSB");
Stack.setChannel(1);
run("Duplicate...", "title="+title+"_Wing");
setAutoThreshold("Default");////////////////////////////////// you can choose a different one (but use the same threshold between images you want to compare)
setOption("BlackBackground", true);
run("Convert to Mask");

/////////////////////////////////////////////////////////////// Detect the 4 wings
run("Set Measurements...", "area center area_fraction display redirect=None decimal=5");// define the measurements (need Area and center of Mass, for the renaming step)
run("Analyze Particles...", "size=100-Infinity add"); // you can define here the min and max size of your wings

/////////////////////////////////////////////////////////////// Rename the detected ROI accordingly to their position in the image
for (roiIndex = 0 ; roiIndex < roiManager("Count");roiIndex++){
        x = getResult("XM", roiIndex);
        y = getResult("YM", roiIndex);
        roiManager("Select",roiIndex);
        if(  (x < xHalf) && (y < yHalf) ) {
                roiManager("Rename","Upper-Left");
        } else if   ( (x > xHalf) && (y < yHalf) ) {
                roiManager("Rename","Upper-Right");
        } else if   ( (x < xHalf) && (y > yHalf) ) {
                roiManager("Rename","Lower-Left");
        } else if   ( (x > xHalf) && (y > yHalf) ) {
                roiManager("Rename","Lower-Right");
        }
}
roiManager("Deselect");


/////////////////////////////////////////////////////////////// select the Brightness channel ((looking for black pixel), duplicate it and apply a threshold
selectImage("HSB");
Stack.setChannel(3);
run("Duplicate...", "title="+title+"_Spot");
setAutoThreshold("RenyiEntropy");////////////////////////////// you can choose a different one (but use the same threshold between images you want to compare)
setOption("BlackBackground", true);
run("Convert to Mask");
for (roiIndex = 0 ; roiIndex < roiManager("Count");roiIndex++){
        roiManager("Select",roiIndex);
        run("Measure");
}
imageCalculator("AND create 8-bit", title+"_Wing", title+"_Spot");
rename("WingAndSpot");

run("Merge Channels...", "c1=["+title+"_Wing] c2=[WingAndSpot]");
selectWindow("RGB");
rename(title+"_mask");
close("\\Others");

//-------- end of code

---------------------------------------------------------------
Dr. Romain Guiet
Bioimaging and Optics Platform (PT-BIOP)
Ecole Polytechnique Fédérale de Lausanne (EPFL)
Faculty of Life Sciences
Station 19, AI 0140
CH-1015 Lausanne

Phone: [+4121 69] 39629
http://biop.epfl.ch/
---------------------------------------------------------------

________________________________________
De : ImageJ Interest Group [[hidden email]] de la part de ram prasad [[hidden email]]
Envoyé : vendredi 10 avril 2015 09:41
À : [hidden email]
Objet : Choose a region automatically and quantify black pixels

Hey All,

I'm trying to quantify black colour on the fore wings of butterflies. I would
like to do this by calculating the ratio of the area occupied by the black
pixels/total number of pixels in the wing. I would really appreciate it if
you could tell me how I should go about it. Is there a way to tell ImageJ
to find the edges of all objects in an image and pick an object with a
certain shape?

Also, since I have hundred of images I would like to employ batch mode
to analyze
them and I would like to know if that is possible. Basically, I want the
software to detect the fore wings by itself I'm not sure if this is
feasible.

I have attached an image for your reference. Any help will be really
appreciated.

Thanks,
Ram

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

testA.jpg (872K) Download Attachment