Perform histogram analysis on certain image area only

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
allomorphy
Posts: 5
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Perform histogram analysis on certain image area only

Post by allomorphy » 2017-10-29T14:46:13-07:00

Hi,

I have images with certain lines or bands on a ~white background. First I want to analyse the image to define the area of these lines or bands and then I would like to perform an analysis of a certain color range on that defined area only or those defined areas only.

What might be a good strategy for this?

Thanks for any help.

A

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-29T14:55:51-07:00

Please do not add topics to the main Digital Image Processing post at the top of this forum. I have separate your post. Also please do not post the same question to multiple forums. I have deleted your post in Fred's Scripts forum, since your question has nothing to do with those scripts.

What is your ImageMagick version and platform?

Please post and example image so we can understand better your question. You can upload to some free hosting service and put the URL here.


See the top-most post in the User's forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://http://www.imagemagick.org/scrip ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

snibgo
Posts: 9388
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Perform histogram analysis on certain image area only

Post by snibgo » 2017-10-29T16:33:28-07:00

As Fred says, we need to understand the problem before we can offer solutions.

For example, if the required area is a rectangle, then crop to it and use the in-built histograms.

My own process modules can regard alpha, so you can make pixels transparent to ignore them for counting, or make some 25% to count as 1/4 pixels, etc, like this:

Image

Code: Select all

%IMDEV%convert ^
  toes_holed.png ^
  -process 'mkhisto norm regardAlpha' ^
  h.png

call %PICTBAT%graphLineCol h.png . . 0 toes_holed_glc.png
Image
snibgo's IM pages: im.snibgo.com

allomorphy
Posts: 5
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Perform histogram analysis on certain image area only

Post by allomorphy » 2017-10-30T02:11:09-07:00

