1

Suppose I have an image with several dots (as int the attached file). Is there a way for a script to find these dots and give me an array of them?

I guess I can do this via image processing, but I was hoping that there was a script that will do this. attached file

Yotam
  • 10,295
  • 30
  • 88
  • 128

1 Answers1

1

I don’t think is is a good idea to do this in Script-Fu. Iterating over pixels with Script-Fu is extremely slow, because it involves an overhead of allocating an array and a list for every pixel.

I’ve quickly written script to do this, but it is extremely slow — it takes about 5 minutes on my machine for your image:

; Returns an array of cons pairs
(define (count-points image)
  (let* ((duplicate (car (gimp-image-duplicate image)))
     (layer (car (gimp-image-flatten duplicate)))
     (width (car (gimp-image-width duplicate)))
     (heigth (car (gimp-image-height duplicate))))
    (display (gimp-drawable-is-gray layer)) (newline)
    (if (not (equal? (car (gimp-drawable-is-gray layer)) 1))
      (gimp-image-convert-grayscale duplicate))
    (plug-in-blur 0 duplicate layer)
    (gimp-threshold layer 0 127)
    (let loop ((x 0) (y 0) (result '()))
      (if (>= y heigth)
      result
      (if (>= x width)
          (loop 0 (+ y 1) result)
          (loop (+ x 1)
            y
            (let ((vals (cadr (gimp-drawable-get-pixel layer x y))))
              (if (< (aref vals 0) 127)
              result
              (cons (cons x y) result)))))))))

;; Call in Script-Fu Console like this (to calculate points
;; of the last image opened/created):
;; 
;;   (let ((imgs (gimp-image-list)))
;;     (count-points (vector-ref (cadr imgs) 0)))

I suggest exporting the image (blur it and use treschold before) to some dumb format like PGB/PBM and then doing the calculations with an external program in C or some other compilable language.

bloodstone
  • 11
  • 2