Edge Detection on an Image
Lisp Starter File
Worksheet #9 - Edge Detection

(defparameter *nrows* 0)
(defparameter *ncols* 0)

(defun file-read (filename)
   (let ((image)
        )
      ;; insert the code to read the image file, fig1.pgm.
      ;; Assume the beginning of the file is:
      ;; P2
      ;; # Comment line
      ;; ncols  nrows
      ;; max pixel value
  
    image
   )
)
 
(defun file-write (file image)
 
   ;; Write the image back to a file
   ;; Your header should be:
   ;; P2
   ;; # optional comment line
   ;; ncols nrows
   ;; max pixel value (255)

)
        
(defun horizontal-differences (image)
  (let ((image2 (make-array (list *nrows* *ncols*)))
       )

    ;; insert code here.
    ;;    Image2[i,j] = Image[i,j+1] - Image[i,j]
    ;;    Don't forget to make all the pixels in the last column
    ;;    equal to 0

     image2
   )
)

(defun roberts-cross (image1)
  "Returns result of applying Roberts' cross operator."
  (let ((image2 (make-array (list *nrows* *ncols*))))

   ;; insert code here
   ;; Image2[i,j] = sqrt (sqr (image1[i,j] - image1[i+1, j+1]) +
                          sqr (image1[i,j+1] - image1[i+1, j]))
   ;; Use (floor x) to truncate the sqrt result
   ;; Also set the pixels in the last row and last column to 0

    image2) 
)

(defun sqr (x)
  "Returns X squared."
  (* x x) 
)

(defun sobel (image1)
  "Returns result of applying Sobel's operator."
  (let ((image2 (make-array (list *nrows* *ncols*)))
        (a) (b) (c) (d) (e) (f) (g) (h) (i) (dx) (dy)
        (temp)
       )

     ;; for this pattern around pixel e:
#|       a  b  c
         d  e  f
         g  h  i

        dx = c + 2f + i - a - 2d - g
        dy = a + 2b + c - g - 2h - i
        
        Image[i,j] = sqrt (sqr(dx) + sqr(dy))
        Use (floor x) to truncate the sqrt.
        If the value is > 255, reset it to 255 so that 
        255 is the max value
|#
    ;;Set the first and last rows and cols to 0

    image2) 
)
(defun test ()
    (let ((image)
          (image2)
          (image3)
          (image4)
          (image5)
         )
      (setf image (file-read "fig1.pgm"))
;;      (print image)
      (setf image2 (horizontal-differences image))
      (setf image3 (vertical-difference image))
      (setf image4 (roberts-cross image))
      (setf image5 (sobel image))
      (file-write "fig1HDiff.pgm" image2)
      (file-write "fig1VDiff.pgm" image3)
      (file-write "fig1Roberts.pgm" image4)
      (file-write "fig1Sobel.pgm" image6)
    )
)
  
(test)