Example usage for org.opencv.core CvType CV_8UC

List of usage examples for org.opencv.core CvType CV_8UC

Introduction

In this page you can find the example usage for org.opencv.core CvType CV_8UC.

Prototype

public static final int CV_8UC(int ch) 

Source Link

Usage

From source file:org.lasarobotics.vision.util.color.Color.java

License:Open Source License

/**
 * Convert this color to a different colorspace and return a scalar
 *
 * @param to Colorspace to convert to/*from  w ww .ja  v  a 2  s.co  m*/
 * @return Scalar in other colorspace
 */
public Scalar convertColorScalar(ColorSpace to) {
    if (getColorSpace() == to)
        return getScalar();
    if (!getColorSpace().canConvertTo(to))
        throw new IllegalArgumentException("Cannot convert color to the desired color space.");

    Scalar output = this.getScalar();

    try {
        for (int i = 0; i < getColorSpace().getConversionsTo(to).length; i += 3) {
            int conversion = getColorSpace().getConversionsTo(to)[i];
            int inputDim = getColorSpace().getConversionsTo(to)[i + 1];
            int outputDim = getColorSpace().getConversionsTo(to)[i + 2];

            Mat pointMatTo = new Mat();
            Mat pointMatFrom = new Mat(1, 1, CvType.CV_8UC(inputDim), output);
            Imgproc.cvtColor(pointMatFrom, pointMatTo, conversion, outputDim);
            output = new Scalar(pointMatTo.get(0, 0));
            pointMatTo.release();
            pointMatFrom.release();
        }
    } catch (Exception ignored) {
        throw new IllegalArgumentException("Cannot convert color to the desired color space.");
    }

    return output;
}