Page history Edit this page How do I edit this website?
Original MediaWiki page

Blend two images

The content of this page has not been vetted since shifting away from MediaWiki. If you’d like to help, check out the how to help guide!

Purpose

A Clojure script illustrating how to:

  • Fetch images from the internet
  • Extract and iterate/loop the pixels on an image
  • Create a new image
  • Set the pixels of the image to a new value, in this case the average of two images

The script is maintained by Albert Cardona.

Code

Copy; Albert Cardona 20080427 at MPI-CBG Dresden Fiji hackathon.

; Opens a URL file path as an image
(let [opener (new ij.io.Opener)]
  (defn open-url [url]
    (.openURL opener url)))

; Fetch two example 512x512 images from the net
(let [baboon (open-url "https://imagej.net/ij/images/baboon.jpg")
      bridge (open-url "https://imagej.net/ij/images/bridge.gif")]
  ; Obtain color channel byte arrays for baboon color image
  (let [len (count (.. baboon (getProcessor) (getPixels))) ; could also say (* 512 512)
    r (make-array Byte/TYPE len)
    g (make-array Byte/TYPE len)
    b (make-array Byte/TYPE len)
    br (.. bridge (getProcessor) (getPixels))]
    ; Fill a copy of the channel arrays
    (.. baboon (getProcessor) (getRGB r g b))
    ; Blend the bridge pixels into each color channel of the baboon image
    (defn avg-byte [a b]
      (byte (/ (+ (bit-and a 255) b) 2)))
    (dotimes [i len]
      (let [pix (bit-and (aget br i) 255)]
    (aset r i (avg-byte (aget r i) pix))
    (aset g i (avg-byte (aget g i) pix))
    (aset b i (avg-byte (aget b i) pix))))
    ; Set the color channels
    (.. baboon (getProcessor) (setRGB r g b))
    ; Done!
    (.show baboon)))

; The above script is ready for a lot of macro abstraction

See also

Clojure Scripting

  1. GNU Lesser General Public License
    The GNU Lesser General Public License (LGPL) is a free, open source license for software and other kinds of works.
  2. Why Closed-Source Is Wrong
    This page has a few war stories about having to fight with developing software where you should not need to fight.
  3. GNU General Public License
    The GNU General Public License (GPL) is a free, open source license for software and other kinds of works.
  4. Apache Software License
    The Apache License is a permissive free software license, imposing minimal restrictions on the redistribution of covered software.
  5. Detect Information Loss
    How to detect various forms of information loss in images
  6. Eclipse Public License
    The Eclipse Public License (EPL) is a free, open source software license.
  7. Color Image Processing
    Images with color come in three different forms: pseudo-color, 24-bit RGB image, or color composite image.
  8. Why Open Source?
    This page highlights the benefits of open source software.
  9. BIG Licensing
    Components distributed via the BIG-EPFL update site were developed by the Biomedical Imaging Group (BIG) at the École Polytechnique Fédérale de Lausanne (EPFL).
  10. Public Domain
    Software in the public domain disclaims all copyright interest, being freely available to everyone for any purpose, with no attribution or acknowledgement required.
  11. Deconvolution
    corrects the systematic error of blur (loss of contrast in smaller features) in optical systems such as fluorescence microscopy images.
  12. Visualization
    is a set of techniques for graphically illustrating scientific data, enabling scientists to better understand, illustrate, and glean insight from their data.
  13. BSD Licenses
    BSD licenses are a family of permissive free software licenses, imposing minimal restrictions on the redistribution of covered software.
  14. Voxelization
    Voxelization is the process of converting a data structures that store geometric information in a continuous domain (such as a 3D triangular mesh) into a rasterized image (a discrete grid).
  15. MIT License
    The MIT License is a permissive free software license, imposing minimal restrictions on the redistribution of covered software.
  16. Licensing
    This page describes the legal structure of ImageJ and SciJava projects.