Amrmsmb
Amrmsmb

Reputation: 1

How to specify a color using Scalar class

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);

Upvotes: 8

Views: 21821

Answers (3)

sOnt
sOnt

Reputation: 97

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.

Upvotes: 2

user3510227
user3510227

Reputation: 576

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.

Upvotes: 2

kiranpradeep
kiranpradeep

Reputation: 11201

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

Upvotes: 14

Related Questions