(My apologies fmw42 for my ineptitude in posting to this forum. The post to Fred's Scripts was unintended and I could not quickly find the delete button. I hope that I have done better with this posting).

My problem is to achieve with an automated script, the location and the bounding of specific areas of interest in an image like www.fruitcom.com/imagemagick_stripes.tiff Here you see two darker bands and these are what I would like to "extract" for histogram and HSV and other statistical analysis to quantify a specific colour intensity.

The locations of these lines may vary in the image on the vertical axis, therefore the non-interactive script must not be dependent on a predetermined location. (It must define that itself and pass this definition to other processes).

How could this best be achieved with ImageMagick (to eventually port to PerlMagick)?

Version: ImageMagick 6.8.9-9 Q16 i686 2017-07-31
on Ubuntu 16.04.3 LTS
(libimage-magick-perl Version: 8:6.8.9.9-7ubuntu5)

Thanks for your support,
A

PS. Thanks for the resource links, many of the items in the left column of the root of this page have a server error, viz.
http://www.imagemagick.org/Usage/option ... gi?segment
And 404 here: http://http//www.imagemagick.org/script ... essing.php

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-30T10:40:44-07:00

Sorry for the typo. The correct link is http://www.imagemagick.org/script/comma ... essing.php

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-30T12:03:29-07:00

Here is one way.

Smooth out the image by averaging to one column, then expand horizontally.
Get the Chroma (saturation) channel, stretch to full dynamic range and threshold to make a black and white mask.
Use connected components analysis to extract the regions from the bounding boxes of the white areas in the mask from the original image.


Code: Select all

dim=`convert imagemagick_stripes.tiff -format "%wx%h" info:`
convert imagemagick_stripes.tiff -scale 1x! -scale $dim! tmp1.png 
convert tmp1.png -colorspace HCL -channel g -separate +channel tmp2.png
convert tmp2.png -auto-level tmp3.png
convert tmp3.png -threshold 50% tmp4.png
k=0
arr=()
OLD_IFS=$IFS
IFS=$'\n'
arr=(`convert tmp4.png \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null: | sed 's/^[ ]*//' | tail -n +2`)
IFS=$OLD_IFS
num=${#arr[*]}
echo "num=$num"
for ((i=0;i<num;i++)); do
bbox=`echo "${arr[$i]}" | cut -d\  -f2`
color=`echo "${arr[$i]}" | cut -d\  -f5`
if [ "$color" = "gray(255)" ]; then
convert imagemagick_stripes.tiff -crop $bbox +repage region_$k.png
k=$((k+1))
fi
done
tmp1 -- averaged image:
Image

tmp2 -- chroma image:
Image

tmp3 -- stretched image:
Image

tmp4 -- thresholded image:
Image

region 0:
Image

region 1:
Image


See http://magick.imagemagick.org/script/co ... onents.php

allomorphy
Posts: 5
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Perform histogram analysis on certain image area only

Post by allomorphy » 2017-10-30T15:44:19-07:00

Dear Fred

Pure magick! And just what I ordered.
FWIW, on my platform this statement was never true:
`if [ "$color" = "gray(255)" ]; then'
because for example:
${arr[0]} = 0: 123x88+0+0 +61.0,+43.5 10824

But no matter, I removed it because I want to generate images of the different pieces.

Thank you for your assistance.

And thanks too, snigbo for your mkhisto.

Kind regards,
A

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-30T15:55:43-07:00

You are probably getting black and white rather than gray(0) and gray(255) or perhaps gray(0%) and gray(100%) or srgb(0,0,0) and srgb(255,255,255). You can check by doing

Code: Select all

convert tmp4.png \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  4: 123x176+0+208 61.0,295.5 21648 gray(0)
  0: 123x88+0+0 61.0,43.5 10824 gray(0)
  2: 123x79+0+100 61.0,139.0 9717 gray(0)
  3: 123x29+0+179 61.0,193.0 3567 gray(255)
  1: 123x12+0+88 61.0,93.5 1476 gray(255)
You may have to make tmp4.png bilevel.

Code: Select all

convert tmp4.png -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  4: 123x176+0+208 61.0,295.5 21648 gray(0)
  0: 123x88+0+0 61.0,43.5 10824 gray(0)
  2: 123x79+0+100 61.0,139.0 9717 gray(0)
  3: 123x29+0+179 61.0,193.0 3567 gray(255)
  1: 123x12+0+88 61.0,93.5 1476 gray(255)

If you are on IM 7, then change convert to magick.

allomorphy
Posts: 5
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Perform histogram analysis on certain image area only

Post by allomorphy » 2017-10-30T16:33:19-07:00

Thank Fred.
It does not give me the colors in those formats with either of this options.
For the practical application at hand this is not a problem, as I will know what is a stripe and what is a background. And actually it is an advantage to have both the types of segments for analysis. In fact I want all the segments form the original, I just need to have the boundary information.

In fact, it occurs to me that I do not need to generate a physical image of each segment to be analysed, rather as long as I know the bounding box of that segment (from your mask), then I can use this information to perform all the analysis required on that segment of the original image. This is more direct and saves writing temporary files. I think.

FWIW, here are my outputs of your suggestions:
~ % convert --version
Version: ImageMagick 6.8.9-9 Q16 i686 2017-07-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

~ % convert tmp4.png -type bilevel \
~ > -define connected-components:verbose=true \
~ > -define connected-components:mean-color=true \
~ > -connected-components 4 \
~ > null:
Objects (id: bounding-box centroid area):
1: 123x362+0+22 +61.0,+202.5 44526
0: 123x22+0+0 +61.0,+10.5 2706
~ % convert tmp4.png \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null:
Objects (id: bounding-box centroid area):
1: 123x362+0+22 +61.0,+202.5 44526
0: 123x22+0+0 +61.0,+10.5 2706

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-30T16:42:32-07:00

I suspect your version of IM was just at the time that connected components was being developed and had not been fully debugged. I suggest that you upgrade if you can. Or perhaps if Linux, it has not been properly patched. I see you have 6.8.9.9, but the patch date says 2017-07-31. That means it has been patched (though not to the current level/date). But I suspect it has botched patches for connected components. 6.8.9.9 per se is about 120 versions old.

The first official debugged release was 6.8.9.10.

allomorphy
Posts: 5
Joined: 2017-10-29T14:15:04-07:00
Authentication code: 1151

Re: Perform histogram analysis on certain image area only - SOLVED

Post by allomorphy » 2017-10-31T01:59:29-07:00

Thank you Fred, when I rolled my own ImageMagick 7.0.7-8 Q16 i686 2017-10-31 all now works as advertised.

A

TedBaker
Posts: 36
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Perform histogram analysis on certain image area only

Post by TedBaker » 2017-10-31T05:22:18-07:00

fmw42 wrote:
2017-10-30T16:42:32-07:00
I see you have 6.8.9.9, but the patch date says 2017-07-31. That means it has been patched (though not to the current level/date). But I suspect it has botched patches for connected components. 6.8.9.9 per se is about 120 versions old.

The first official debugged release was 6.8.9.10.
I will take a wild guess and say the user is using ubuntu 16.04 (long term supported release), the version of IM is indeed 6.8.9.9 and compiled on 2017-07-31, but I think the only patches are security changes to files such as policy.xml.

The 6.8.9.9 I have ignores

Code: Select all

-define connected-components:mean-color=true
and the output looks this (note different image):

Code: Select all

Objects (id: bounding-box centroid area):
  10: 142x94+726+0 +796.5,+46.5 13348
  2: 141x94+136+0 +206.0,+46.5 13254
instead of

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  10: 142x94+726+0 796.5,46.5 13348 gray(0)
  2: 141x94+136+0 206.0,46.5 13254 gray(0)

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Perform histogram analysis on certain image area only

Post by fmw42 » 2017-10-31T09:30:29-07:00

-define connected-components:mean-color=true was only added later at 6.9.2.8

See
http://magick.imagemagick.org/script/co ... onents.php
http://www.imagemagick.org/script/comma ... php#define (sections on connected components)

Post Reply