gimp script-fu 中的自动阈值功能

发布于 2024-12-20 21:17:41 字数 131 浏览 5 评论 0原文

在 GIMP UI 中,有一个“应用阈值”功能 (GIMP 2.6),其中有一个名为“自动”的选项。这会自动计算图像的适当下阈值。此功能/选项可以在插件中使用吗? gimp-threshold 和 gimp-histogram 函数似乎没有此选项。

In the GIMP UI, there is an Apply Threshold functionality (GIMP 2.6) which has an option called Auto. This automatically calculates an appropriate lower threshold value for the image. Is this function/option available to use in a plugin? The gimp-threshold and gimp-histogram functions don't seem to have this option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-12-27 21:17:41

这是我最终使用的解决方案。仅适用于灰度图像。它与 gimphistogram.c

http: //git.gnome.org/browse/gimp/tree/app/base/gimphistogram.c

(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
)
)

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/app/base/gimphistogram.c

(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
)
)
清风挽心 2024-12-27 21:17:41

不幸的是,从 GIMP 版本 2.6 开始,此功能未向过程数据库 (API) 公开,因此不能在 script-fu 或 Python 脚本中使用。

Unfortunately, as of GIMP version 2.6, this feature is not exposed to the Procedural Database (API) ,and thus can't be used in a script-fu or Python script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文