Accessing a matrix element in the “Mat” object (not the CvMat object) in OpenCV C++

前端 未结 5 1016
有刺的猬
有刺的猬 2020-12-08 06:50

How to access elements by row, col in OpenCV 2.0\'s new \"Mat\" class? The documentation is linked below, but I have not been able to make any sense of it. http://opencv.wi

5条回答
  •  时光取名叫无心
    2020-12-08 07:27

    The ideas provided above are good. For fast access (in case you would like to make a real time application) you could try the following:

    //suppose you read an image from a file that is gray scale
    Mat image = imread("Your path", CV_8UC1);
    //...do some processing
    uint8_t *myData = image.data;
    int width = image.cols;
    int height = image.rows;
    int _stride = image.step;//in case cols != strides
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            uint8_t val = myData[ i * _stride + j];
            //do whatever you want with your value
        }
    }
    

    Pointer access is much faster than the Mat.at<> accessing. Hope it helps!

提交回复
热议问题