Filtering out particular shade of greeen

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

Filtering out particular shade of greeen

Andrew Sanchez
I have a macro that uses color thresholding to allow me to get a pixel count of the number of green pixels.  It works great, but I've run into an issue.  Some of the images I am analyzing contain compost bags that are green.  Luckily, the bags are a very particular shade of green.  Is there a way to adjust my macro to exclude this shade of green?

Here is the macro I am currently working with::

dir1 = getDirectory("Choose Source Directory ");
list = getFileList(dir1);
//setBatchMode(true);
for (k = 0; k<list.length; k++) {
        showProgress(k+1, list.length);
        open(dir1+list[k]);

// crops photo, clears outside of circle, and measures total area
setLocation(1, 1, 1366, 768);
run("Set... ", "zoom=25");
run("Set Measurements...", "area display redirect=None decimal=2");
setTool("oval");

// macro will not continue until the selection is made.  you can exit by closing the image window.
while(selectionType()<0){
        waitForUser("Select area inside the ring","Hold shift while drawing circle, then click 'OK'");
}

run("Crop");
setBackgroundColor(255, 255, 255);
run("Clear Outside");
run("Measure");

// generates black and white image.  black pixels represent green pixels
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=36;
max[0]=128;
filter[0]="pass";
min[1]=34;
max[1]=210;
filter[1]="pass";
min[2]=30;
max[2]=225;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Color Thresholding-------------

// counts number of green pixels under total area column in the summary table.  excludes pixel groups of less than 10 pixels
run("Analyze Particles...", "size=10-Infinity summarize");

        selectWindow(list[k]);
        close();

// saves summary and results tables
String.copyResults;
string = String.paste;
File.append(string, "/home/truthling/Documents/ECOSS/ImageJ_Results/"+"area.csv");

IJ.renameResults("Summary","Results");
String.copyResults;
string = String.paste;
File.append(string, "/home/truthling/Documents/ECOSS/ImageJ_Results/"+"green.csv");

}
Reply | Threaded
Open this post in threaded view
|

Re: Filtering out particular shade of greeen

Anderson, Charles (DNR)
Andrew,

In essence, you need to do the color thresholding twice, once to identify "green" as you have, and again to identify "NotBag", then combine results.

Duplicate the image just before beginning the color thresholding.  Select the original to identify "green" as you already have. Select the copy to identify "NotBag" (avoid renaming this one 'a' at the end of the thresholding). Then use the image calculator as
imageCalculator("AND create", "green","NotBag");  or
imageCalculator("AND create", "a","NotBag");

Charles

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Andrew Sanchez
Sent: Thursday, August 20, 2015 3:09 PM
To: [hidden email]
Subject: Filtering out particular shade of greeen

I have a macro that uses color thresholding to allow me to get a pixel count of the number of green pixels.  It works great, but I've run into an issue.
Some of the images I am analyzing contain compost bags that are green.
Luckily, the bags are a very particular shade of green.  Is there a way to adjust my macro to exclude this shade of green?

Here is the macro I am currently working with::

dir1 = getDirectory("Choose Source Directory "); list = getFileList(dir1); //setBatchMode(true); for (k = 0; k<list.length; k++) {
        showProgress(k+1, list.length);
        open(dir1+list[k]);

// crops photo, clears outside of circle, and measures total area setLocation(1, 1, 1366, 768); run("Set... ", "zoom=25"); run("Set Measurements...", "area display redirect=None decimal=2"); setTool("oval");

// macro will not continue until the selection is made.  you can exit by closing the image window.
while(selectionType()<0){
        waitForUser("Select area inside the ring","Hold shift while drawing circle, then click 'OK'"); }

run("Crop");
setBackgroundColor(255, 255, 255);
run("Clear Outside");
run("Measure");

// generates black and white image.  black pixels represent green pixels min=newArray(3); max=newArray(3); filter=newArray(3); a=getTitle(); run("HSB Stack"); run("Convert Stack to Images"); selectWindow("Hue"); rename("0"); selectWindow("Saturation"); rename("1"); selectWindow("Brightness"); rename("2"); min[0]=36; max[0]=128; filter[0]="pass"; min[1]=34; max[1]=210; filter[1]="pass"; min[2]=30; max[2]=225; filter[2]="pass"; for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert"); } imageCalculator("AND create", "0","1"); imageCalculator("AND create", "Result of 0","2"); for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Color Thresholding-------------

// counts number of green pixels under total area column in the summary table.  excludes pixel groups of less than 10 pixels run("Analyze Particles...", "size=10-Infinity summarize");

        selectWindow(list[k]);
        close();

// saves summary and results tables
String.copyResults;
string = String.paste;
File.append(string,
"/home/truthling/Documents/ECOSS/ImageJ_Results/"+"area.csv");

IJ.renameResults("Summary","Results");
String.copyResults;
string = String.paste;
File.append(string,
"/home/truthling/Documents/ECOSS/ImageJ_Results/"+"green.csv");

}




--
View this message in context: http://imagej.1557.x6.nabble.com/Filtering-out-particular-shade-of-greeen-tp5014085.html
Sent from the ImageJ mailing list archive at Nabble.com.

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

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