英文:
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,
!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.
[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 = '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,
- !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.
- [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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论