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

标签 c++ matlab opencv

我对 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)。如何做到这一点?

最佳答案

framesstd::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/

相关文章:

c++ - 二维 vector 大小的 Visual Studio 2015 断点条件

c++ - 是否需要注意非错误提示?好像找不到包 ‘***’?

c++ - 如何调用带参数指针的函数指针?

C++ 函数参数数据类型没有名称

c++ - 从数学函数到 C++ 代码

python - 使用opencv在python中以灰度显示相机供稿

iphone - HoughCircles 给出了错误的圈数和位置 - iOS

function - 在不添加该路径的情况下调用不在 Matlab 路径上的函数

MATLAB:varargin 的行为与 'real' 元胞数组不同?

encryption - 如何在图像中封装一些文本信息并使用 MATLAB 将其提取出来?