user261002
user261002

Reputation: 2252

opencv copy one matrix into the first column of another matrix

I have a vector of pointers whcih are pointing to a image (size w=96 h=100 NoImg=100) that I got from my camrea and framegrabber. az you see I am creating matrix of them and start reading the data one by pixel. the next step is to create a bigger matrix(h=9600 w=100) which can hold all the pixels data of one image in the first column, and the next image data to second column, but the time that I reach to the 100 of images. I was wondering is there any sufficient way or pre ready method in opencv , that helps me to copy all the image data to the one column of another matrix directly?? or do I need to read all the pixels one by one, like my code at below and write them one by one inside another one? Edit I end up with the following code

cv::Mat _mainMatrix(_imageSize,_totalImgNO,CV_8UC1);    
//loop for creating a matrix of each image in the memory
for(counter=0;counter<_totalImgNO;counter++){
    unsigned char* _pointer=(unsigned char*)_imgPtrVector.at(counter);
    cv::Mat _matrixImage(cv::Size(width,height), CV_8UC1,_pointer , cv::Mat::AUTO_STEP);         
    channels=_matrixImage.channels();
    nRows=_matrixImage.rows * channels;
    nCols=_matrixImage.cols;
    if (_matrixImage.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }
    for(int i = 0; i < nRows; ++i)
    {
        p = _matrixImage.ptr<uchar>(i);
        for (int  j = 0; j < nCols; ++j)
        {
            if(iRow<_totalImgNO && jCol<_imageSize){                
                _mainMatrix.at<uchar>(iRow,jCol)=p[j];
                iRow++;
            }               
        }
    }
    jCol++;
    iRow=0; 
    _matrixImage.~Mat();            
}

Upvotes: 3

Views: 2423

Answers (2)

ypnos
ypnos

Reputation: 52357

OpenCV iterators are STL iterators. That means, you can use STL functions on them.

Rough example:

cv::Mat1b bigmat(9600, 100);
for (int i = 0; i < bigmat.cols; ++i) {
    cv::Mat1b column = bigmatrix.col(i);
    cv::Mat1b &source = *(images[i]);
    std::copy(source.begin(), source.end(), column.begin());
}

Upvotes: 2

jlengrand
jlengrand

Reputation: 12817

I think what you want is really close to the reshape function.

Tell me if I'm wrong :)

Opencv Reshape

Upvotes: 1

Related Questions