C++ 多个图像上的 OpenCV 线性代数?

C++ OpenCV linear algebra on multiple images?

本文关键字:OpenCV 线性代数 图像 C++      更新时间:2023-10-16

我对C++和OpenCV很陌生,但对Matlab更熟悉。我有一个任务需要移动到C++以加快处理速度。因此,我想就图像处理问题征求您的建议。我在一个文件夹中有 10 张图像,我能够像这样使用 dirent.h 读取它们,并通过在 while 循环中调用frame[count] = rawImage来提取每一帧:

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++;
}

现在我想访问每个帧并进行类似于 Matlab 的计算,以获得另一个矩阵A,以便A = frames(:,:,1)+2*frames(:,:,2).怎么做?

由于frames是一个std::vector<cv::Mat>,您应该能够通过以下方式访问每个Mat

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

现在,如果您想进行您在前两个Mat上所说的计算,那么:

cv::Mat A = frames[0] + 2 * frames[1];
<小时 />

示例:

// 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]]

您始终可以阅读可接受的表达式列表。