lab/test.ss
#lang scheme/base

(require
 "../tools.ss"
 "gnuplot.ss"
 "image-io.ss"
 "hough.ss"
 )

;; Image processing library.

(define (sobel i)
  (define ^2 (U (lambda (x) (* x x))))
  ((B +)
   (^2 ((X -) i))
   (^2 ((Y -) i))))
     
(define (thresh t)
  (U (lambda (x) (if (> x t) 255 0))))



;; Hough transform. Compute statistics for a single angle. For each
;; point over threshold, add  x cos t + y sin t to the accumulator.

     
;; stuff

(define (pgm-gen dir)
  (generator
   (lambda (yield)
     (parameterize ((current-directory dir))
       (for/list ((file (directory-list)))
         (yield (load-pgm file)))))))


;; Convert pgm stream to raw video file for PF.

(define (pgm-sequence->raw dir file)
  (with-output-to-file file
    (lambda ()
      (parameterize ((current-directory dir))
        (for ((file (directory-list)))
             (write-image (load-pgm file)))))
    #:exists 'replace))



(define img (pgm-gen "/home/tom/LFSR/seq1"))


;; (define play (make-yuvplay))
(define play (make-yuv4mpeg-process "yuvplay" "-s" "512x384"))

(define (test)
  (for ((pgm (in-generator img)))
     (play pgm)))

(define i (img))


;; Hough transform specialized for the particular problem
(define hough
  (hough-transform #:r-bins 100     #:max-r 800
                   #:angle-bins 100 #:max-angle (atan 1)))

(define (seq-max seq)
  (let ((max 0))
    (for ((s seq))
      (when (> s max) (set! max s)))
    max))

;; Dynamic range compression
(define (compress img)
  (let* ((max (seq-max (image-data img)))
         (scale (/ 255 max)))
    ((U (lambda (x)
          (inexact->exact
           (floor (* x scale)))))
     img)))

;; (define (hough-transform img)
;;   (let ((edges ((thresh 700) (sobel img))))
;;     (for/list ((angle (in-range 80)))
;;       ((hough/histo angle) edges))))

;; (define (plot-hough angle)
;;   (lplot ((hough/histo angle) ((thresh 700) (sobel i)))))

;; (define (hough-movie)
;;   (for ((i (in-naturals)))
;;        (let ((a (/ i 200)))
;;          (printf "~a\n" (* (/ a (* 2 (atan 1))) 180))
;;          (plot-hough a))))