我对 C++ 和 OpenCV 很陌生,但对 Matlab 比较熟悉。我有一项任务需要转移到 C++ 以加快处理速度。所以我想就图像处理问题征求您的建议。我在一个文件夹中有 10 张图片,我可以使用 dirent.h 读取它们,就像在 this 中一样。并通过在 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
中所说的计算s,那么:
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]]
您可以随时阅读 acceptable expressions 的列表.
关于多个图像上的 C++ OpenCV 线性代数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980189/