Calculate coverage of TIFFG4 in percent

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pictogram
Posts: 17
Joined: 2010-03-23T14:19:21-07:00
Authentication code: 8675308

Calculate coverage of TIFFG4 in percent

Post by pictogram »

Hello,

i have several TIFFG4 compressed bilevel images and want to calculate the converage of black pixels from a given rectangle of that page. So is it possible to count the black/white pixels of an arbitrary rectangle inside an image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Calculate coverage of TIFFG4 in percent

Post by fmw42 »

In unix:

numpixels=`convert image[WxH+X+Y] -format "%[fx:w*h]" info:`

totalwhitepixels=`convert xc: -format "%[fx:$numpixels*mean]" info:`
(note mean will be between 0 and 1 in fx expressions)

totalblackpixels=`cconvert xc: -format "%[fx:$numpixels - $totalwhitepixels]" info:`

see subsections at
http://www.imagemagick.org/Usage/files/#read_mods

see string formats and fx calculations
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/script/fx.php
http://www.imagemagick.org/Usage/transform/#fx
http://www.imagemagick.org/Usage/transform/#fx_escapes

if on windows, see differences in syntax at http://www.imagemagick.org/Usage/windows/
pictogram
Posts: 17
Joined: 2010-03-23T14:19:21-07:00
Authentication code: 8675308

Re: Calculate coverage of TIFFG4 in percent

Post by pictogram »

Hello and thank you very much! This code would do, but what i forgott to mention is that those bilevel TIFFs i'm talking about are very large (16200*23700 pixels).
It would take several Minutes so process one image...
Any hints about that? Can i convert the TG4s into something else before (with an other tool) so data is reduced? I don't think that i need to be more precise than 1% steps.
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Calculate coverage of TIFFG4 in percent

Post by whugemann »

The basic trick that Fred suggests is to calculate the mean value of the image. This is supplied in the "mean" line when Identifying with the -verbose option. So it's basically a question of Identifying and then filtering. In a Windows batchfile:

Identify -verbose %1 | FIND "mean"

You can than store the value in an (environment) variable.
Wolfgang Hugemann
pictogram
Posts: 17
Joined: 2010-03-23T14:19:21-07:00
Authentication code: 8675308

Re: Calculate coverage of TIFFG4 in percent

Post by pictogram »

I understand and find the idea stunning simple. My problem is, that processing the compressed bilevel files takes sooo much time. But even processing uncompressed bilevel TIFFs takes too long. This is a IM problem, because on bit gets converted to 3 or more bytes per pixel by reading in the image.
This takes huge amounts of RAM and time.
Because i don't think this could be changed i need to preprocess the images somehow to make it more handy for IM. One thing is to reduze the size by cummulating pixelareas to single pixels (in respect to the expected precision i need) and output at least a graylevel image.
Post Reply