It is currently Mon Apr 29, 2024 3:00 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: get info on saturation/contrast of an image
PostPosted: Wed Dec 03, 2014 11:35 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
GIMP Version: 2.8.14
Operating System: Windows
GIMP Experience: Beginner Level

List any relevant plug-ins or scripts:
none

List any ERROR messages you received:
none



Hi all gimp experts, I have a little (maybe silly, I don't know) question:
- in a filter it is possible to modify the saturation on an image (+ or -), but...
- how is it possible to know which is the current level of saturation of the image?
So far I used the saturation modification only online, but assume I want to write a script in which -depending upon the current level of saturation- I would like to increase it -if too low- or decrease it -if too high- without asking the user thru a parameter.
How can I do it?
Is maybe gimp-histogram usable (as it is to know the mean luminosity)? How?

While writing this note emerged in my mind also a similar curiosity about the contrast of an image: again, how do I know the current contrast level of an image?
So to decide whether to increase it or decreasing it.
Is it derivable by the standard deviation provided by gimp-histogram?

Thanks a lot!

_________________
"Where am I ?"


Last edited by dinasset on Wed Dec 03, 2014 2:49 pm, edited 1 time in total.

Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: get info on saturation of an image
PostPosted: Wed Dec 03, 2014 12:25 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
The following procedure will extract the saturation channel and return the mean value.
(define (get-saturation layer)
  (let* ((image (car (gimp-item-get-image layer)))
         (sat-image (car (plug-in-decompose RUN-NONINTERACTIVE
                                            image
                                            layer
                                            "Saturation"
                                            FALSE )))
         (sat-layer (car (gimp-image-get-active-layer sat-image)))
         (saturation (car (gimp-histogram sat-layer
                                          HISTOGRAM-VALUE
                                          0
                                          255 ))) )
    (gimp-image-delete sat-image)
    saturation ))


The following procedure will return the difference between the darkest pixel and the brightest (the traditional definition of contrast, though other methodologies may be more suitable in some situations).

Note: rather than do a brute force to find the extremes, which could take as many as 256 calls to gimp-histogram, I use a binary search (worst case, 16 calls to gimp-histogram).
(define (get-contrast layer)   
  (define (darkest drawable channel)
    (define (avg a b)
      (inexact->exact (truncate (/ (+ a b) 2))) )
    (let ((lower (let loop ((lower 1))
                   (if (zero? (car (cddddr (gimp-histogram drawable channel 0 (pred lower)))))
                     (loop (* 2 lower))
                     (inexact->exact (truncate (/ lower 2))) ))))
      ; binary search for largest zero count
      (let loop ((lower (max 1 lower))
                 (upper (* 2 lower))
                 (value (avg lower (* 2 lower)))
                 (last 0) )
        (if (= last value)
          value
          (if (zero? (car (cddddr (gimp-histogram drawable channel 0 (pred value)))))
            (loop value
                  upper
                  (avg value upper)
                  value )
            (loop lower
                  value
                  (avg lower value)
                  value ))))))
  (define (brightest drawable channel)
    (define (avg a b)
      (inexact->exact (truncate (/ (+ a b) 2))) )
    (let ((upper (let loop ((upper 256))
                   (if (zero? (car (cddddr (gimp-histogram drawable channel (pred upper) 255))))
                     (loop (/ upper 2))
                     (* upper 2)  ))))
      ; binary search for largest non-zero count
      (if (> upper 256)
        255
        (let loop ((lower (max 1 (/ upper 2)))
                   (upper upper)
                   (value (avg (/ upper 2) upper))
                   (last 256) )
          (if (= last value)
            (pred value)
            (if (zero? (car (cddddr (gimp-histogram drawable channel (pred value) 255))))
              (loop lower
                    value
                    (avg lower value)
                    value )
              (loop value
                    upper
                    (avg value upper)
                    value )))))))
    (- (brightest layer HISTOGRAM-VALUE) (darkest layer HISTOGRAM-VALUE)) )

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: get info on saturation of an image
PostPosted: Wed Dec 03, 2014 1:37 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Wow, Saul! very rapid and complete solution! Thanks a lot.
IMO those two pieces of code -if not yet- could become released "sg-xxxx" filters
available to the Gimp population

_________________
"Where am I ?"


Top
 Post subject: Re: get info on saturation of an image
PostPosted: Wed Dec 03, 2014 2:23 pm  (#4) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Saul, just an additional info (confirmation), if you don't mind:
- the value returned from the "saturation" procedure is in the range 0-255 like the values when using histogram, isn't it ?
I ask that because I did a test on an image and got a value of about 90.
Then I forced (interactively) an increase in the saturation (+100), then -tried again the function- I got a value of around 130
(by repeating the two steps above, I got a value of about 155; hence my impression the saturation value is between 0 and 255; am I right?)

_________________
"Where am I ?"


Top
 Post subject: Re: get info on saturation of an image
PostPosted: Wed Dec 03, 2014 2:47 pm  (#5) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Yes. You are correct.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: get info on saturation/contrast of an image
PostPosted: Wed Dec 03, 2014 3:00 pm  (#6) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks Saul!
another note: I consider my problem on saturation perfectly solved, while the problem on contrast seems to remain open because of this :
- we can have an image with a diffused low contrast which would require an increase but -unfortunately- having just one pixel almost black and one pixel almost white; in this case the function returns to the program a value of around 250, letting the program think the image is well contrasted.
Probably it would be necessary to work on a subset of values, let's say: getting the average value of the 10% of the brightest pixels to be compared to the average of the 10% of the darkest pixels.
What is your opinion?

_________________
"Where am I ?"


Last edited by dinasset on Wed Dec 03, 2014 3:20 pm, edited 1 time in total.

Top
 Post subject: Re: get info on saturation/contrast of an image
PostPosted: Wed Dec 03, 2014 3:19 pm  (#7) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Saul, which is your opinion in considering the "std deviation" value from gimp-histogram as a good approximation of the "contrast" concept?

_________________
"Where am I ?"


Top
 Post subject: Re: get info on saturation/contrast of an image
PostPosted: Fri Dec 05, 2014 12:38 am  (#8) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
dinasset wrote:
Probably it would be necessary to work on a subset of values, let's say: getting the average value of the 10% of the brightest pixels to be compared to the average of the 10% of the darkest pixels.
What is your opinion?

I don't think that you will find an absolute formula that will produce optimal results for all images. You might be able to find through experimentation a threshold that works for many typical pictures, but "contrast" is not a very precise term. (Personally, I consider the most meaningful approach to contrast to be the difference in lightness between a subject and its background.)

You may very well find that a particular heuristic based on standard deviation produces useful results. Try it (and write a doctoral thesis if it works).

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: get info on saturation/contrast of an image
PostPosted: Fri Dec 05, 2014 2:48 am  (#9) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Thanks, Saul!
I will try to work around the standard deviation.
Should I get (by chance...) something useful for somebody else, I will post it here...

_________________
"Where am I ?"


Top
 Post subject: Re: get info on saturation/contrast of an image
PostPosted: Thu Dec 31, 2015 9:02 am  (#10) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
(topic's resurrection)

OK, maybe Saul you may now test my filter ModLCS, attached to the recent post related to Crystallize (A GIFT FOR OMG) as a standalone piece of software to "enhance" an image on Luminosity-Contrast-Saturation.
It makes use of your piece of code, kindly provided here.
Let me know your feeling.
...and Happy New Year!

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Saturation H M L plug-in

17

No new posts Getting Black from Hue/Saturation

1

No new posts Attachment(s) GEGL Vibrance - fancy saturation plugin (probably canceled)

8

No new posts Attachment(s) WHERE IS THE 'DISCARD TEXT INFO' IN THE NEWEST GIMP? I CANNOT FIND IT

10

No new posts Retexture an image with another image as "Image Pattern" Plug-in

4



* Login  



Powered by phpBB3 © phpBB Group