Post Reply 

Auto Threshold

Dec 8, 2011, 14:01
Post: #1
Auto Threshold
The Apply Threshold functionality in the ui has an option called Auto, which sets the lower threshold value. Any idea is this function/option is available to use in a plugin? The gimp-threshold and gimp-histogram functions doesn't seem to have these options
Find all posts by this user
Quote this message in a reply
Dec 9, 2011, 09:49
Post: #2
RE: Auto Threshold
I suppose this value could be easily recomputed (likely something like setting the threshold to the median as returned by the histogram), but this would need to know its actual definition, and I would have to dig in the source code for that, unless you find it documented somewhere.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 10, 2011, 10:57
Post: #3
RE: Auto Threshold
I searched the GIMP source code and came across this function gimp_histogram_get_threshold in gimphistogram.c which seems to do all that magic.

http://git.gnome.org/browse/gimp/tree/ap...1281b476d3

I'm trying to convert that algorithm into script-fu. However, unfamiliarity with Scheme is slowing me down. Will try and post a solution to this as soon as possible.

Thanks,
Aldrin
Find all posts by this user
Quote this message in a reply
Dec 10, 2011, 12:41
Post: #4
RE: Auto Threshold
Personally I find python a lot more useful. Scheme is limited to the Gimp built-ins. Python can use all the modules that comes with it (I have a script to dump/restore paths to CSV files, I didn't have to write the CVS parser). Object programming also make a lot of things easier to code, not speaking of the very nice Gimp interface that provides you with image/layers with native methods so you can avoid the PDB for many things and write more "natural" code.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 11, 2011, 08:34
Post: #5
RE: Auto Threshold
Here's the solution I finally used. Works only for grayscale images. Its the same algorithm as in the gimp_histogram_get_threshold function in gimphistogram.c

http://git.gnome.org/browse/gimp/tree/ap...istogram.c

Code:
(define (auto-threshold imagePath)
    (let*
        (
            (theImage (car (gimp-file-load
                                    RUN-NONINTERACTIVE
                                    imagePath
                                    imagePath
                            )
                    )
            )
                    
            (theDrawable (car  (gimp-image-get-active-drawable theImage)))
            (hist (get-hist theDrawable 0))
        )
        (get-auto-threshold hist)
    )
)

;returns the threshold
(define (get-auto-threshold hist)
    (let*
        (
            (hist_max (vector-ref hist 0))
            (chist (make-vector 256))
            (cmom (make-vector 256))
            (maxval 255) ;end - start
            (i 1)
            (tmp )
            (chist_max)
            (cmom_max)
            (bvar_max 0)
            (threshold 127)
        )
        
        (vector-set! chist 0 (vector-ref hist 0))
        (vector-set! cmom 0 0)
            
        (set! i 1)
        (while (<= i maxval)
            (if (> (vector-ref hist i) hist_max)
                (set! hist_max (vector-ref hist i))
            )
            (vector-set! chist i (+ (vector-ref chist (- i 1)) (vector-ref hist i)) )
            (vector-set! cmom i (+ (vector-ref cmom (- i 1)) (* i (vector-ref hist i))) )
            (set! i (+ i 1))
        )

        (set! chist_max (vector-ref chist maxval))
        (set! cmom_max (vector-ref cmom maxval))

        (set! i 0)        
        (while (< i maxval)
        (if (and (> (vector-ref chist i) 0) (< (vector-ref chist i) chist_max) )
            (let*
               ((bvar (/ (vector-ref cmom i) (vector-ref chist i))))

               (set! bvar (- bvar (/ (- cmom_max (vector-ref cmom i)) (- chist_max (vector-ref chist i)) ) ))
               (set! bvar (* bvar bvar))
               (set! bvar (* bvar (vector-ref chist i)) )
               (set! bvar (* bvar (- chist_max (vector-ref chist i)) ))

               (if (> bvar bvar_max)
                  (begin
                    (set! threshold i)
                    (set! bvar_max bvar)
                  )
                )

            )    
        )
        (set! i (+ i 1))
    )
    
    threshold
)     

    
)

;returns the raw histogram  with values 0-1 as an array
(define (get-hist drawable chan)
(let* (
(i 0)
(hist (make-vector 256))
)
(set! i 0)
(while (< i 256)
(vector-set! hist i (car (cddddr (gimp-histogram drawable chan i i))))
(set! i (+ i 1))
)
hist
)
)
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Auto transparency dbuske 2 1,032 Jan 25, 2013 18:44
Last Post: dbuske
Photo auto-cropping all layers and exporting or saving all layers as separate images? Pesquit 3 1,771 Mar 27, 2012 00:51
Last Post: ofnuts

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS