Sampled Static Visual Content
An Introduction with Examples in Java
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Sampling Static Visual Content
- Color Sampling:
- Converting from a continuous "rainbow" of colors to
a discrete finite palette of colors
- Sometimes called quantization
- Spatial Sampling:
- Another name for spatial discretization
- The most common scheme involves the use of a finite grid
with equal size cells
Sampling Static Visual Content (cont.)
- The Result:
- A grid (or matrix) of picture elements (or pixels),
each of which contains a single color in the palette
- Sometimes called a raster representation
- An Illustration:
Examples of Sampled Static Visual Content
"Quick Start"
Reading Sampled Visual Content
javaexamples/visual/statik/sampled/ImageCanvasApp.java
(Fragment: part2)
"Quick Start" (cont.)
Rendering Sampled Visual Content
javaexamples/visual/statik/sampled/ImageCanvas.java
"Quick Start" (cont.)
The Complete App
javaexamples/visual/statik/sampled/ImageCanvasApp.java
Manipulating BufferedImage
Objects
javaexamples/visual/statik/sampled/IdentityOp.java
(Fragment: copy)
Constructing BufferedImage
Objects
javaexamples/visual/statik/sampled/ImageFactory.java
(Fragment: step)
Constructing BufferedImage
Objects (cont.)
From a File/Resource
javaexamples/visual/statik/sampled/ImageFactory.java
(Fragment: method2)
Operating on Sampled Content (cont.)
An IdentityOp
javaexamples/visual/statik/sampled/IdentityOp.java
(Fragment: 1)
javaexamples/visual/statik/sampled/IdentityOp.java
(Fragment: 2)
javaexamples/visual/statik/sampled/IdentityOp.java
(Fragment: 3)
Operating on Sampled Content (cont.)
A More Interesting Example: GrayExceptOp
javaexamples/visual/statik/sampled/GrayExceptOp.java
(Fragment: skeleton)
Operating on Sampled Content (cont.)
GrayExceptOp
(cont.)
- An Observation:
- We probably want to keep colors that
are "similar to" to the given color
- Two Metrics:
- \(d_E = \sqrt{(R_1 - R_2)^2 + (G_1 - G_2)^2 + (B_1 - B_2)^2}
\)
- \(d_M = |R_1 - R_2| + |G_1 - G_2| + |B_1 - B_2|
\)
Operating on Sampled Content (cont.)
GrayExceptOp.areSimilar()
- Design Alternatives:
- Implement the metric in the method
- Use the strategy pattern
- Choosing Between the Two:
- Even though we are only going to use one metric,
the flexibility afforded by the strategy pattern
makes it worthwhile
Operating on Sampled Content (cont.)
Metrics
javaexamples/math/Metric.java
javaexamples/math/RectilinearMetric.java
Operating on Sampled Content (cont.)
GrayExceptOp.areSimilar()
(cont.)
javaexamples/visual/statik/sampled/GrayExceptOp.java
(Fragment: areSimilar)
Operating on Sampled Content (cont.)
GrayExceptOp
(cont.)
- Another Observation:
- We want the "gray scale" to be the same brightness/luminance
as the color image
- What We Know:
- Perceived luminance is a function of the total
signal transmitted to the brain. So,
\(R_g + G_g + B_g = R_c + G_c + B_c\)
- Shades of gray are mixtures of black and white.
So, \(R_g = G_g = B_g\)
- The Implication:
-
\(R_g = G_g = B_g = (R_c + G_c + B_c) / 3
\)
Operating on Sampled Content (cont.)
GrayExceptOp
(cont.)
javaexamples/visual/statik/sampled/GrayExceptOp.java
(Fragment: filter)
Convolutions (cont.)
The Kernel as a Grid/Matrix
Convolutions (cont.)
Applying the Convolution to One Pixel
\(
d_{i,j} = \sum_{r=-1}^{1} \sum_{c=-1}^{1} s_{i+r, j+c} k_{r,c}
\)
Convolutions (cont.)
Convolutions (cont.)
A System for Constructing Convolutions
Requirements
- Create different
BufferedImageOp
objects
- Conserve memory used for kernels
A System for Constructing Convolutions (cont.)
An Inflexible Factory
A System for Constructing Convolutions (cont.)
An Wasteful Factory
A System for Constructing Convolutions (cont.)
A Good Design
A System for Constructing Convolutions (cont.)
A Low-Level Design that Leads to a Repetitive Implementation
javaexamples/visual/statik/sampled/RepetitiveBufferedImageOpFactory.java
A System for Constructing Convolutions (cont.)
A Good Low-Level Design
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
(Fragment: createOp)
A System for Constructing Convolutions (cont.)
An Unmaintainable Imeplementation of the Enumeration
javaexamples/visual/statik/sampled/UnmaintainableConvolutions.java
A System for Constructing Convolutions (cont.)
A Good Imeplementation of the Enumeration
javaexamples/visual/statik/sampled/Convolutions.java
A System for Constructing Convolutions (cont.)
A Good Factory
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
Affine Transformations (cont.)
Scaling
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
(Fragment: AffineTransformOp)
Affine Transformations (cont.)
Rotation
javaexamples/visual/statik/sampled/RotationApp.java
(Fragment: rotate)
Look-Ups (cont.)
Creating a Photo Negative
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
(Fragment: LookupOp1)
Look-Ups (cont.)
"Night Vision"
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
(Fragment: LookupOp2)
Color Space Conversion
javaexamples/visual/statik/sampled/BufferedImageOpFactory.java
(Fragment: ColorConvertOp)
Cropping/Cutting
- Defined:
- Extraction of a rectangular "sub-image" from an image
- In Java:
- Use methods in the
Image
class
Cropping/Cutting (cont.)
An Example
javaexamples/visual/statik/sampled/SurveillanceApp.java
(Fragment: crop)
Design of a Sampled Static Visual Content System
Requirements
- Support the addition of sampled static visual content
- Support the removal of sampled static visual content
- Support \(z\)-ordering of sampled static visual content
- Support the transformation of sampled static visual content
- Support the rendering of sampled static visual content
Design of a Sampled Static Visual Content System (cont.)
Bad Alternatives (to add the functionality required for rendering)
- Sepcialize the
BufferedImage
class -- will lead to
code duplication when we consider described content
- Use the decorator pattern -- confusing because of the methods
in the
Image
interface
Implementing this Design
The Overall Structure
javaexamples/visual/statik/sampled/Content.java
(Fragment: skeleton)
Implementing this Design (cont.)
The "Setters"
javaexamples/visual/statik/sampled/Content.java
(Fragment: set)
Implementing this Design (cont.)
Transformations
javaexamples/visual/statik/sampled/Content.java
(Fragment: transform)
Implementing this Design (cont.)
Rendering
javaexamples/visual/statik/sampled/Content.java
(Fragment: render)
Implementing this Design (cont.)
A Factory
javaexamples/visual/statik/sampled/ContentFactory.java
There's Always More to Learn