How to set given channel of a cv::Mat to a given value efficiently without changing other channels?

后端 未结 4 809
走了就别回头了
走了就别回头了 2020-12-11 01:12

How to set given channel of a cv::Mat to a given value efficiently without changing other channels? For example, I want to set its fourth channel (alpha channel

4条回答
  •  一生所求
    2020-12-11 02:05

    Simple algorithm:

    void SetChannel(Mat mat, uint channel, uchar value)
    {
        const uint channels = mat.channels();
        if (channel > channels - 1)
            return;
    
        uchar * data = mat.data;
        uint N = mat.rows * mat.step / mat.elemSize1();
    
        for (uint i = channel; i < N; i += channels)
            data[i] = value;
    }
    

提交回复
热议问题