【问题标题】:C++ OpenCV linear algebra on multiple images?多个图像上的 C++ OpenCV 线性代数?
【发布时间】:2016-06-23 00:07:10
【问题描述】:

我对 C++ 和 OpenCV 非常陌生,但对 Matlab 更熟悉。我有一项任务需要转移到 C++ 以加快处理速度。所以我想请教您对图像处理问题的建议。我在一个文件夹中有 10 张图像,我可以像在 this 中一样使用 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)。该怎么做?

【问题讨论】:

    标签: c++ matlab opencv


    【解决方案1】:

    由于framesstd::vector&lt;cv::Mat&gt;,您应该可以通过这种方式访问​​每个Mat

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

    现在,如果你想对前两个Mats 进行计算,那么:

    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的列表。

    【讨论】:

    • 非常感谢贝瑞尔
    猜你喜欢
    • 2019-01-25
    • 2020-07-23
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多