Class RGBToGrayConversion
- java.lang.Object
-
- net.sourceforge.jiu.ops.Operation
-
- net.sourceforge.jiu.ops.ImageToImageOperation
-
- net.sourceforge.jiu.color.reduction.RGBToGrayConversion
-
public class RGBToGrayConversion extends ImageToImageOperation
Converts RGB color images (both truecolor and paletted) to grayscale images. The weights to be used with the three base colors red, green and blue can be modified with a call tosetColorWeights(float, float, float).Supported image types
RGB24ImageandPaletted8Imagecan be used as input image types. AGray8Imagebe will be created from them.Could be optimized to use int multiplication instead of float multiplication.
Usage example
Convert some PixelImage rgbImage to grayscale:PixelImage grayImg = RGBToGrayConversion.convert(rgbImage);
Using your own color weights can be done like this. You may also want to specify an output grayscale image if you have one to reuse.RGBToGrayConversion rgbtogray = new RGBToGrayConversion(); rgbtogray.setInputImage(image); rgbtogray.setColorWeights(0.3f, 0.3f, 0.4f); rgbtogray.process(); PixelImage grayImage = rgbtogray.getOutputImage();
- Author:
- Marco Schmidt
-
-
Field Summary
Fields Modifier and Type Field Description private floatblueWeightstatic floatDEFAULT_BLUE_WEIGHTThe default weight for blue samples in the conversion, 0.11f.static floatDEFAULT_GREEN_WEIGHTThe default weight for green samples in the conversion, 0.59f.static floatDEFAULT_RED_WEIGHTThe default weight for red samples in the conversion, 0.3f.private floatgreenWeightprivate floatredWeight
-
Constructor Summary
Constructors Constructor Description RGBToGrayConversion()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static PixelImageconvert(PixelImage rgbImage)Static convenience method to convert an RGB image to a grayscale image.voidprocess()This method does the actual work of the operation.private voidprocess(Paletted8Image in)private voidprocess(RGB24Image in)private voidprocess(RGB48Image in)private voidprocess(RGBIntegerImage in, GrayIntegerImage out)voidsetColorWeights(float red, float green, float blue)Sets the weights for the three colors red, green and blue used in the conversion procedure.-
Methods inherited from class net.sourceforge.jiu.ops.ImageToImageOperation
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
-
Methods inherited from class net.sourceforge.jiu.ops.Operation
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
-
-
-
-
Field Detail
-
DEFAULT_RED_WEIGHT
public static final float DEFAULT_RED_WEIGHT
The default weight for red samples in the conversion, 0.3f.- See Also:
- Constant Field Values
-
DEFAULT_GREEN_WEIGHT
public static final float DEFAULT_GREEN_WEIGHT
The default weight for green samples in the conversion, 0.59f.- See Also:
- Constant Field Values
-
DEFAULT_BLUE_WEIGHT
public static final float DEFAULT_BLUE_WEIGHT
The default weight for blue samples in the conversion, 0.11f.- See Also:
- Constant Field Values
-
redWeight
private float redWeight
-
greenWeight
private float greenWeight
-
blueWeight
private float blueWeight
-
-
Method Detail
-
convert
public static PixelImage convert(PixelImage rgbImage) throws MissingParameterException, WrongParameterException
Static convenience method to convert an RGB image to a grayscale image.- Parameters:
rgbImage- input RGB image to be converted- Returns:
- a new grayscale image, created from the RGB input image
- Throws:
MissingParameterException- rgbImage is nullWrongParameterException- rgbImage's type is unsupported- Since:
- 0.14.2
-
process
private void process(RGBIntegerImage in, GrayIntegerImage out)
-
process
public void process() throws MissingParameterException, WrongParameterExceptionDescription copied from class:OperationThis method does the actual work of the operation. It must be called after all parameters have been given to the operation object.- Overrides:
processin classOperation- Throws:
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.)
-
process
private void process(Paletted8Image in) throws MissingParameterException, WrongParameterException
-
process
private void process(RGB24Image in) throws WrongParameterException
- Throws:
WrongParameterException
-
process
private void process(RGB48Image in) throws WrongParameterException
- Throws:
WrongParameterException
-
setColorWeights
public void setColorWeights(float red, float green, float blue)Sets the weights for the three colors red, green and blue used in the conversion procedure. For each RGB value(r, g, b)to be converted (whether in a truecolor image or in the palette), the formula isgray = r * red + g * green + b * blue. The default values for these weights areDEFAULT_RED_WEIGHT,DEFAULT_GREEN_WEIGHTandDEFAULT_BLUE_WEIGHT. This method lets the user change these values. Each of these arguments must be >= 0.0f and <= 1.0f. The sum of the three must be <= 1.0f. For any resulting gray value to be spread over the complete scale from 0.0f to 1.0f it is preferable for the sum to be equal to or at least close to 1.0f. However, this is not checked. The smaller the sum of the weights is, the darker the resulting gray image will become.- Parameters:
red- weight of the red sample in the conversion, between0.0fand1.0fgreen- weight of the green sample in the conversion, between0.0fand1.0fblue- weight of the blue sample in the conversion, between0.0fand1.0f- Throws:
IllegalArgumentException- if any one of the above mentioned constraints for the arguments is not met
-
-