It is currently Thu Mar 28, 2024 9:05 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Color Correction Script (experimentation, very slow)
PostPosted: Mon Jun 09, 2014 5:18 pm  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
following this tutorial -> http://blog.patdavid.net/2011/06/getting-around-in-gimp-color-correction.html
and with gamma code given by saulgoode found here: http://gimpchat.com/viewtopic.php?f=9&t=10463&p=138668&sid=62915fb726e511676cddef9460074cff#p138664

I wrote a script to scan through the image pixel by pixel to find darkest point, brightest point and grayest point.
So the script works but is VERY slow, I don't dare run it on images larger than 200x200.
If you open the error console you can see it output which line it's on and it'll say "Complete!" when complete.

So my question is: "Is there anyway to speed this script up?". As of right now, if an image is thousands by thousands of pixel, it's way faster to follow the tutorial manually than running the script.

Script is available under Script-Fu -> Color Correction
Attachment:
color_correction.scm [6.92 KiB]
Downloaded 172 times


Go ->here<- for python version (much faster :roll: )

_________________
TinT


Last edited by trandoductin on Tue Jun 10, 2014 1:30 am, edited 3 times 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: Color Correction Script (experimentation, very slow)
PostPosted: Mon Jun 09, 2014 7:01 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4726
You can use the histogram plugin and some dichotomy to find the lowest/highest values:
mean, std_dev, median, pixels, count, percentile = pdb.gimp_histogram(drawable, channel, start_range, end_range)

_________________
Image


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Mon Jun 09, 2014 7:26 pm  (#3) 
Offline
GimpChat Member

Joined: May 23, 2012
Posts: 150
the simple answer, you already know is
add 2 rows of pixels together, x and y so your script believes its
one row, or skip every other row.

scan with binary engine with math designed to detect
dark, light, and half way between dark and light, or gray.

i see you set black to max
white to max
and gray in the exact middle

if you let the scan set the max, make the numbers variables,
not only for min and max but for color difference, the the numbers might for
example not start at 0 for black, but could start at say 50,
and might go to 255 for white, but say only 200 removing quite a
few computations


this might be the same as ofnuts answer but there might
be something useful here
don't have a good answer sorry
hope this understandable tin


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Mon Jun 09, 2014 9:27 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
trandoductin wrote:
So my question is: "Is there anyway to speed this script up?". As of right now, if an image is thousands by thousands of pixel, it's way faster to follow the tutorial manually than running the script.


;; Perform a binary search for the smallest value for a particular channel
;
(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 ))))))
;; Perform a binary search for the largest value for a particular channel
;
(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 )))))))


