public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* imagej - a clojure example
@ 2015-09-18 23:38 David Pirotte
  2015-09-24 18:31 ` David Pirotte
  0 siblings, 1 reply; 5+ messages in thread
From: David Pirotte @ 2015-09-18 23:38 UTC (permalink / raw)
  To: kawa


[-- Attachment #1.1: Type: text/plain, Size: 1142 bytes --]

Hello,

1]

For info, here is the tutorial I used to start to try to use imagej from clojure:

	http://imagej.net/Clojure_Scripting


	[ Note that section 1.1 is obsolate, if you read, start at section 1.2
	[ for a [terribly basic] repl, I do:
	[
	[	java -cp clojure.jar:ij-core.jar:cljs clojure.main
	[
	[ 	where clj is a symlink to the path of .clj files

Any similar document, even for another library? How to do these things in kawa
would be fantastic for a zero/null java guy like me :), see subsections:

	- You can specify imports in Clojure in a few ways
	- Calling methods and variables on a java object
	- ...
	- Calling static fields and methods: namespace syntax
	- ...
	- Creating objects: invoking constructors

2]

	attached a very first module I should port to kawa, for info as well

	[  I do not ask to do my job here, but any hint(s) to get me me of the ground
	[  more then welcome

	java -cp clojure.jar:ij-core.jar:cljs clojure.main
	Clojure 1.6.0
	user=> (import '(ij IJ)
        		'(ij ImagePlus))
	user=> (def bf (ij-open "bf.png"))
	user=> (ij-show bf)
	...

Cheers,
David

[-- Attachment #1.2: ij_support.clj --]
[-- Type: application/octet-stream, Size: 5293 bytes --]

;; -*- mode: clojure; coding: utf-8 -*-

;;;;
;;;; Copyright (C) 2015
;;;; David Pirotte <david at altosw dot be>

;;;; Get-particles is part of a set of image processing scripts.

;;;; Get-particles is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License as
;;;; published by the Free Software Foundation, either version 3 of
;;;; the License, or (at your option) any later version.

;;;; It is distributed in the hope that it will be useful, but WITHOUT
;;;; ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; General Public License for more details.

;;;; You should have received a copy of the GNU General Public License
;;;; along with this s/w.  If not, see <http://www.gnu.org/licenses/>.
;;;;

;;; Commentary:

;;   <- stands for ->
;; bb		bounding-box
;; iplus	ImagePlus
;; iproc	ImageProcessor
;; istack 	ImageStack
;; icalc	ImageCalculator

;;; Code:


(ns ij-support
  "imagej support"
  (:import (ij IJ)
           (ij ImagePlus)
           (ij ImageStack)
           (ij.measure Measurements)
           (ij.measure ResultsTable)
           (ij.gui Roi)
           (ij.process ImageProcessor)
           (ij.plugin ImageCalculator)
           (ij.plugin.frame RoiManager)
           (ij.plugin.filter EDM)
           (ij.plugin.filter Analyzer)
           (ij.plugin.filter PlugInFilter)
           (java.io File)))


(defn ij-open [filename]
  (IJ/openImage filename))

(defn ij-save [iplus filename]
  (IJ/save iplus filename))

(defn ij-show [iplus]
  (.show iplus))

(defn ij-set-scale-pixel [iplus]
  (IJ/run iplus "Set Scale..." "distance=0 known=0 pixel=1 unit=pixel"))

(defn ij-clear-results [ & [rt]]
  (let [rtable (if rt rt (ResultsTable/getResultsTable))]
    ;; (ResultsTable/getResultsWindow)  <- these
    ;; (.show "Results") 		<- 2 lines do not work, will write our own
    (.reset rtable)))

(defn ij-get-bb-values [ & [rt]]
  (let [rtable (if rt rt (ResultsTable/getResultsTable))
        nb-rows (.getCounter rtable)]
    [nb-rows
     (doall
      (for [i (range nb-rows)]
        [(.getValue rtable "BX" i)
         (.getValue rtable "BY" i)
         (.getValue rtable "Width" i)
         (.getValue rtable "Height" i)]))]))

(defn ij-8bit [iplus]
  (IJ/run iplus "8-bit" ""))

(defn ij-16bit [iplus]
  (IJ/run iplus "8-bit" ""))

(defn ij-make-binary [iplus]
  (IJ/run iplus "Make Binary" ""))

(defn ij-set-threshold [iplus inf sup & [mode]]
  (if mode
    (IJ/setThreshold iplus inf sup mode)
    (IJ/setThreshold iplus inf sup ""))
  (IJ/run iplus "Convert to Mask" ""))

(defn ij-invert-lut [iplus]
  (IJ/run iplus "Invert LUT" ""))

(defn ij-scrap-inf [iplus min-area]
  (let [analyze (str "size=" min-area "-Infinity show=Masks in_situ")]
    (IJ/run iplus "Set Measurements..." "area redirect=None decimal=3")
    (IJ/run iplus "Analyze Particles..." analyze)))

(defn ij-get-bounding-boxes [iplus]
  (IJ/run iplus "Set Measurements..." "bounding redirect=None decimal=3")
  (IJ/run iplus "Analyze Particles..." "show=Nothing clear")
  (ij-get-bb-values))

(defn ij-clean-bounding-box [iplus]
  (let [b (bean iplus)
        i-width (* 1.0 (:width b))
        i-height (* 1.0 (:height b))]
    (ij-set-threshold iplus 1 255 "Red")
    ;; add stands for add to overlay [see overlays in imagej's doc]
    (IJ/run iplus "Set Measurements..." "bounding add redirect=None decimal=3")
    (IJ/run iplus "Analyze Particles..." "show=[Count Masks] clear in_situ")
    (let [[nb-objs results] (ij-get-bb-values)]
      (loop [i 0]
        (if (< i nb-objs)
          (let [[x y width height] (nth results i)]
            ;; (println ["i: " i x y width height])
            (if (and (= x 0.0)
                     (= y 0.0)
                     (= width i-width)
                     (= height i-height))
              (let [pos (inc i)]
                (ij-set-threshold iplus (- pos 0.1) (+ pos 0.1)))
              (recur (inc i)))))))))

(defn ij-get-max-area [iplus]
  (IJ/run iplus "Set Measurements..." "area redirect=None decimal=3")
  (IJ/run iplus "Analyze Particles..." "show=Nothing clear")
  ;; (IJ/run iplus "Invert LUT" "")
  (let [rt (ResultsTable/getResultsTable)
        nb-objs (.getCounter rt)]
    (reduce max 0
            (doall (for [i (range nb-objs)] (.getValue rt "Area" i))))))

(defn ij-set-roi [iplus x y width height]
  (let [iproc (.getProcessor iplus)
        roi (new Roi x y width height)]
    (.setRoi iproc roi)))

(defn ij-crop [iplus x y width height]
  (let [iproc (.getProcessor iplus)]
    (ij-set-roi iplus x y width height)
    (new ImagePlus "" (.crop iproc))))

(defn ij-duplicate [iplus title]
  (new ImagePlus title (.duplicate (.getProcessor iplus))))

(defn ij-and [iplus1 iplus2]
  (let [icalc (new ImageCalculator)]
    (.run icalc "AND create" iplus1 iplus2)))


;;;
;;; Testing
;;;

(comment
  (import '(ij IJ)
          '(ij ImagePlus)
          '(ij.measure Measurements)
          '(ij.measure ResultsTable)
          '(ij.gui Roi)
          '(ij.process ImageProcessor)
          '(ij.plugin ImageCalculator)
          '(ij.plugin.frame RoiManager)
          '(ij.plugin.filter EDM)
          '(ij.plugin.filter Analyzer)
          '(ij.plugin.filter PlugInFilter)))

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-09-25  1:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-18 23:38 imagej - a clojure example David Pirotte
2015-09-24 18:31 ` David Pirotte
2015-09-24 21:02   ` Per Bothner
2015-09-24 23:52     ` David Pirotte
2015-09-25  1:24       ` David Pirotte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).