通过MATLAB实现JBIG-KIT

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

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:

  1. function [y,nbr_bits] = perform_jbig_coding(x)
  2. % perform_jbig_coding - perform binary image coding
  3. % [y,nbr_bits] = perform_jbig_coding(x);
  4. % It requires pbmtojbg and jbgtopbm executable.
  5. % Copyright (c) 2006 Gabriel Peyr
  6. name_pbm = 'b.pbm';
  7. name_jbg = 'c.jbg';
  8. if size(x,1)>1 && size(x,2)>1
  9. % forward transform
  10. % save as pbm
  11. imwrite(rescale(x), name_pbm, 'pbm');
  12. % convert to jgib
  13. !/Users/sahilsharma/Documents/MATLAB/JBIG/pbmtojbg -q b.pbm c.jbg
  14. % read jbig file
  15. fid = fopen(name_jbg); %Here%
  16. if fid<0
  17. error('Unable to open Jbig file.');
  18. end
  19. [y,cnt] = fread(fid, Inf);
  20. fclose(fid);
  21. nbr_bits = length(y)*8;
  22. % remove tmp files
  23. !del c.jbg
  24. !del b.pbm
  25. else
  26. % backward transform
  27. fid = fopen(name_jbg, 'wb');
  28. if fid<0
  29. error('Unable to open Jbig file.');
  30. end
  31. fwrite(fid, x);
  32. fclose(fid);
  33. % convert to pbm
  34. !/Users/sahilsharma/Documents/MATLAB/JBIG/jbgtopbm c.jbg b.pbm
  35. % read pbm
  36. y = imread(name_pbm);
  37. % remove tmp files
  38. !del c.jbg
  39. !del b.pbm
  40. nbr_bits = -1;
  41. 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.

  1. function [y,nbr_bits] = perform_jbig_coding(x)
  2. % perform_jbig_coding - perform binary image coding
  3. % [y,nbr_bits] = perform_jbig_coding(x);
  4. % It requires pbmtojbg and jbgtopbm executable.
  5. % Copyright (c) 2006 Gabriel Peyr
  6. name_pbm = &#39;b.pbm&#39;;
  7. name_jbg = &#39;c.jbg&#39;;
  8. if size(x,1)&gt;1 &amp;&amp; size(x,2)&gt;1
  9. % forward transform
  10. % save as pbm
  11. imwrite(rescale(x), name_pbm, &#39;pbm&#39;);
  12. % convert to jgib
  13. !/Users/sahilsharma/Documents/MATLAB/JBIG/pbmtojbg -q b.pbm c.jbg
  14. % read jbig file
  15. fid = fopen(name_jbg); %Here%
  16. if fid&lt;0
  17. error(&#39;Unable to open Jbig file.&#39;);
  18. end
  19. [y,cnt] = fread(fid, Inf);
  20. fclose(fid);
  21. nbr_bits = length(y)*8;
  22. % remove tmp files
  23. !del c.jbg
  24. !del b.pbm
  25. else
  26. % backward transform
  27. fid = fopen(name_jbg, &#39;wb&#39;);
  28. if fid&lt;0
  29. error(&#39;Unable to open Jbig file.&#39;);
  30. end
  31. fwrite(fid, x);
  32. fclose(fid);
  33. % convert to pbm
  34. !/Users/sahilsharma/Documents/MATLAB/JBIG/jbgtopbm c.jbg b.pbm
  35. % read pbm
  36. y = imread(name_pbm);
  37. % remove tmp files
  38. !del c.jbg
  39. !del b.pbm
  40. nbr_bits = -1;
  41. 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:

确定