how do i efficiently convert cv::Mat to vector<vector<uchar>> img. right now i am trying beolove code: vector<vector<vec3b>> inputImg(OrigImg.cols,vector<vec3b>(OrigImg.rows)); for(int i=0;i<origimg.cols;i++) {="" for(int="" j="0;j<OrigImg.rows;j++)" {="" inputimg[i][j]="backgroundSubImg.at<cv::Vec3b">(j,i); } } Why i'm trying to do is since it looks like accessing each pixel in cv::Mat or scanning hole image is little bit slower by using backgroundSubImg.at<cv::vec3b>(j,i); i want to access the color values of this pixels only by its position. Right now my algorithm is accessing every pixel 6404608 times in each cycle.