2017-06-19 52 views
0

我有用於運行長度編碼的Matlab代碼,我想編碼解碼。請任何人都可以幫助我製作此代碼的解碼器?我有用於運行長度編碼的matlab代碼,我想爲解碼編碼

的編碼器是如下所示:

function out = rle (image) 
% 
% RLE(IMAGE) produces a vector containing the run-length encoding of 
% IMAGE, which should be a binary image. The image is set out as a long 
% row, and the conde contains the number of zeros, followed by the number 
% of ones, alternating. 
% 
% Example: 
% 
% rle([1 1 1 0 0;0 0 1 1 1;1 1 0 0 0]) 
% 
% ans = 
% 
% 03453 
% 
    level = graythresh(image); 
    BW = im2bw(image, level); 
    L  = prod(size(BW)); 
    im = reshape(BW.', 1, L); 
    x  = 1; 
    out = []; 
    while L ~= 0, 
     temp = min(find(im == x)); 
     if isempty(temp) 
      out = [out, L]; 
      break; 
     end 
     out = [out, temp-1]; 
     x = 1 - x; 
     im = im(temp : L); 
     L = L - temp + 1; 
    end 
end 
+2

避免在您尋求解決方案時提問。而是詢問如何找到解決方案並在實現目標時展示自己的投入。 –

+1

@advise也清楚地格式化代碼是一種重要的技能,當你試圖在代碼中進行交流時(或者至少表明你付出了一些努力來幫助別人閱讀它)。注意這一點! :) –

回答

2

Matlab具有用於遊程長度解碼,即repelem(開始於R2015a)一個內置函數。你用一個包含原始值的載體(在你的情況下爲01)和一個包含遊程長度的向量來餵它。

x = [0 3 4 5 3]爲輸入。然後,

y = repelem(mod(0:numel(x)-1, 2), x) 

給出

y = 
    1  1  1  0  0  0  0  1  1  1  1  1  0  0  0 

其是線性形式的orinal圖像按您的編碼功能。

+1

+1這是一個很好的答案。但我同意Nissim的評論,如果OP在展示這樣一個流暢的解決方案之前表現出了一些努力去理解問題甚至是什麼,那將是非常好的。 –

+0

@TasosPapastylianou完全同意。但答案很簡單::-)當有內置函數時,最好使用它,而不是自己開發(除非用於練習,當然) –

+0

好的,謝謝,我明白了,我會嘗試它 – advise20023

0

這個問題也有一個較短但更復雜的解決方案。您將灰度值和灰度級矩陣的行傳遞給函數RLE,它會給出答案。

function rle = RLE(gray_value, image_rows) 
for i =1:image_rows 
    diF = diff([gray_value(i,1)-1, gray_value(i,:)]) ; 
    k = diff([find(diF), numel(gray_value(i,:))+1]); 
    rle=[gray_value(i,find(diF));k] ; 
end 
end