public class MedianCutContourRemoval extends ImageToImageOperation
Quantization is an operation that reduces the number of colors in
an image while trying to remain as close to the original image
as possible.
Standard Median Cut quantization is implemented in the
MedianCutQuantizer
class.
This class implements an algorithm that improves the standard implementation. It repeatedly calls the original quantizer and adjusts the palette in order to reduce the amount of contouring errors.
RGB24Image
object as input and produces a Paletted8Image
as output.
RGB24Image inputImage = ...; // image to be processed, from a file etc. MedianCutQuantizer quantizer = new MedianCutQuantizer(); quantizer.setPaletteSize(256); MedianCutContourRemoval removal = new MedianCutContourRemoval(); removal.setQuantizer(quantizer); removal.setInputImage(inputImage); removal.setTau(11.0); removal.setNumPasses(3); removal.process(); PixelImage outputImage = removal.getOutputImage();
MedianCutQuantizer
is used for that purpose.
This results in a palette and an output image that was mapped from
the original using the palette.
CoOccurrenceFrequencyMatrix
is created
from a CoOccurrenceMatrix
, which is in
turn created from the paletted image that was produced in the previous step.
The co-occurrence frequency matrix stores how often a pixel value j is the
neighbor of pixel i in the image, in relation to all occurrences of i.
The matrix stores this information as double
values between
0.0
and 1.0
.
If the value is 0.6
, j makes 60 percent of all neighboring
pixels of i.
setTau(double)
is a
distance value in RGB space (tau is adjusted for an RGB cube where each
axis can take values from 0 to 255).
It is used to define a threshold for similar colors.
A pair of compressible colors may not differ by more than tau.
It is guaranteed that no color can be both compressible and contouring.
setNumPasses(int)
method.
Usually, more than eight iterations do not make a difference.
MedianCutQuantizer
Modifier and Type | Field | Description |
---|---|---|
private Vector |
compressibleNodes |
|
private Vector |
contouringPairs |
|
static int |
DEFAULT_NUM_PASSES |
The default number of passes, used if they are not specified
with
setNumPasses(int) . |
static double |
DEFAULT_TAU |
The default tau value, used if none is specified
with
setTau(double) . |
private MedianCutNode[] |
leaves |
|
private double[] |
meanC |
|
private double |
meanS |
|
private int |
numPasses |
|
private Palette |
palette |
|
private MedianCutQuantizer |
quantizer |
|
private double[] |
stdDevC |
|
private double |
stdDevS |
|
private double |
sumMeanStdDevS |
|
private double |
tau |
Constructor | Description |
---|---|
MedianCutContourRemoval() |
Modifier and Type | Method | Description |
---|---|---|
private double |
computeDistance(int index1,
int index2) |
|
private void |
computeStatistics(CoOccurrenceFrequencyMatrix matrix) |
Computes the mean and standard deviation (stddev) values and
from the argument matrix and initializes the mean / stddev
fields of this class with them.
|
private Vector |
createContouringIndexList() |
Takes
|
private void |
findColorPairs(CoOccurrenceFrequencyMatrix matrix,
CoOccurrenceMatrix A) |
|
static void |
main(String[] args) |
Small command line application that performs a contour removal
on an image.
|
private void |
mergeAndSplit() |
|
void |
process() |
This method does the actual work of the operation.
|
void |
setNumPasses(int newValue) |
Specify the number of contour removal passes to be performed.
|
void |
setQuantizer(MedianCutQuantizer medianCutQuantizer) |
Set the
MedianCutQuantizer
object to be used with this contour removal operation. |
void |
setTau(double newValue) |
Specify the tau value to be used by this operation.
|
private Object[] |
toArray(Vector list) |
Converts a Vector to an Object array.
|
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
public static final double DEFAULT_TAU
setTau(double)
.
Check the class documentation to find out more about
the meaning of tau: MedianCutContourRemoval
.public static final int DEFAULT_NUM_PASSES
setNumPasses(int)
.
Check the class documentation to find out more about
the meaning of that number of passes: MedianCutContourRemoval
.private Vector compressibleNodes
private Vector contouringPairs
private MedianCutNode[] leaves
private double[] meanC
private double meanS
private int numPasses
private Palette palette
private MedianCutQuantizer quantizer
private double stdDevS
private double[] stdDevC
private double sumMeanStdDevS
private double tau
private double computeDistance(int index1, int index2)
private void computeStatistics(CoOccurrenceFrequencyMatrix matrix)
matrix
- private Vector createContouringIndexList()
private void findColorPairs(CoOccurrenceFrequencyMatrix matrix, CoOccurrenceMatrix A)
public static void main(String[] args) throws Exception
args
- program arguments; must have length one, the only argument being the input image file nameException
private void mergeAndSplit()
public void process() throws MissingParameterException, OperationFailedException, WrongParameterException
Operation
process
in class Operation
MissingParameterException
- if any mandatory parameter was not given to the operationWrongParameterException
- if at least one of the input parameters was
not initialized appropriately (values out of the valid interval, etc.)OperationFailedException
public void setQuantizer(MedianCutQuantizer medianCutQuantizer)
MedianCutQuantizer
object to be used with this contour removal operation.
This is a mandatory parameter.
If process gets called before the quantizer object was specified,
a MissingParameterException
is thrown.medianCutQuantizer
- the quantizer object that will get used by this operationpublic void setNumPasses(int newValue)
DEFAULT_NUM_PASSES
is used.newValue
- number of passes, 1 or higherIllegalArgumentException
- if the argument is 0 or smallerpublic void setTau(double newValue)
DEFAULT_TAU
is used.newValue
- tau value, 0.0 or higherIllegalArgumentException
- if the argument is smaller than 0.0