Last active
July 27, 2018 00:56
-
-
Save nblumoe/5468625 to your computer and use it in GitHub Desktop.
A Clojure port of the OpenCV Java example from http://docs.opencv.org/2.4.4-beta/doc/tutorials/introduction/desktop_java/java_dev_intro.html described in a blog post here: http://nils-blum-oeste.net/image-analysis-with-clojure-up-and-running-with-opencv/#.UXq2UEAW3eU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns create-draw.opencv | |
(:import | |
org.opencv.core.Core | |
org.opencv.core.Mat | |
org.opencv.core.MatOfRect | |
org.opencv.core.Point | |
org.opencv.core.Rect | |
org.opencv.core.Scalar | |
org.opencv.highgui.Highgui | |
org.opencv.objdetect.CascadeClassifier)) | |
(defn create-classifier | |
[] | |
(CascadeClassifier. (.getPath (clojure.java.io/resource "lbpcascade_frontalface.xml" )))) | |
(defn load-image | |
[] | |
(Highgui/imread (.getPath (clojure.java.io/resource "lena.png")))) | |
(def face-detections (atom [])) | |
(defn detect-faces! | |
[classifier image] | |
(.detectMultiScale classifier image @face-detections)) | |
(defn draw-bounding-boxes! | |
[image] | |
(doall (map (fn [rect] | |
(Core/rectangle image (Point. (.x rect) (.y rect)) | |
(Point. (+ (.x rect) (.width rect)) | |
(+ (.y rect) (.height rect))) | |
(Scalar. 0 255 0))) | |
(.toArray @face-detections))) | |
(Highgui/imwrite "faceDetections.png" image)) | |
(defn process-and-save-image! | |
[] | |
(let [image (load-image)] | |
(detect-faces! (create-classifier) image) | |
(draw-bounding-boxes! image))) | |
(defn main | |
[] | |
(clojure.lang.RT/loadLibrary Core/NATIVE_LIBRARY_NAME) | |
(reset! face-detections (MatOfRect.)) | |
(process-and-save-image!)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment