簡體   English   中英

如何減少 MATLAB 中圖像匹配代碼的運行時間?

[英]How can I reduce runtime in my image matching code in MATLAB?

我正在嘗試使用簡單的圖像匹配算法來穩定 MATLAB 中的視頻文件。 基本上,我將視頻第一幀的 window 與第 n 幀相減。 我想得到一個從第 n 幀的 position 到第一幀的 x 和 y 位移向量數組。 視頻采用 1501x971 灰度格式,391 幀。

下面是我的代碼。 我已經讓代碼運行了 15 分鍾以上並且仍在運行。 我一直在尋找答案並實施了我看到人們建議的 int8 和預分配矩陣解決方案,但它仍然運行時間太長。 任何幫助,將不勝感激。

% Define image region (window)
xmin = 35;
xmax = 1465;
ymin = 35;
ymax = 940;

% Matching algorithm
error = uint16(10^8); % set error to a larger number than expecting in the loop
deltax = 0;
deltay = 0;
deltaxArray = zeros(1,N,'int8');    % prealloacting arrays
deltayArray = zeros(1,N,'int8');    % using int8 to optimize code
deltaxArray(1) = 0;
deltayArray(1) = 0;

for n = 2:N % N = number of frames
    for deltay = -34:31         % Iterating over all possible displacements
        for deltax = -34:36
            current_error = uint16(sum(abs(f(1, ymin+deltay:ymax+deltay , xmin+deltax:xmax+deltax ) - f(n, ymin:ymax, xmin:xmax)),'all'));        % f is the video array
            if current_error < error        % searching for the smallest error in the nth frame
                error = current_error;      % set error if current error is smaller
                deltaxArray(n) = deltax;    % save x displacement coordinate
                deltayArray(n) = deltay;    % save y displacement coordinate
            end
        end
    end
    error = uint16(10^8);   % reset error for next iteration
end

使用分析器。

profile on;
your_script_name;
profile viewer;

這會告訴您哪些代碼行消耗了大部分運行時。

output看起來像這樣

但是通過閱讀您的代碼,您應該考慮通過在矩陣/向量級別上操作而不是使用 for 循環在元素級別上操作來對代碼進行矢量化。 請參閱這篇文章中的教程

逐像素循環以計算圖像熵的更快方法

暫無
暫無

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

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