Starter Lisp Program
Worksheet #9 - Components
;; Connect4 - starter file
(defparameter *nrows* 0)
(defparameter *ncols* 0)
(defparameter *threshold* 100)
(defun file-read (filename)
;; insert code here to read from the file
)
(defun file-write (file image)
;;insert code to write to the file
;;(the option :if-exists :supersede can be used for overwriting
)
(defun print-image (image)
"Prints out IMAGE nicely formatted."
)
(defun threshold (image)
(let ((image2 (make-array (list *nrows* *ncols*)))
)
;; If the pixel value is >= 100, set to 255
;; otherwise set to 0
image2
)
)
(defun convert-to-ones (image)
(let ((image2 (make-array (list *nrows* *ncols*)))
)
;; For component labeling, start out by labeling all
;; 255's as 0's and 0's as 1's
image2
)
)
(defun negate (a)
"Creates and returns a new 2-D image array in which
each element is the negation of its corresponding
element in the array A. I.E. 0 remains 0, and 1 becomes -1.
This will help identifying if a pixel has been visited."
(let ((b (make-array (list *nrows* *ncols*))))
b) )
;;; CONNECTED-COMPONENTS is the top-level function for
;;; the finding connected components of the input IMAGE.
(defun connected-components (image)
"Calls SCAN on the negation of IMAGE."
(scan (negate image)) )
(defun scan (image)
"Performs a raster-scan of the image looking for
successive connected components."
(let ((count 0))
;; Go through the image matrix, pixel by pixel. Whenever
;; a pixel's value is -1, increase the count and
;; call depth first search. This will label the
;; 4-connected component with "count".
image) ;; end the "let" and return the image
)
(defparameter *directions* ; 4-adjacency definition.
'((-1 0)(0 -1)(0 1)(1 0)) )
(defun dfs (image count i j)
"Conducts a depth-first search from position I, J for
more cells in the current connected component."
;; If cell (i, j) = -1 it is an unvisited pixel in the component.
;; Mark the pixel with count and then use dolist to go through
;; each coordinate in *directions* by calling the branch function.
;; If pixel value is not equal to -1, return nil
;; (Don't continue if cell is already labelled or background).
)
(defun branch (direction image count i j)
"Determine row and column indexes of new cell using direction.
Check array bounds (hint: between 0 and numrows-1 and
0 and numcols-1, inclusive)
Then call dfs again with the array, the count, and the new
cell's indexes"
)
(defun test ()
(let ((image)
(image2)
(image3)
)
(setf image (file-read "image1.pgm"))
(setf image2 (threshold image))
(setf image3 (convert-to-ones image2))
(file-write "binaryThresh.pgm" image2)
(format t "Initial image:~%")
(print-image image)
(format t "~%Threshold binary image:~%")
(print-image image2)
(format t "~%Threshold binary ones image:~%")
(print-image image3)
(format t "~%Connect-4 image:~%")
(print-image
(connected-components
image3)
)
)
)
(test) ;;This runs the program when loaded