c_cpp Opencv通历Mat

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Opencv通历Mat相关的知识,希望对你有一定的参考价值。

//The efficient way
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() != sizeof(uchar));
    int channels = I.channels();
    int nRows = I.rows;
    int nCols = I.cols * channels;
    if (I.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }
    int i, j;
    uchar* p;
    for (i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for (j = 0; j < nCols; ++j)
        {
            p[j] = table[p[j]];
        }
    }
    return I;
}

//The iterator(safe) way
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() != sizeof(uchar));
    const int channels = I.channels();
    switch (channels)
    {
    case 1:
    {
              MatIterator_<uchar> it, end;
              for (it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
                  *it = table[*it];
              break;
    }
    case 3:
    {
              MatIterator_<Vec3b> it, end;
              for (it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
              {
                  (*it)[0] = table[(*it)[0]];
                  (*it)[1] = table[(*it)[1]];
                  (*it)[2] = table[(*it)[2]];
              }
    }
    }
    return I;
}

//On-the-fly address calculation with reference returning
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() != sizeof(uchar));
    const int channels = I.channels();
    switch (channels)
    {
    case 1:
    {
              for (int i = 0; i < I.rows; ++i)
              for (int j = 0; j < I.cols; ++j)
                  I.at<uchar>(i, j) = table[I.at<uchar>(i, j)];
              break;
    }
    case 3:
    {
              Mat_<Vec3b> _I = I;

              for (int i = 0; i < I.rows; ++i)
              for (int j = 0; j < I.cols; ++j)
              {
                  _I(i, j)[0] = table[_I(i, j)[0]];
                  _I(i, j)[1] = table[_I(i, j)[1]];
                  _I(i, j)[2] = table[_I(i, j)[2]];
              }
              I = _I;
              break;
    }
    }
    return I;
}

以上是关于c_cpp Opencv通历Mat的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp OpenCV基本数据结构Mat

c_cpp mat to dlib to eigen

opencv怎么给mat赋值

opencv中MatND和Mat的区别

opencv mat的数据类型CV_32FC1变成CV_8UC1

No implementation found for long org.opencv.core.Mat.n_Mat() 错误使用 OpenCV