如何有效地将cv::Mat的给定通道设置为给定值,而不更改其他通道

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

本文关键字:通道 其他 设置 Mat 有效地 cv      更新时间:2023-10-16

如何在不更改其他通道的情况下有效地将cv::Mat的给定通道设置为给定值?例如,我想将其第四个通道(alpha通道(值设置为120(即半透明(,类似于:

cv::Mat mat; // with type CV_BGRA
...
mat.getChannel(3) = Scalar(120); // <- this is what I want to do

第页。S.:我目前的解决方案是首先将mat拆分为多个通道并设置alpha通道,然后将它们合并回来。

第页。S.2:我知道如果我也想通过改变其他频道,我可以很快做到这一点

mat.setTo(Scalar(54, 154, 65, 120)); 

使用通用解决方案更新:

这两种方法都适用于将给定通道的所有垫值设置为给定值。它们将适用于所有矩阵,无论它们是否连续。

方法-1-更高效

->基于@Antonio的回答,并由@MichaelBurdinov 进一步改进

// set all mat values at given channel to given value
void setChannel(Mat &mat, unsigned int channel, unsigned char value)
{
    // make sure have enough channels
    if (mat.channels() < channel + 1)
        return;
    const int cols = mat.cols;
    const int step = mat.channels();
    const int rows = mat.rows;
    for (int y = 0; y < rows; y++) {
        // get pointer to the first byte to be changed in this row
        unsigned char *p_row = mat.ptr(y) + channel; 
        unsigned char *row_end = p_row + cols*step;
        for (; p_row != row_end; p_row += step)
            *p_row = value;
    }
}

方法2-更优雅

->基于@MichaelBurdinov的回答

// set all mat values at given channel to given value
void setChannel(Mat &mat, unsigned int channel, unsigned char value)
{
    // make sure have enough channels
    if (mat.channels() < channel+1)
        return;
    // check mat is continuous or not
    if (mat.isContinuous())
        mat.reshape(1, mat.rows*mat.cols).col(channel).setTo(Scalar(value));
    else{
        for (int i = 0; i < mat.rows; i++)
            mat.row(i).reshape(1, mat.cols).col(channel).setTo(Scalar(value));
    }
}

p。S.:值得注意的是,根据文档,使用Mat::create()创建的矩阵总是连续的。但是,如果使用Mat::col()Mat::diag()等提取矩阵的一部分,或者为外部分配的数据构造矩阵标头,则此类矩阵可能不再具有此属性。

如果您的图像在内存中是连续的,您可以使用以下技巧:

mat.reshape(1,mat.rows*mat.cols).col(3).setTo(Scalar(120));

如果不是连续的:

for(int i=0; i<mat.rows; i++)
    mat.row(i).reshape(1,mat.cols).col(3).setTo(Scalar(120));

编辑(感谢Antonio的评论(:

请注意,此代码可能是最短的,并且它不会分配新内存,但它根本没有效率。它可能比拆分/合并方法还要慢。当OpenCV应该对一行中有1个像素的非连续矩阵执行操作时,它的效率确实很低。如果时间性能很重要,您应该使用@Antonio提出的解决方案。

只是对他的解决方案的一个小改进:

const int cols = img.cols;
const int step = img.channels();
const int rows = img.rows;
for (int y = 0; y < rows; y++) {
    unsigned char* p_row = img.ptr(y) + SELECTED_CHANNEL_NUMBER; //gets pointer to the first byte to be changed in this row, SELECTED_CHANNEL_NUMBER is 3 for alpha
    unsigned char* row_end = p_row + cols*step;
    for(; p_row != row_end; p_row += step)
         *p_row = value;
    }
}

这将保存x的增量操作,并在寄存器中少保存一个值。在资源有限的系统上,它可能会提供大约5%的加速。否则时间性能将相同。

Mat img;
[...]
const int cols = img.cols;
const int step = img.channels();
const int rows = img.rows;
for (int y = 0; y < rows; y++) {
    unsigned char* p_row = img.ptr(y) + SELECTED_CHANNEL_NUMBER; //gets pointer to the first byte to be changed in this row, SELECTED_CHANNEL_NUMBER is 3 for alpha
    for (int x = 0; x < cols; x++) {
         *p_row = value;
         p_row += step; //Goes to the next byte to be changed
    }
}

注意:根据opencv术语的使用,这适用于连续矩阵和不连续矩阵:http://docs.opencv.org/modules/core/doc/basic_structures.html#bool%20Mat::isContinuous%28%29%20const

关于直接Mat::数据访问(我很确定setTo((或oother opencv-Mat-api使用类似的解决方案(:

template<int N>
void SetChannel(Mat &img, unsigned char newVal) {   
    for(int x=0;x<img.cols;x++) {
        for(int y=0;y<img.rows;y++) {
            *(img.data + (y * img.cols + x) * img.channels() + N) = newVal;
        }
    }
}

int main() {
    Mat img = Mat::zeros(1000, 1000, CV_8UC4);
    SetChannel<3>(img, 120);
    imwrite("out.jpg", img);
    return 0;
}

简单算法:

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;
}
相关文章: