Image analysis at work, using threshold in PS...

krazy_horse

Suspended / Banned
Messages
824
Edit My Images
Yes
Hi folks,

At work we are currently doing some image analysis on some composite samples, and we would like to calculate porosity of the samples.

We now have a black and white image produced using the threshold function, but want to know if it’s possible to calculate the percentage of black pixels to white pixels and potentially the area in Photoshop?

We have PS elements 6 here at work, but I could try on CS2 at home.

Thanks,

Jonathan
 
Hmmm - not sure. It kind of sounds like you need to use a GIS (Geographical Information System). If you have access to something like ArcGIS then that would work. I use ArcInfo and ArcGIS and its the sort of thing that is reasonably easy to do in these software suites. PM me if you need more info on that!
 
Had another thought! If this image is simply black and white there there are only black and whites and no greys in between then you could open the histogram window in photoshop and in there it will show you the number of pixels (Count) that are either black or white (move your mouse over the histogram peaks). The total number of pixels will also be shown and therefore you can calculate percentages. If you have greys in between then I guess thats not really going to work, but it is is pure black vs. white then this would be a quick fix.
 
Use imagemagick and the command:

identify -verbose "filename.ext"

and it will tell you exactly how many pixels of each colour there are. Here's a sampel output from a G4 B&W Tiff file:

C:\>identify -verbose "C:\0006_2.tif"
Image: C:\0006_2.tif
Format: TIFF (Tagged Image File Format)
Class: DirectClass
Geometry: 2578x3510+0+0
Type: Bilevel
Endianess: MSB
Colorspace: Gray
Channel depth:
Gray: 1-bit
Channel statistics:
Gray:
Min: 0 (0)
Max: 1 (1)
Mean: 0.801558 (0.801558)
Standard deviation: 0.398827 (0.398827)
Colors: 2
Histogram:
1795659: #000000 black
7253121: #FFFFFF white

Rendering intent: Undefined
Resolution: 300x300
Units: PixelsPerInch
Filesize: 70.0215kb
Interlace: None
Background color: white
Border color: rgb(223,223,223)
Matte color: grey74
Transparent color: black
Page geometry: 2578x3510+0+0
Dispose: Undefined
Iterations: 0
Compression: Group4
Orientation: TopLeft
Comment: Picture Elements, Inc. ISE/SSE
Signature: c08cb2e00ce2d8c1c9aeea8617d5f94c45673d86a57e01bcce831933b9dcb1ab
Tiff:make: Picture Elements, Inc.
Tiff:model: Scanning Subsystem Engine/RGB[11]
Tiff:software: smcrgb.scof
Tainted: False
User time: 0.310u
Elapsed time: 0:01
Pixels per second: 26.8834mb
Version: ImageMagick 6.3.4 06/01/07 Q8 http://www.imagemagick.org
 
OK - I found a plugin called histotext for photoshop (works in elements or CS) that will output the histogram of an image to a tab delimited text file. You can then open it in Excel or wordpad and look at it.
Go to:
http://www.telegraphics.com.au/sw/dl.php3?file=Histotext-1.0f2-win.zip to download for free.
Then put the plugin file in your plugins folder and open photoshop. Open your image and then the plugin will appear at the bottom of the filters list under the 'Telegraphics' heading.

The output is a bit complicated because it outputs a list of pixel values per channel (usually 3 - Red Green and Blue) and then per level in that channel (i.e. there are 256 levels in each channel - black being at one end, white being at the other), but you should be able to figure out what its saying! I tried it with a black and white image that I posterised to show only 2 channels. Using histotext I get a file that shows the number of pixels at each level - most of which are 0 and then with large numbers at the black and the white level.

Job done and it takes 2 minutes to install and then run :)
 
This should be near..

Code:
var area = activeDocument.histogram; 
var PercentBlack = (100/256) * parseInt(mean(area));
alert("White = "+ Math.round(PercentBlack)+"\%");

function mean(hist) { 
   var acc = 0; 
   var cnt = 0; 
   for (var i = 0; i < hist.length; i++) { 
     acc += i*hist[i]; 
     cnt += hist[i]; 
   } 
   return acc/cnt; 
}
 
Thanks guys,

Will try the plugin, but i think the histogram idea will be enough for what we want.

Many thanks again for your time and help.

Jonathan
 
Another version that you could try...
Code:
var startRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var black = activeDocument.histogram[0];
var white = activeDocument.histogram[255];
var res = activeDocument.resolution;
var total = activeDocument.width.value * activeDocument.height.value;
var onePer = total/100;
alert("Black pixels = "+black + " \rWhite Pixel = " +white +"\rTotal Pixels = " + total +"\rPercentage Black = " +
(black/onePer).toFixed (2)+"\rPercentage White = "+(white/onePer).toFixed (2)+"\rResolution = " +res+"PPI");
app.preferences.rulerUnits = startRulerUnits;
 
out of curiosity why would you need to know such information?
 
At work we are currently doing some image analysis on some composite samples, and we would like to calculate porosity of the samples.

It's not for normal "photos" its to actually determine the % of one pixel to another, in an image from our microscope at work.

Jonathan
 
Back
Top