How to specify a color using Scalar class

12,328

Solution 1

Usage of Scalar to specify color, depends on the Mat type. Attempting to store/draw Red color on a grayscale Mat will fail.

  • Type CV_8UC1- grayscale image

    //8 bits per pixel and so range of [0:255]. 
    Scalar color = new Scalar( 255 )
    //For type: 16UC1, range of [0:65535]. For 32FC1 range is [0.0f:1.0f] 
    
  • Type CV_8UC3 - 3 channel color image

    // BLUE: color ordering as BGR
    Scalar color = new Scalar( 255, 0, 0 ) 
    
  • Type CV_8UC4 - color image with transparency

    //Transparent GREEN: BGRA with alpha range - [0 : 255]
    Scalar color = new Scalar( 0, 255, 0, 128 ) 
    

In the question, the first parameter to drawKeyPoints should be your source image(Mat) and not keypoints. The code would have compiled because MatOfKeyPoint is derived from Mat

Solution 2

You can use the method below to convert ARGB to Scalar

public static Scalar argbtoScalar(int r, int g, int b, int a) {
    Scalar s = new Scalar(b, g, r, a);
    return s;
}

a stands for Alpha which specifies the transparency.

Solution 3

Be sure to check out the Java API (http://docs.opencv.org/java/3.1.0/org/opencv/core/Scalar.html)

Scalar colour = new Scalar(B,G,R);

Where B,G,R are doubles, one for each colour channel.

Share:
12,328
user2121
Author by

user2121

Updated on June 16, 2022

Comments

  • user2121
    user2121 almost 2 years

    I do not know how to specifiy a color using Scalar class in the below posted method?

    Features2d.drawKeypoints(mKeyPoints_0, mKeyPoints_0, outImage, Scalar color, Features2d.DRAW_RICH_KEYPOINTS);
    
  • Beeing Jk
    Beeing Jk almost 6 years
    i'm new to OpenCV, and I'm so confused that since color = new Scalar( 255, 0, 0 ) means Blue, when I try out with mMat.setTo(color);, it shows a Red color mat, why is it so?