I have no idea what the "grayest point" would be.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:15 am  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
Thanks guys,
But i wrote it in python instead,
and found some way to access the pixel array, it's much faster now :) ,
you can see the progress bar as it runs.
For an image 2048x1152 it took about 9 seconds (still not as fast as I like it to be but it's usable).
Install in plug-ins folder, (it'll be available under Colors->Auto->Color Correction)
Attachment:
color-correction.zip [1.53 KiB]
Downloaded 228 times


below version allows you select black/white/gray points' colors
Attachment:
color-correction-pick-color.zip [1.6 KiB]
Downloaded 168 times


below version uses luminosity difference to seek out black/white/gray pixels instead of total RGB values to seek.
Attachment:
color-correction-luminosity.zip [1.62 KiB]
Downloaded 166 times


You can get => yet another version <= that takes 3 times as long, but because it does detects white point first and then sets levels, detects black point then sets levels, detects gray points then sets levels (trying to simulate the exact steps like in tutorial) It does produce a different result, and it doesn't produce different results if you try run multiple times like earlier versions.

_________________
TinT


Last edited by trandoductin on Tue Jun 10, 2014 6:27 pm, edited 8 times in total.

Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:44 am  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4726
trandoductin wrote:
Thanks guys,
But i wrote it in python instead,
and found some way to access the pixel array, it's much faster now :) ,
you can see the progress bar as it runs.
For an image 2048x1152 it took about 9 seconds (still not as fast as I like it to be but it's usable).
Install in plug-ins folder, (it'll be available under Colors->Auto->Color Correction)
Attachment:
color-correction.zip

Using gimp-histogram you wouldn't even need a progress bar...

_________________
Image


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 7:22 am  (#7) 
Offline
GimpChat Member

Joined: Jan 20, 2014
Posts: 66
Tried your python script, it doesn't produce the same as Pat's. From the diff, it looks only shifted the red channel and not others.


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 7:58 am  (#8) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4726
trandoductin wrote:
Thanks guys,
But i wrote it in python instead,
and found some way to access the pixel array, it's much faster now :) ,
you can see the progress bar as it runs.
For an image 2048x1152 it took about 9 seconds (still not as fast as I like it to be but it's usable).


The following code runs in under 10ms on a 14Mpx image on my Core I5 laptop:
def maxValue(layer,percent):
   low,high=0,255
   while (high-low) > 1:
      t=(low+high)/2
      _,_,_,_,_,pct=pdb.gimp_histogram(layer, HISTOGRAM_VALUE, t, 255)
#      print "Low: %d high: %d, t: %d pct: %3.1f -> %3.1f" % (low,high,t,pct*100,percent*100)
      if pct < percent:  # tried too high
         high=t
      else:
         low=t
   return low


It finds the value for which there are "percent" pixels with a higher value (this avoids skewing the result due to stray pixels), but you can use the pixel count instead.

_________________
Image


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 11:18 am  (#9) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
that's some crazy 'ss coding, too advanced for me. The speed sounds tempting.

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 12:00 pm  (#10) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4726
Nothing advanced :) try a value, see if too short or too long, determine on which side to jump next, and compute corresponding test value. Rinse and repeat.

At each iteration, you divide the interval where the value can be by 2, so to find a value among 256 you never do more that 8 iterations (i.e., 8 calls to the histogram function).

_________________
Image


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 12:36 pm  (#11) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
I am not sure what to call the function with, using what percent?
and what to do with the returned value?

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:30 pm  (#12) 
Offline
GimpChat Member
User avatar

Joined: Jan 10, 2013
Posts: 863
trandoductin wrote:
Go ->here<- for python version (much faster :roll: )

I used the Python version, but did not notice any change in the image.
Image
Untitled - Nathalie Martin - cc

_________________
bbbbbbbbbbbImage
bbbbbbbbbbb Be patient, English is not my language.


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:32 pm  (#13) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
okyl168 wrote:
Tried your python script, it doesn't produce the same as Pat's. From the diff, it looks only shifted the red channel and not others.

The script is pure experimentation, but you can try the very bottom version of this post

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:38 pm  (#14) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
Akros wrote:
trandoductin wrote:
Go ->here<- for python version (much faster :roll: )

I used the Python version, but did not notice any change in the image.
[ Image ]
Untitled - Nathalie Martin - cc


I tried it too on your image and didn't notice any difference either. I am guessing that your pure white is already pure white and your pure black is pure black and your pure gray is pure gray so the script doesn't do anything.

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:43 pm  (#15) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14801
Location: roma, italy
for me, this is obvious, it is the same that happens when using the auto-correction of Gimp: if there is just 1 pixel pure white, 1 pixel pure black and 1 pixel pure grey, nothing happens!
manually it's different...

_________________
"Where am I ?"


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:46 pm  (#16) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
but i tried the built-in colors->auto->white balance and it makes a huge difference, looks much better. If you want to do it manually, maybe select a sample average instead of single pixel (as your image looks very noisy) so single pixels of pure white/pure black/pure gray already exists..

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 1:58 pm  (#17) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14801
Location: roma, italy
I was referring more to Auto Stretching std functions in gimp

(this is what the script performs)

_________________
"Where am I ?"


Last edited by dinasset on Tue Jun 10, 2014 2:01 pm, edited 1 time in total.

Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 2:00 pm  (#18) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
oh got ya, i was talking to akros hehe

_________________
TinT


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 2:18 pm  (#19) 
Offline
GimpChat Member
User avatar

Joined: Jan 10, 2013
Posts: 863
The image I chose, has a certain degree of difficulty. Perhaps by having more than one light source. In case the flame in the grill.
Even the technique to detect the lightest and the darkest, points do not work well in this picture.
With visual adjustments, one can obtain best results.

_________________
bbbbbbbbbbbImage
bbbbbbbbbbb Be patient, English is not my language.


Top
 Post subject: Re: Color Correction Script (experimentation, very slow)
PostPosted: Tue Jun 10, 2014 2:35 pm  (#20) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3970
Location: Canada
just for fun i tried on a shark image found here
Attachment:
shark.jpg
shark.jpg [ 329.5 KiB | Viewed 1645 times ]

here's what happens if you run script one more time. it produce a different result (I didn't expect it to). But everytime after that the script no longer changes the image.
Attachment:
sharkmultipleruns.jpg
sharkmultipleruns.jpg [ 322.51 KiB | Viewed 1640 times ]


here's another play with rob ford found here
Attachment:
robford.jpg
robford.jpg [ 663.15 KiB | Viewed 1639 times ]

_________________
TinT


Top
Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Slow Change Kaleidoscope

7

No new posts Gimp running slow

1

No new posts Attachment(s) Legacy Mosaic in Gimp 2.10 because GEGL's version is so slow

10

No new posts Attachment(s) GEGL Color Light Fusion (12 blend modes for color overlay)

9

No new posts Attachment(s) Background color chanage and typed text color change

8



* Login  



Powered by phpBB3 © phpBB Group