LZW compression using easy method(Dictionary-based coding)in DIP & its Implementation in MATLAB

Поделиться
HTML-код
  • Опубликовано: 13 дек 2024

Комментарии • 20

  • @pravinbhoi3121
    @pravinbhoi3121 2 года назад

    The best channel, and the only one, that covers both theoretical as well as practical concepts in such a easy way. Thank you.

  • @tahseenali5609
    @tahseenali5609 Год назад

    Dear Sir, I am very very thank full to you. I entered the code and successfully run. I am very happy thanks a lotttttttt.

  • @tahseenali5609
    @tahseenali5609 Год назад

    Very very very nice Sir.

    • @tahseenali5609
      @tahseenali5609 Год назад

      Sir i sm doing PhD in Telecom. What is your research field....

    • @StudywithDrDafda
      @StudywithDrDafda  Год назад

      I have done my PhD on Satellite Communications.

    • @tahseenali5609
      @tahseenali5609 Год назад

      @@StudywithDrDafda I am doin research... Resource allocation in 5g and 6g...can you help me Sir in this regard?

    • @StudywithDrDafda
      @StudywithDrDafda  Год назад

      Yes, you can mail me your queries on my email doctordafda@gmail.com

  • @kartikforwork
    @kartikforwork 6 месяцев назад

    why are we putting last char value in the encoded output?
    like at 7:28, the 7 output value has fda in dictonary, so decoding it will give fda directly, so why 1 in the end

    • @StudywithDrDafda
      @StudywithDrDafda  6 месяцев назад

      At 7:28, the 7 output value has FD in the dictionary, and not FDA. Check dictionary location number 7, it is FD and therefore always last character is required for correct decoding.

  • @yousefeldewik3027
    @yousefeldewik3027 Год назад

    if I want to do this but instead of Image compression, voice compression and calculate the equivalent probability of each symbol what will be the code
    could you provide me with some help

  • @tuananhvu5398
    @tuananhvu5398 2 года назад

    Can you sum up all the code, I'm new so I don't know how to arrange it properly
    Thank you

  • @StudywithDrDafda
    @StudywithDrDafda  2 года назад

    function [output,table, Alphabet] = LZW_encoder_string(vector)
    % LZW Data Compression for string (encoder)
    % For vectors, LZW_encoder_string(X) is the compressed vector
    % of X using the LZW algorithm.
    % [...,T] = LZW_encoder_string(X) returns also the table
    % that the algorithm produces.
    % For matrices, X(:) is used as fprintf.
    % fprintf must be of uint8 type, while the output is a uint16.
    %Table is a cell array, each element_k containing the resp.code.
    % How it encodes:
    % STRING = get fprintf character
    % WHILE there are still fprintf characters DO
    % CHARACTER = get fprintf character
    % IF STRING+CHARACTER is in the string table then
    % STRING = STRING+character
    % ELSE
    % output the code for STRING
    % add STRING+CHARACTER to the string table
    % STRING = CHARACTER
    % END of IF
    % END of WHILE
    % output the code for STRING
    % ensure to handle uint8 fprintf vector
    if ~isa(vector,'uint8'),
    error('fprintf argument must be a uint8 vector')
    end
    % vector as uint16 row
    vector = uint16(vector(:)');
    %initialize table (don't use cellstr because char(10) will be turned to empty!!!)
    fprintf('Initialising Table
    ');
    table = [];
    Alphabet = unique(vector);
    for i = 1:numel(Alphabet)
    [table, code] = addcode(table, (double(Alphabet(i))));
    fprintf('Table Entry %d : %s
    ', i, char(Alphabet(i)));
    end
    fprintf('
    ');
    % initialize output
    output = vector;
    % main loop
    outputindex = 1;
    startindex = 1;
    element_k = vector(1);
    substr_w = [];
    fprintf('w k output index symbol
    ');
    fprintf('------------------------------------
    ');
    fprintf('NIL\t\t%s
    ', char(element_k));

    for index=2:length(vector),
    element_k = vector(index);
    substr_w = vector(startindex:(index-1));
    code = getcodefor([substr_w element_k],table);

    if isempty(code),
    % add it to the table
    output(outputindex) = getcodefor(substr_w,table);
    [table,code] = addcode(table,[substr_w element_k]);
    fprintf('%s\t\t%s\t\t%d\t\t%d\t\t%s
    ', ...
    char(substr_w), char(element_k), output(outputindex), code,...
    char([substr_w element_k]));
    outputindex = outputindex+1;
    startindex = index;
    else
    fprintf('%s\t\t%s
    ', char(substr_w), char(element_k));
    end
    end
    substr_w = vector(startindex:index);
    output(outputindex) = getcodefor(substr_w,table);
    % remove not used positions
    output((outputindex+1):end) = [];
    % ###############################################
    function code = getcodefor(substr,table)
    code = uint16([]);
    for index=1:length(table),
    if isequal(substr,table{index}),
    code = uint16(index); % start from 0
    break
    end
    end
    % ###############################################
    function [table,code] = addcode(table,substr_w)
    code = length(table)+1; % start from 1
    table{code} = substr_w;
    code = uint16(code); % start from 1

  • @doaajawad2379
    @doaajawad2379 2 года назад

    When I run this program in maylab there are,an error ??

    • @StudywithDrDafda
      @StudywithDrDafda  2 года назад

      First, please save the functions given in above comments in MATLAB folder, then run the programs.

  • @doaajawad2379
    @doaajawad2379 2 года назад

    LZW_encoder string ;error in this step

    • @StudywithDrDafda
      @StudywithDrDafda  2 года назад

      First, please save the functions given in above comments in MATLAB folder, then run the programs.

  • @StudywithDrDafda
    @StudywithDrDafda  2 года назад

    function [output,table] = LZW_decoder_string(vector, alphabet)
    % LZW Data Compression for string(decoder)
    % For vectors, LZW_decoder_string(X) is the uncompressed
    % vector of X using the LZW algorithm.
    % [...,T] = LZW_decoder_string(X) returns also the table
    % that the algorithm produces.
    % For matrices, X(:) is used as input.
    % Input must be of uint16 type, while the output_w is a uint8.
    %Table is a cell array, each element_k containing the resp.code.
    % How it decodes:
    % Read OLD_CODE
    % output_w OLD_CODE
    % CHARACTER = OLD_CODE
    % WHILE there are still input characters DO
    % Read NEW_CODE
    % IF NEW_CODE is not in the translation table THEN
    % entry = get translation of OLD_CODE
    % entry = entry+CHARACTER
    % ELSE
    % entry = get translation of NEW_CODE
    % END of IF
    % output_w entry
    % CHARACTER = first character in entry
    %add translation of OLD_CODE + CHARACTER to the translation table
    % OLD_CODE = NEW_CODE
    % END of WHILE
    % ensure to handle uint8 input vector
    if ~isa(vector,'uint16'),
    error('input argument must be a uint16 vector')
    end
    % vector as a row
    vector = vector(:)';
    %Intialise Table
    fprintf('Initialising Table
    ');
    table = [];
    % Alphabet = unique(vector);
    for i = 1:numel(alphabet)
    [table,code] = addcode(table, uint16(double(i)));
    end
    for index = 1:numel(table),
    fprintf('Table Entry %d : %s
    ',index, alphabet(table{index}));
    end
    % initialize output_w
    output = uint8([]);
    code = vector(1);
    output(end+1) = table{code};
    character = code;
    fprintf('Input + code_w k (w=k): %d
    ', code);
    fprintf('w k output index symbol
    ');
    fprintf('------------------------------------
    ');
    fprintf('NIL\t\t%s
    ', alphabet(code));
    for index=2:length(vector),
    element_k = vector(index);
    %k_pos = getcodefor(element_k,table)
    % fprintf('Input k: %d
    ', code);
    % element_k
    if (element_k)>length(table),
    % add it to the table
    entry = table{double(code)};
    entry = [entry character];
    else
    entry = element_k;
    end

    if code > numel(alphabet)
    ow = num2str(code);
    else
    ow = alphabet(code);
    end

    if element_k > numel(alphabet)
    ok = num2str(element_k);
    else
    ok = alphabet(element_k);
    end
    character = table{entry};
    [table,code] = addcode(table,[table{code} character(1)]);
    fprintf('%s\t\t%s\t\t%s\t\t%d\t\t%s', ow, ok, char(alphabet(table{entry})), ...
    code, alphabet([table{code}]));
    warning off
    output = [output table{entry}];
    warning on
    code = element_k;
    end
    % ###############################################
    function code = getcodefor(substr,table)
    code = uint16([]);
    for index=1:length(table),
    if isequal(substr,table{index}),
    code = uint16(index); % start from 0
    break
    end
    end
    % ###############################################
    function [table,code] = addcode(table,substr)
    code = length(table)+1; % start from 1
    table{code} = substr;
    code = uint16(code); % start from 1

  • @StudywithDrDafda
    @StudywithDrDafda  2 года назад

    function [output,table] = LZW_decoder_image(vector, alphabet)
    % LZW Data Compression (decoder)
    % For vectors, LZW_decoder_image(X) is the uncompressed
    % vector of X using the LZW algorithm.
    % [...,T] = LZW_decoder_image(X) returns also the table
    % that the algorithm produces.
    % For matrices, X(:) is used as input.
    % Input must be of uint16 type, while the output_w is a uint8.
    %Table is a cell array, each element_k containing the resp. code.
    % How it decodes:
    % Read OLD_CODE
    % output_w OLD_CODE
    % CHARACTER = OLD_CODE
    % WHILE there are still input characters DO
    % Read NEW_CODE
    % IF NEW_CODE is not in the translation table THEN
    % entry = get translation of OLD_CODE
    % entry = entry+CHARACTER
    % ELSE
    % entry = get translation of NEW_CODE
    % END of IF
    % output_w entry
    % CHARACTER = first character in entry
    %add translation of OLD_CODE + CHARACTER to the translation table
    % OLD_CODE = NEW_CODE
    % END of WHILE
    % ensure to handle uint8 input vector
    if ~isa(vector,'uint16'),
    error('input argument must be a uint16 vector')
    end
    % vector as a row
    vector = vector(:)';
    %Intialise Table
    fprintf('Initialising Table
    ');
    table = [];
    % Alphabet = unique(vector);
    for i = 1:numel(alphabet)
    [table,code] = addcode(table, uint16(double(i)));
    end
    for index = 1:numel(table),
    fprintf('Table Entry %d: %d
    ',index, alphabet(table{index}));
    end
    fprintf('
    ');
    % initialize output_w
    output = uint8([]);
    code = vector(1);
    output(end+1) = table{code};
    character = code;
    fprintf('Input + code_w k (w=k): %d
    ', code);
    fprintf('w k output index symbol
    ');
    fprintf('------------------------------------
    ');
    fprintf('NIL\t\t%d
    ', alphabet(code));

    for index=2:length(vector),
    element_k = vector(index);
    %k_pos = getcodefor(element_k,table)
    % fprintf('Input k: %d
    ', code);
    % element_k
    if (element_k)>length(table),
    % add it to the table
    entry = table{double(code)};
    entry = [entry character];
    else
    entry = element_k;
    end

    if code > numel(alphabet)
    ow = num2str(code);
    else
    ow = alphabet(code);
    end

    if element_k > numel(alphabet)
    ok = num2str(element_k);
    else
    ok = alphabet(element_k);
    end


    character = table{entry};
    [table,code] = addcode(table,[table{code} character(1)]);
    fprintf('%d\t\t%d\t\t%d\t\t%d\t\t%d
    ',ow,ok,char(alphabet(table{entry})),...
    code, alphabet([table{code}]));
    warning off
    output = [output table{entry}];
    warning on
    code = element_k;
    end
    % ###############################################
    function code = getcodefor(substr,table)
    code = uint16([]);
    for index=1:length(table),
    if isequal(substr,table{index}),
    code = uint16(index); % start from 0
    break
    end
    end
    % ###############################################
    function [table,code] = addcode(table,substr)
    code = length(table)+1; % start from 1
    table{code} = substr;
    code = uint16(code); % start from 1

  • @StudywithDrDafda
    @StudywithDrDafda  2 года назад

    function [output,table, Alphabet] = LZW_encoder_image(vector)
    % LZW Data Compression (encoder)
    % For vectors, LZW_encoder_image(X) is the compressed vector
    % of X using the LZW algorithm.
    % [...,T] = LZW_encoder_image(X) returns also the table
    % that the algorithm produces.
    % For matrices, X(:) is used as fprintf.
    % fprintf must be of uint8 type, while the output is a uint16.
    % Table is a cell array, each element_k containing resp.code.
    % How it encodes:
    % STRING = get fprintf character
    % WHILE there are still fprintf characters DO
    % CHARACTER = get fprintf character
    % IF STRING+CHARACTER is in the string table then
    % STRING = STRING+character
    % ELSE
    % output the code for STRING
    % add STRING+CHARACTER to the string table
    % STRING = CHARACTER
    % END of IF
    % END of WHILE
    % output the code for STRING
    % ensure to handle uint8 fprintf vector
    if ~isa(vector,'uint8'),
    error('fprintf argument must be a uint8 vector')
    end
    % vector as uint16 row
    vector = uint16(vector(:)');
    % initialize table (don't use cellstr because char(10)
    % will be turned to empty!!!)
    fprintf('Initialising Table
    ');
    table = [];
    Alphabet = unique(vector);
    for i = 1:numel(Alphabet)
    [table, code] = addcode(table, (double(Alphabet(i))));
    fprintf('Table Entry %d : %d
    ', i, char(Alphabet(i)));
    end
    fprintf('
    ');
    % initialize output
    output = vector;
    % main loop
    outputindex = 1;
    startindex = 1;
    element_k = vector(1);
    substr_w = [];
    fprintf('w k output index symbol
    ');
    fprintf('------------------------------------
    ');
    fprintf('NIL\t\t%d
    ', char(element_k));

    for index=2:length(vector)
    element_k = vector(index);
    substr_w = vector(startindex:(index-1));
    code = getcodefor([substr_w element_k],table);

    if isempty(code)
    % add it to the table
    output(outputindex) = getcodefor(substr_w,table);
    [table,code] = addcode(table,[substr_w element_k]);
    fprintf('%d\t\t%d\t\t%d\t\t%d\t\t%d
    ',...
    char(substr_w), char(element_k), output(outputindex), code, ...
    char([substr_w element_k]));
    outputindex = outputindex+1;
    startindex = index;
    else
    % go on looping
    fprintf('%d\t\t%d
    ', char(substr_w), char(element_k));
    end
    end
    substr_w = vector(startindex:index);
    output(outputindex) = getcodefor(substr_w,table);
    % remove not used positions
    output((outputindex+1):end) = [];
    % ###############################################
    function code = getcodefor(substr,table)
    code = uint16([]);
    for index=1:length(table),
    if isequal(substr,table{index}),
    code = uint16(index); % start from 0
    break
    end
    end
    % ###############################################
    function [table,code] = addcode(table,substr_w)
    code = length(table)+1; % start from 1
    table{code} = substr_w;
    code = uint16(code); % start from 1