2017-08-16 84 views
0

尋找最小的像素值,我試着寫一些MATLAB代碼,這樣給單色的視頻,它需要產生這樣的圖像,該圖像的每個像素等於最低值表示像素髮生在視頻。作爲一個例子,像素(200,300)將等於視頻過程中像素(200,300)的最小值。我寫了一些代碼來做到這一點,但效率非常低。以提高我的代碼任何評論都將appriciatedMATLAB:從視頻

hologramVideo = VideoReader('test.mp4') 
mkdir('images') 
frames = int16(hologramVideo.Duration * hologramVideo.FrameRate) 
imageValues = cell(frames, 1); 
ii = 1; 

while hasFrame(hologramVideo) 
    imageValues{ii} = im2uint8(rgb2gray(readFrame(hologramVideo))); 
    ii = ii + 1; 
end 

newImage = zeros(512) 
currentMin = 255 
currentVal = 0 

x = 1; 
y = 1; 

for x = 1:512 
    for y = 1:512 
     currentMin = 0; 
     for i = 1:frames 
      currentImg = imageValues(i,1,1); 
      currentVal = currentImg{1,1}(x,y) 
      if currentVal < currentMin; 
       currentMin = currentVal; 
      end 
     end 
     newImage(x,y) = currentMin; 
    end 
end 

回答

0

我沒有一個文件來測試,但主要的瓶頸是如何存儲圖像。而不是將它們存儲在單元陣列,你是關閉它們存儲在三維陣列更好:

imageValues = zeros([Nframes, 512, 512]); 
ii=1; 
while hasFrame(hologramVideo) 
    imageValues(ii,:,:) = im2uint8(rgb2gray(readFrame(hologramVideo))); 
    ii = ii + 1; 
end 

這將使代碼非常容易和矢量的剩餘部分(即快):

newImage = squeeze(min(imageValues,[],1));