Choose a region automatically and quantify black pixels

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Choose a region automatically and quantify black pixels

ram prasad
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

mail.jpg (2M) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Choose a region automatically and quantify black pixels

Romain Guiet
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
Reply | Threaded
Open this post in threaded view
|

Re: Choose a region automatically and quantify black pixels

ram prasad
Thank you very much for your script, Romain. It works like a charm. And I
really appreciate the fact that you have included comments in the script.
It is very helpful ( allowed me to make tiny tweaks like changing the
particle size, etc.,).

After I processed my images, I observed some differences between
the tif files (heat maps?) and the original images which is arising due to
some fluctuations in the background yellow colour (Some appear to have a
darker shade of yellow and some don't). So, I was wondering if I were to
convert the images into greyscale and then do the same analysis, will it
fix the issue? If yes, then could you tell me how I should go about it?
Will just changing HSB to grayscale in the script work?

I'm attaching the raw image files here for your reference. Sorry, I
couldn't attach the tif files as were too big for the email.

Thanks again.

Cheers,
Ram

On Fri, Apr 10, 2015 at 1:35 PM, Romain Guiet <[hidden email]> wrote:

> 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
>
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

ed_12-77-1.jpg (868K) Download Attachment
ed_21-17-1.jpg (845K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Choose a region automatically and quantify black pixels

Romain Guiet
Hi Ram,

Using the two images you provide me with in your last e-mail, the macro detects pretty well the wings (by the way the macro is meant to use images containing 4 wings). If you have issue with some image, send them using a dropbox-like service.

What is not so well detected ? the wings or the spots ?
In this macro we use the :
- Hue component ( kind of colors) of the HSB to identify the wings
- Brightness component (since the spot are dark, somehow the inverse of brightness).

If you “just” convert your color image to a grey one, everything will be more or less grey, and it will be very difficult to distinguish wings from the background….


I see two options:
- You can give a try to the Lab stack conversion instead of the HSB stack.
You can use :
# the 3rd channel (b*) ,with Huang threshold and dark background  to detect the wing.
# the 1st channel (L*),with Huang threshold and  white background to detect the spots.

Otherwise you could use the color calibration (you have in the first image you send ) to adjust the color balance for each image using :
- # http://imagejdocu.tudor.lu/doku.php?id=plugin:color:chart_white_balance:start
or
- # download this bsh script made by colleague Olivier https://drive.switch.ch/public.php?service=files&t=f2f10dc350ee4602270d876709430bcb
You need to draw a ROI in a white part of your image and it will correct the image.

Cheers,

Romain


---------------------------------------------------------------
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é : mardi 14 avril 2015 12:15
À : [hidden email]
Objet : Re: Choose a region automatically and quantify black pixels

Thank you very much for your script, Romain. It works like a charm. And I
really appreciate the fact that you have included comments in the script.
It is very helpful ( allowed me to make tiny tweaks like changing the
particle size, etc.,).

After I processed my images, I observed some differences between
the tif files (heat maps?) and the original images which is arising due to
some fluctuations in the background yellow colour (Some appear to have a
darker shade of yellow and some don't). So, I was wondering if I were to
convert the images into greyscale and then do the same analysis, will it
fix the issue? If yes, then could you tell me how I should go about it?
Will just changing HSB to grayscale in the script work?

I'm attaching the raw image files here for your reference. Sorry, I
couldn't attach the tif files as were too big for the email.

Thanks again.

Cheers,
Ram

On Fri, Apr 10, 2015 at 1:35 PM, Romain Guiet <[hidden email]> wrote:

> 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
>

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

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