Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
rename color-flag to color and use :color :grayscale as values
Browse files Browse the repository at this point in the history
  • Loading branch information
Chouffe committed Jun 2, 2019
1 parent 33e6a80 commit cc54869
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
39 changes: 25 additions & 14 deletions contrib/clojure-package/src/org/apache/clojure_mxnet/image.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,35 @@
([input-stream]
(decode-image input-stream {})))

(s/def ::color #{:grayscale :color})
(s/def ::decode-image-opts-2 (s/keys :opt-un [::color ::to-rgb ::output]))

(defn- color->int
[color-flag]
(case color-flag
:grayscale 0
:color 1))

(defn decode
"Decodes an image from an input stream with OpenCV.
`input-stream`: `InputStream` - Contains the binary encoded image
`color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1)
`color`: keyword in `#{:color :grayscale}` - Convert decoded image to
grayscale or color
`to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB
format (instead of opencv's default BGR)
`output`: nil or `NDArray`
returns: `NDArray` with dtype uint8
Ex:
(decode input-stream)
(decode input-stream {:color-flag 1})
(decode input-stream {:color-flag 0 :output nd})"
([input-stream {:keys [color-flag to-rgb output]
:or {color-flag COLOR to-rgb true output nil}
(decode input-stream {:color :color})
(decode input-stream {:color :grayscale :output nd})"
([input-stream {:keys [color to-rgb output]
:or {color :color to-rgb true output nil}
:as opts}]
(util/validate! ::input-stream input-stream "Invalid input stream")
(util/validate! ::decode-image-opts opts "Invalid options for decoding")
(Image/imDecode input-stream color-flag to-rgb ($/option output)))
(util/validate! ::decode-image-opts-2 opts "Invalid options for decoding")
(Image/imDecode input-stream (color->int color) to-rgb ($/option output)))
([input-stream]
(decode input-stream {})))

Expand Down Expand Up @@ -126,26 +136,27 @@
"Reads an image file and returns an ndarray with OpenCV. It returns image in
RGB by default instead of OpenCV's default BGR.
`filename`: string - Name of the image file to be loaded
`color-flag`: 0 or 1 - Convert decoded image to grayscale (0) or color (1)
`color`: keyword in `#{:color :grayscale}` - Convert decoded image to
grayscale or color
`to-rgb`: boolean - Whether to convert decoded image to mxnet's default RGB
format (instead of opencv's default BGR)
`output`: nil or `NDArray`
returns: `NDArray` with dtype uint8
Ex:
(read \"cat.jpg\")
(read \"cat.jpg\" {:color-flag 0})
(read \"cat.jpg\" {:color-flag 1 :output nd})"
([filename {:keys [color-flag to-rgb output]
:or {color-flag nil to-rgb nil output nil}
(read \"cat.jpg\" {:color :grayscale})
(read \"cat.jpg\" {:color :color :output nd})"
([filename {:keys [color to-rgb output]
:or {color :color to-rgb nil output nil}
:as opts}]
(util/validate! ::filename filename "Invalid filename")
(util/validate! ::optional-color-flag color-flag "Invalid color flag")
(util/validate! ::color color "Invalid color")
(util/validate! ::optional-to-rgb to-rgb "Invalid conversion flag")
(util/validate! ::output output "Invalid output")
(Image/imRead
filename
($/option color-flag)
($/option (when color (color->int color)))
($/option to-rgb)
($/option output)))
([filename]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,33 @@
[src-path dest-path]
(fn [f]
(cp src-path dest-path)
(f)
(rm dest-path)))
(f)))

(use-fixtures :once (with-file image-src-path image-path))

(deftest test-decode-image
(let [img-arr (image/decode-image
(io/input-stream image-path))
img-arr-2 (image/decode-image
(io/input-stream image-path)
{:color-flag image/GRAYSCALE})]
(let [img-arr (image/decode-image (io/input-stream image-path))
img-arr-2 (image/decode-image (io/input-stream image-path)
{:color-flag image/GRAYSCALE})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))

(deftest test-decode
(let [img-arr (image/decode
(io/input-stream image-path))
img-arr-2 (image/decode
(io/input-stream image-path)
{:color-flag image/GRAYSCALE})]
(let [img-arr (image/decode (io/input-stream image-path))
img-arr-2 (image/decode (io/input-stream image-path)
{:color :grayscale})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))

(deftest test-read-image
(let [img-arr (image/read-image image-path)
img-arr-2 (image/read-image
image-path
{:color-flag image/GRAYSCALE})]
img-arr-2 (image/read-image image-path {:color-flag image/GRAYSCALE})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))

(deftest test-read
(let [img-arr (image/read image-path)
img-arr-2 (image/read
image-path
{:color-flag image/GRAYSCALE})]
img-arr-2 (image/read image-path {:color :grayscale})]
(is (= [576 1024 3] (ndarray/shape-vec img-arr)))
(is (= [576 1024 1] (ndarray/shape-vec img-arr-2)))))

Expand Down

0 comments on commit cc54869

Please sign in to comment.