2014-01-22 94 views
0

我正在使用matlab進行視頻鏡頭邊界檢測。這裏是我的代碼片段形成一個64×64矩陣的巨陣列

clc; 
clear all; 

[file path] = uigetfile('*.avi','Pick a Video');% this line opens a pop up window so that you can choose your video for further processing  
video = VideoReader([path file]);% the video chosen through UI get stored in video object 
numOfFrames = video.NumberOfFrames;% this will be a number (total number of frames in your chosen video) 
%allFrames = read(video);% catching all the frames from the video in object allFrames 
for i=1:1:numOfFrames 
    try 
     lFrame = read(video, i);%frames(:, :, :, i); 
    catch 
     break; 
    end 
    lRFrame = lFrame(:,:,1); 
    lGFrame = lFrame(:,:,2); 
    lBFrame = lFrame(:,:,3); 
    lGray = 0.299*lRFrame + 0.587*lGFrame + 0.114*lBFrame; 
    grayImage = imresize(lGray, [256 256]); 

    % step 2 of the algorithm begins from here % 
    meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(1,1, class(theBlockStructure.data)); 

    blockSize = [4 4]; 
    blockyImage4 = blockproc(single(grayImage), blockSize, meanFilterFunction); 
end 

我得到一個64×64圖像中blockyImage4但因爲它是在一個循環中,每次刷新值本身很下一次迭代。

我想要一個可以存儲(可能是所有)圖像(矩陣)的圖像數組。我應該可以訪問它們。如果所有圖像都不能存儲在數組中,那麼如何只存儲連續迭代的兩幅圖像,因爲我需要「i」和「i + 1」圖像來找出它們之間的差異。請幫忙

+0

我不確定是否理解了這個問題,但'blockyImage4(:,:,i)= ...'會有幫助嗎? – Adiel

+0

你需要RGB中的所有圖像嗎?也許你可以將幀轉換爲灰度(將它保存在數組中會更容易)。基本上,你可以把它保存爲一個單元格陣列,例如,每個單元格保存關於每一幀的信息(在Matlab中單元格可能非常複雜) – hesar

+1

@hesar現在已經有一個灰度轉換。 – Trilarion

回答

0

想要在Matlab中存儲3D(x,y,time)數據嗎?無論是在單元陣列還是3D陣列中。對於陣列,不要忘記預先分配所需的大小以避免性能下降。

因此,要麼:

blockyimages = cell(numOfFrames, 1); 
for i=1:1:numOfFrames 
    ... 
    blockyimages{i} = blockproc(single(grayImage), blockSize, meanFilterFunction); 
end 
% access the blocky image of the ith frame with blockyimages{i} 
% you could even do blockyimages = cat(3, blockyimages{:}) to convert to a 3d matrix 

blockyimages = zeros(64, 64, numOfFrames); % better frame sizes/block sizes 
for i=1:1:numOfFrames 
    ... 
    blockyimages(:, :, i) = blockproc(single(grayImage), blockSize, meanFilterFunction); 
end 
% access the blocky image of the ith frame with blockyimages(:, :, i) 

所以,如果你選擇了第二個選項,以後要再做一次與blockyimages的東西,只是讓通過循環第二遍。例如,如果你想計算連續blockyimages的差異。

diffblockyimages = zeros(64, 64, numOfFrames - 1); 
for i = 1 : numOfFrames - 1 
    framei = blockyimages(:, :, i); 
    frameiplusone = blockyimages(:, :, i+1); 
    diffblockyimages(:, :, i) = frameiplusone - framei; 
end 

雖然同樣可以在差別的特殊情況下,已經實現也爲

diffblockyimages = diff(blockyimages, 1, 3); 
+0

blockyimages {i + 1}不工作....當我們在一個循環中如何訪問下一幀 – user3184410

+0

@ user3184410我從來沒有寫過blockyimages {i + 1}?在一個循環中,通過索引訪問幀.. – Trilarion

+0

然後如何訪問循環中的下一個(第i + 1)圖像..plz幫助我是一個新的蜜蜂matlab – user3184410

0

由於我的評論被阻止我提出可能的解決方案:
測試,對我來說

工作正常
file_path = uigetfile('*.avi','Pick a Video');% this line opens a pop up window so that you can choose your video for further processing  
video = VideoReader(file_path);% the video chosen through UI get stored in video object 
numOfFrames = video.NumberOfFrames;% this will be a number (total number of frames in your chosen video) 
%allFrames = read(video);% catching all the frames from the video in object allFrames 

all_frames = cell(1,numOfFrames); 
for i=1:numOfFrames %default step is always 1 
    try 
     lFrame = read(video, i);%frames(:, :, :, i); 
    catch 
     break; 
    end 
    lGray = rgb2gray(lFrame); 
    grayImage = imresize(lGray, [256 256]); 
all_frames{1,i} = grayImage; 

end 

現在你有all_frames數組中的所有幀 訪問很簡單:

img1 = all_frames{1,1}; imshow(img1); 
img2 = all_frames{1,2}; imshow(img2); 
+0

@ hesar感謝您的回覆,但是您的代碼並沒有解決兩件事:1.將256x256劃分爲4x4塊並採取均值,將每幅圖像轉換爲64 x 64塊。另外我想要做的是僅在兩個連續幀(i和i + 1)之間進行差異化處理,這樣纔有可能將第一個和第二個圖像存儲到數組中並取出數組。然後存儲第3個和第4個圖像取其均值並再次清空數組 – user3184410

+0

@ user3184410您可以以同樣的方式執行此操作,您可以將所有內容都放置爲單元格或使用更多維度的單元格數組來存儲更多信息。例如all_frames = cell(3,numOfFrames),其中有3行分別保留:分割塊的單元,塊的均值,差值。所有的值都可以在for循環內計算並保存到單元格數組中。請記住跳過循環的第一次迭代,因爲您無法計算差異。 img1和img2可以是臨時值,不會將它們保存在內存中。這只是使用單元格數組來存儲結果的例子 – hesar