通过MATLAB实现JBIG-KIT

huangapple go评论70阅读模式
英文:

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:

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 <file name> 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 I will 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.

英文:

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 = &#39;b.pbm&#39;;
name_jbg = &#39;c.jbg&#39;;
if size(x,1)&gt;1 &amp;&amp; size(x,2)&gt;1
% forward transform
% save as pbm
imwrite(rescale(x), name_pbm, &#39;pbm&#39;);
% convert to jgib
!/Users/sahilsharma/Documents/MATLAB/JBIG/pbmtojbg -q b.pbm c.jbg
% read jbig file
fid = fopen(name_jbg); %Here%
if fid&lt;0
    error(&#39;Unable to open Jbig file.&#39;);
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, &#39;wb&#39;);
if fid&lt;0
    error(&#39;Unable to open Jbig file.&#39;);
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 <file name> 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.

答案1

得分: 0

一旦你弄清楚了你的数字,似乎就没有问题。你将一个57 KB的双色图像压缩成14 KB的JBIG压缩图像,完全在预期范围内。

英文:

Once you get your numbers straight, there doesn't seem to be an issue. You are compressing a 57 KB bi-level image to a 14 KB JBIG compression of that image. Well within the realm of expectation.

huangapple
  • 本文由 发表于 2023年2月10日 07:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定