Determining template type when accessing OpenCV Mat elements

后端 未结 3 898
粉色の甜心
粉色の甜心 2021-02-06 08:02

I\'m using the following code to add some noise to an image (straight out of the OpenCV reference, page 449 -- explanation of cv::Mat::begin):

void
         


        
3条回答
  •  遥遥无期
    2021-02-06 08:34

    The problem is that you need determine to determine type and number of channels at runtime, but templates need the information at compile time. You can avoid hardcoding the number of channels by either using cv::split and cv::merge, or by changing the iteration to

    for(int row = 0; row < in.rows; ++row) {
        unsigned char* inp  = in.ptr(row);
        unsigned char* outp = out.ptr(row);
        for (int col = 0; col < in.cols; ++col) {
            for (int c = 0; c < in.channels(); ++c) {
                *outp++ = *inp++ + noise();
            }
        }
    }
    

    If you want to get rid of the dependance of the type, I'd suggest putting the above in a templated function and calling that from your function, depending on the type of the matrix.

提交回复
热议问题