簡體   English   中英

多個圖像上的C ++ OpenCV線性代數?

[英]C++ OpenCV linear algebra on multiple images?

我是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) 怎么做?

由於framesstd::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]]

您始終可以閱讀可接受的表達式列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM