2023-02-12

Implementation of JBIG-KIT Via MATLAB

I am trying to implement the JBIG compression for some images. I just want to know the compression ratio achieved by the algorithm. For this, I am using JBIG-KIT by Markus K.

https://www.cl.cam.ac.uk/~mgk25/jbigkit/ Also, there is a MATLAB implementation available that I am using the code pasted below: Can you please tell me the questions regarding the following MATLAB code? It is code from the wavelet toolbox with paths and commands added for the JBIG-KIT's executables.

function [y,nbr_bits] = perform_jbig_coding(x)
% perform_jbig_coding - perform binary image coding
%  [y,nbr_bits] = perform_jbig_coding(x);
%  It requires pbmtojbg and jbgtopbm executable.
%  Copyright (c) 2006 Gabriel Peyr
name_pbm = 'b.pbm';
name_jbg = 'c.jbg';
if size(x,1)>1 && size(x,2)>1
% forward transform
% save as pbm
imwrite(rescale(x), name_pbm, 'pbm');
% convert to jgib
!/Users/sahilsharma/Documents/MATLAB/JBIG/pbmtojbg -q b.pbm c.jbg
% read jbig file
fid = fopen(name_jbg); %Here%
if fid<0
    error('Unable to open Jbig file.');
end
[y,cnt] = fread(fid, Inf);
fclose(fid);
nbr_bits = length(y)*8;
% remove tmp files
!del c.jbg
!del b.pbm
else
% backward transform
fid = fopen(name_jbg, 'wb');
if fid<0
    error('Unable to open Jbig file.');
end
fwrite(fid, x);
fclose(fid);
% convert to pbm
!/Users/sahilsharma/Documents/MATLAB/JBIG/jbgtopbm c.jbg b.pbm
% read pbm
y = imread(name_pbm);
% remove tmp files
!del c.jbg
!del b.pbm
nbr_bits = -1;
end

I have added the path here to run my code. It is working now. However I have two doubts,

  1. !del command is not working, MATLAB is telling that "Command not found:del". So I thought that "rm" might work here. However, that is also not working, if you have any idea how will I be able to delete those files, please do answer.
  1. [y,cnt] = fread(fid, Inf); (I have commented %Here% in code), am I getting encoded values here? Cause I need to find the compression ratio achieved by JBIG. JBIG uses context-based arithmetic encoding. So I wanted to know if the [y,cnt] reads the encoded data. Through this, I would directly be able to get CR as I know the original size.

'x' is a binary image, currently, I am using an image of size 740x628 (size(x) = [740 628]). cnt = 115392 and y= 14424x1 double. I wanted to have a confirmation about 'y', that if it is the encoded image. If it is then my Compression Ratio becomes (740*628)/115392. The operating system that I am using is macOS.

Oh very sorry 115392 is the value of 'nbr_bits' and 'cnt' = 14424.



No comments:

Post a Comment