2

I am trying to store a IPL_DEPTH_8U, 3 channel image into an array so that I can store 100 images in memory.

To initialise my 4D array I used the following code (rows,cols,channel,stored):

int size[] = { 324, 576, 3, 100 };
CvMatND* cvImageBucket; = cvCreateMatND(3, size, CV_8U);

I then created a matrix and converted the image into the matrix

CvMat *matImage = cvCreateMat(Image->height,Image->width,CV_8UC3 );
cvConvert(Image, matImage );

How would I / access the CvMatND to copy the CvMat into it at the position of stored?

e.g. cvImageBucket(:,:,:,0) = matImage; // copied first image into array

1

3 Answers 3

2

You've tagged this as both C and C++. If you want to work in C++, you could use the (in my opinion) simpler cv::Mat structure to store each of the images, and then use these to populate a vector with all the images.

For example:

std::vector<cv::Mat> imageVector;
cv::Mat newImage;

newImage = getImage();       // where getImage() returns the next image,
                             // or an empty cv::Mat() if there are no more images
while (!newImage.empty())
{
    // Add image to vector
    imageVector.push_back(image);

    // get next image
    newImage = getImage();
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm guessing something similar to:

for ith matImage

memcpy((char*)cvImageBucket->data+i*size[0]*size[1]*size[2],(char*)matImage->data,size[0]*size[1]*size[2]);

Comments

0

Although I agree with @Chris that it is best to use vector<Mat> rather than a 4D matrix, this answer is just to be a reference for those who really need to use 4D matrices in OpenCV (even though it is a very unsupported, undocumented and unexplored thing with so little available online and claimed to be working just fine!).

So, suppose you filled a vector<Mat> vec with 2D or 3D data which can be CV_8U, CV_32F etc.

One way to create a 4D matrix is

vector<int> dims = {(int)vec.size(), vec[0].rows, vec[0].cols};
Mat m(dims, vec[0].type(), &vec[0]);

However, this method fails when the vector is not continuous which is typically the case for big matrices. If you do this for a discontinuous matrix, you will get a segmentation fault or bad access error when you would like to use the matrix (i.e. copying, cloning, etc). To overcome this issue, you can copy matrices of the vector one by one into the 4D matrix as follows:

Mat m2(dims, vec[0].type());
for (auto i = 0; i < vec.size(); i++){
    vec[i].copyTo(temp.at<Mat>(i));
}

Notice that both methods require the matrices to be the same resolution. Otherwise, you may get undesired results or errors.

Also, notice that you can always use for loops but it is generally not a good idea to use them when you can vectorize.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.