setBatchMode(true);
run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding fit shape feret's integrated median skewness kurtosis area_fraction stack redirect=None decimal=2");
if (isOpen("Results")) { 
	selectWindow("Results"); 
	run("Close"); 
} 

n = roiManager("count");
for (a=0;a<n;a++){
	roiManager("Select", a);
	run("Measure");
}
Dir1=getDirectory("image");;
File.makeDirectory(Dir1 +"\\rejected");
Wi=getWidth();
He=getHeight();

n=roiManager("count");
for (b=0;b<n;b++){
	Area = getResult("Area",n-b-1);
	Aspect_Ratio = getResult("AR",n-b-1);
	Roundness = getResult("Round",n-b-1);
	Mean = getResult("Mean",n-b-1);
	Standard_Deviation = getResult("StdDev",n-b-1);
	Modal = getResult("Mode",n-b-1);
	Median = getResult("Median",n-b-1);
	Max = getResult("Max",n-b-1);
	Min = getResult("Min",n-b-1);
	Integrated_Density = getResult("IntDen",n-b-1);
	Solidity = getResult("Solidity",n-b-1);
	Perimeter = getResult("Perim.",n-b-1);
	Area_fraction = getResult("%Area",n-b-1);
	Major_Axis = getResult("Major",n-b-1);
	Minor_Axis = getResult("Minor",n-b-1);
	Axis_Angle = getResult("Angle",n-b-1);
	Major_Feret = getResult("Feret",n-b-1);
	Minor_Feret = getResult("MinFeret",n-b-1);
	Feret_Angle = getResult("FeretAngle",n-b-1);
	Circularity =getResult("Circ.",n-b-1);
	Morphological_Number= ((2*sqrt(Area*Solidity)*Roundness)/(sqrt(PI)*Major_Feret));
	if(
		(((Morphological_Number<0||Morphological_Number>0.85)
		||
		(Standard_Deviation<6||Standard_Deviation>55))
		||
		(Mean<0||Mean>200))
		||
		((Mean<0||Mean>75)&&(Standard_Deviation<15||Standard_Deviation>255))
		||
		((Area<0||Area>10000)&&(Aspect_Ratio<0||Aspect_Ratio>5))
		||
		((Circularity<0||Circularity>0.6)&&(Area<10000||Area>1000000))
		||
		((Area<50000||Area>1000000)&&(Standard_Deviation<0||Standard_Deviation>50))
		||
		((Area<0||Area>1000)&&(Standard_Deviation<10||Standard_Deviation>255))
		||
		((Area<1000||Area>1000000)&&(Roundness<0||Roundness>0.7))
		||
		((Area<0||Area>10000)&&(Standard_Deviation<12||Standard_Deviation>255))
		||
		((Mean<0||Mean>105)&&(Roundness<0.3||Roundness>1))
		||
		((Mean<0||Mean>100)&&(Circularity<0.09||Circularity>1))
		||
		((Roundness<0||Roundness>0.7)&&(Standard_Deviation<25||Standard_Deviation>255))
		||
		(((Area<10000||Area>1000000)&&(Roundness<0||Roundness>0.8))&&(Mean<0||Mean>105))
		||
		(((Area<2500||Area>1000000)&&(Circularity*Roundness<0||Circularity*Roundness>0.1))&&(Mean<0||Mean>100)))
	{
		roiManager("select", n-b-1);
		Enlarging=floor(Major_Feret/10);
		run("Enlarge...", "enlarge="+Enlarging);
		run("Duplicate...", "title=Rej_"+(n-b-1)+".jpg");
		saveAs("Jpeg", ""+ Dir1+"\\rejected\\Rej_"+(n-b-1)+".jpg");
		roiManager("Delete");
		IJ.deleteRows(n-b-1, n-b-1);
		selectWindow("Original");
	}
}