hlambert24
hlambert24

Reputation: 77

C++ OpenCV linear algebra on multiple images?

I am very new to C++ and OpenCV but more familiar with Matlab. I have a task that I need to move to C++ for faster processing. So I would like to ask for your suggestion on a image processing problem. I have 10 images in a folder and I was able to read them all using dirent.h like in this and extract each frame by calling frame[count] = rawImage in a while loop:

int count = 0;
std::vector<cv::Mat> frames;
frames.resize(10);
while((_dirent = readdir(directory)) != NULL)
{
    std::string fileName = inputDirectory + "\\" +std::string(_dirent->d_name);
    cv::Mat rawImage = cv::imread(fileName.c_str(),CV_LOAD_IMAGE_GRAYSCALE);
    frames[count] = rawImage; // Insert the rawImage to frames (this is original images)
    count++;
}

Now I want to access each frames and do calculation similar to Matlab to get another matrix A such that A = frames(:,:,1)+2*frames(:,:,2). How to do that?

Upvotes: 0

Views: 239

Answers (1)

Berriel
Berriel

Reputation: 13641

Since frames is a std::vector<cv::Mat>, you should be able to access each Mat this way:

// suppose you want the nth matrix
cv::Mat frame_n = frames[n];

Now, if you want to do the calculation you said on the first two Mats, then:

cv::Mat A = frames[0] + 2 * frames[1];

Example:

// mat1 = [[1 1 1]
//         [2 2 2]
//         [3 3 3]]
cv::Mat mat1 = (cv::Mat_<double>(3, 3) << 1, 1, 1, 2, 2, 2, 3, 3, 3);
cv::Mat mat2 = mat1 * 2; // multiplication matrix x scalar

// just to look like your case
std::vector<cv::Mat> frames;
frames.push_back(mat1);
frames.push_back(mat2);

cv::Mat A = frames[0] + 2 * frames[1]; // your calculation works
// A = [[ 5  5  5]
//      [10 10 10]
//      [15 15 15]]

You can always read the list of acceptable expressions.

Upvotes: 1

Related Questions