OpenCV 3.4 Scalar parameter description, used in conjunction with CvType

Advertising column: welcome to follow my personal blog

When we were learning OpenCV, we found that when constructing Mat, there will be 2 key parameters, a CvType and a Scalar. Many people don't understand the meaning of these two parameters, so when they use other people's examples or write them themselves, they find that they don't know how to fill in this parameter. This article will explain the Scalar parameter in detail.

Scalar is a parameter used with CvType. If you don't know CvType, you can first read the OpenCV 3.4 I wrote to understand the CvType type description to understand what an image channel is and the structure of CvType.

The following examples in this article are all JAVA codes, and the implementations in other languages ​​are similar

Scalar Constructor

public class Scalar {
    public double[] val;

    public Scalar(double v0, double v1, double v2, double v3) {
        this.val = new double[]{v0, v1, v2, v3};
    }

    public Scalar(double v0, double v1, double v2) {
        this.val = new double[]{v0, v1, v2, 0.0D};
    }

    public Scalar(double v0, double v1) {
        this.val = new double[]{v0, v1, 0.0D, 0.0D};
    }

    public Scalar(double v0) {
        this.val = new double[]{v0, 0.0D, 0.0D, 0.0D};
    }

    public Scalar(double[] vals) {
        if (vals != null && vals.length == 4) {
            this.val = (double[])vals.clone();
        } else {
            this.val = new double[4];
            this.set(vals);
        }

    }

    .......
}

You can see that there are 5 constructors, the parameters are not complicated, and it is easy to understand. In addition to the constructor for the input parameters of public Scalar(double[] vals)this array, the other four constructors are constructors that pass in 1, 2, 3, and 4 double types, respectively. So what do these parameters mean and what are their value ranges?

Meaning of Scalar parameters

Scalar must be used in conjunction with CvType, which represents the value of the specified channel in CvType. If you don't know CvType, you can first read the OpenCV 3.4 I wrote to understand the CvType type description to understand what is an image channel and the structure of CvType.

Construction instructions:

  1. The number of input parameters (values) of the constructor <= the number of channels [if it is a 2-channel image, use a constructor with 4 parameters, the latter 2 parameters do not work]
  2. When the number of input parameters (values) of the constructor < the number of channels, the channel value for which no value is passed in is 0
  3. For multi-channel images, input parameter order new Scalar(B,G,R,alpha)

Here's an example:


import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;

import org.junit.Test;
import org.opencv.core.Size;

public class TestScalar {
    
    /**
     * 创建灰色图像
     */
    @Test
    public void createGrayImage(){
        //选择一个单通道类型 CV_8UC1
        //单通道下 0 为黑,255为白,取中间值 125 ,灰色。
        Mat sourceImage = new Mat(100,100, CvType.CV_8UC1, new Scalar(125));
        Imgcodecs.imwrite("savePath/gary-image.png",sourceImage);
    }

    /**
     * 创建蓝色图像
     */
    @Test
    public void createBlueImage(){
        //蓝色为彩色图,所以通道类型为3通道,CV_8UC3
        //蓝色是 RGB 中的 B,其RGB值为 R:0  ,  G:0  ,  B:255
        //Scalar 入参顺序为 new Scalar(B,G,R,A)。
        //new Scalar(255) 的意思为 B 通道值为255 ,G、R 值不传递的话,默认为0

        Mat sourceImage = new Mat(100,100, CvType.CV_8UC3, new Scalar(255));
        Imgcodecs.imwrite("savePath/blue-image.png",sourceImage);
    }

    /**
     * 创建绿色图像
     */
    @Test
    public void createGreenImage(){
        //绿色为彩色图,所以通道类型为3通道,CV_8UC3
        //绿色是 RGB 中的  G,其RGB值为 R:0  ,  G:255  ,  B:0
        //Scalar 入参顺序为 new Scalar(B,G,R,A)。
        //new Scalar(0,255) 的意思为 B 通道值为0,G通道值为255,R 值不传递的话,默认为0

        Mat sourceImage = new Mat(100,100, CvType.CV_8UC3, new Scalar(0,255));
        Imgcodecs.imwrite("savePath/green-image.png",sourceImage);
    }

    /**
     * 创建红色图像
     */
    @Test
    public void createRedImage(){
        //红色为彩色图,所以通道类型为3通道,CV_8UC3
        //红色是 RGB 中的 R,其RGB值为 R:255  ,  G:0  ,  B:0
        //Scalar 入参顺序为 new Scalar(B,G,R,A)。
        //new Scalar(0,0,255) 的意思为 B 通道值为 0,G 通道值为 0,R 通道值为 255

        Mat sourceImage = new Mat(100,100, CvType.CV_8UC3, new Scalar(0,0,255));
        Imgcodecs.imwrite("savePath/red-image.png",sourceImage);
    
    }


    /**
     * 创建透明图像
     */
    @Test
    public void createAlphaImage(){
        //透明图像为4通道图像,所以通道类型为4通道,CV_8UC4
        //创建一个半透明的红色图像来试试
        //红色是 RGB 中的 R,其RGB值为 R:255  ,  G:0  ,  B:0
        //Scalar 入参顺序为 new Scalar(B,G,R,A)。
        //new Scalar(0,0,255,125) 的意思为 B 通道值为 0,G 通道值为 0,R 通道值为 255,Alpha 通道值为125 (255为不透明,0为全透明会忽略所有的 RGB 颜色)

        Mat sourceImage = new Mat(100,100, CvType.CV_8UC4, new Scalar(0,0,255,125));
        Imgcodecs.imwrite("savePath/alpha-image.png",sourceImage);
    }
}

Through the above example, you should already understand the meaning of Scalar, which is the value of the channel

Data range for Scalar parameters

The value of the processing Alpha channel is 0~255, and the value range of other channels is determined according to the number of bits (Bite) of the image. You can check OpenCV 3.4 to understand the description of CvType type to understand what is an image channel, the structure of CvType, and the value range of the channel under each Bite later.

Advertising column: welcome to follow my personal blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324392815&siteId=291194637