您的位置:首页 > 编程语言 > MATLAB

Matlab调用Java版JPEG2000图像压缩工具:jj2000-4.1

2015-04-01 23:21 453 查看
Matlab调用Java版JPEG2000图像压缩工具:jj2000-4.1

0. 简介

0.1. JPEG与JPEG2000

大家知道JPEG是基于离散余弦变换(DCT)的图像压缩标准,而JPEG2000是基于小波变换(DWT)的图像压缩标准,当然不同的还有编码算法。JPEG与JPEG2000及其扩展由Joint
Photographic Experts Group组织创建和维护,更多信息参见维基百科:JPEG2000,或者JPEG官网:http://www.jpeg.org/,里面提供了C++、C、Java等语言的实现,请根据需要自行下载。

0.2. jj2000简介

JJ2000实现的是JPEG2000标准中的part1部分,纯由Java语言写成,项目官网:http://jpeg2000.epfl.ch/,但该网址本人试图访问多次,未果;在JPEG官网也提供了下载链接:https://code.google.com/p/jj2000/,不过版本号是5.1,此外Google
Code明年将关闭服务,本人将其导出到了GitHub下载链接点我。对于文章使用的版本下载,待会介绍。

1. 工具

硬件:PC机

软件:Matlab、Jpeg2000Gui、Java虚拟机、jj2000-4.1

Note:软件工具下载链接请百度或谷歌,当然,Jpeg2000Gui、jj2000-4.1、以及Google基于jj2000-5.1修改的的jj2000-master,也可以点此下载

             Jpeg2000Gui 是Nikola Sprljan 写的一个调用jj2000-4.1.jar工具包的 matlab GUI 程序,为方便使用,本人进行了一定的修改。

2. 步骤与方法

2.0 安装软件工具

没有安装MATLAB 和 Java虚拟机的,请自行安装。Java虚拟机根据自己需要选择,有:Microsoft JVMSun‘s Java等等,’其环境变量的配置,见2.1软件配置。

Jpeg2000Gui 和 jj2000-4.1无需安装,解压即可。

2.1 软件配置

2.1.1 Java虚拟机的环境变量配置

按照提示安装好Java虚拟机后,还需要配置环境变量,按照下述流程打开环境变量设置页:

控制面板(Control Panel)——> 系统(system)——>  高级系统设置(Advanced system settings)——> 高级(Advanced)——> 环境变量(Environment Variables)



图2-1 环境变量设置界面
如上图所示,包含两类环境变量:用户环境变量 和 系统环境变量。我们需要在用户环境变量中添加
JAVA_HOME 环境变量,在系统变量里添加 CLASSPATH 环境变量 和 新建(如果没有的话)或修改(如果有的话)PATH环境变量。它们的值如下面绿色部分:

JAVA_HOME=JavaPath(JavaPath是Java的安装路径,如E:\Program
Files\Java\jdk1.7.0_51)


PATH=.;%JAVA_HOME%\bin(等同于E:\Program
Files\Java\jdk1.7.0_51\bin)

CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
下面,给出图解

参照上图2-1,点击“用户变量(User Variables for ... )” 区域的“新建(New)”,在弹出窗口中,按下图输入, 点击OK,创建JAVA_HOME环境变量,注意你的JAVA安装路径。



图2-2 JAVA_HOME 环境变量
参照上图2-1,点击“系统环境变量(System variables)” 区域的“新建(New)”,在弹出窗口中,输入CLASSPATH环境变量名,及其对应的值,点击OK创建。

参照上图2-1,找到“系统环境变量(System variables)” 区域PATH环境变量,点击“编辑(Edit)”,在变量值前端输入PATH的值,如下图2-3所示:



图2-3 给Path环境变量添加值
这样环境变量就配置好了。

测试:为了测试环境变量配置是否成功,在运行里输入cmd,打开DOS窗口,输入:Java -version,若输出下图所示信息,则表明配置成功。



图2-4 Java 的环境变量配置成功

2.1.2 jj2000-4.1.jar工具包的环境变量配置

在jj2000-4.1文件夹中找到jj2000-4.1.jar (没有的话自行编译),并将其复制到要存放的路径下。

jj2000-4.1.jar是一个Java类包,为了能够在任意路径下自由调用,将其路径添加进上面建立的CLASSPATH环境变量里去。假设jj2000-4.1.jar的路径是:

D:\Workspace\Matlab\DIP\jpeg2000\Jpeg2000Gui\jj2000-4.1.jar,那么,就把它添加进CLASSPATH环境变量的值里面,用;
隔开,这样就添加好了。

测试:为了测试环境变量配置是否成功,在运行里输入cmd,打开DOS窗口,输入:Java JJ2KEncoder -u,若输出下图所示信息,则表明配置成功,此命令是查看 JJ2KEncoder 的使用帮助。



图2-5 JJ2KEncoder 参数选项

2.2 jj2000-4.1.jar 的使用

找到jj2000中的 README 文件,使用写字板打开,在里面可以找到,jj2000-4.1工具的调用方法。下面进行简要介绍:

Windows下,使用命令添加环境变量:set CLASSPATH=<jjdir>/jj2000-4.1.jar;%CLASSPATH%,其中,<jjdir>,为jj2000-4.1.jar 所在文件夹路径。

本文使用的是Sun的JDK,编解码命令如下,其它虚拟机请参见 README 文件。

2.2.1 DOS下执行编解码

Windows下,编码命令:

                                          java JJ2KEncoder <args>

                         解码命令:

                                          java JJ2KDecoder <args>

其中,<args>,为可选参数,使用Java JJ2KEncoder -u 可以查看具体参数选项,如-i
是输入文件参数,-o 是输出文件参数,-rate 用以指定压缩比。

如下命令,以“pict.pgm”文件为输入,进行8倍的压缩编码,输出文件为“pict_JJ2.j2k”。

Java JJ2KEncoder -i pict.pgm -o pict_JJ2.j2k -rate 1




2.2.2 Matlab下通过DOS执行

如果在Matlab下调用,可以使用Matlab自带的 dos 函数,将命令加载到DOS运行,格式如下,其中,cmdstr为命令字符串。

[s,w] = dos(cmdstr)


2.2.3 Matlab下直接执行Java命令

如果想直接在Matlab中使用Java命令,需要在Java命令前加感叹号 ! ,如在Matlab命令窗口,输入:!Java
-version,则输出,下图所示信息:



图2-6 在Matlab中查看 Java 版本信息
同样,如果想运行 JJ2KEncoder 命令,可以使用:

!Java JJ2KEncoder -i pict.pgm -o pict_JJ2.j2k -rate 1


2.2.4 Jpeg2000Gui 运行结果示例

Jpeg2000Gui 实现了在Matlab下的调用,请参考之,下面给出本人修改后的一个运行结果图。



图2-7 jpeg2000Gui 运行结果示例
图中实现的是 16 倍的压缩,PSNR在23.0dB,还是很高的!

3. Note

1. 如果出现Matlab调用不成功,而在DOS下调用成功的情况,请注销或重启系统。

2. 在dos下 输入:Java JJ2KEncoder -u >>C:\help.txt,可以将结果输出到help.txt文件。

4. 附录

4.1 JJ2KEncoder参数选项

Usage:
JJ2KEncoder args...

The exit code of the encoder is non-zero if an error occurs.

Note: Many encoder modules accept tile-component specific parameters. These
parameters must be provided according to the pattern:
"[<tile-component idx>] <param>" (repeated as many time as needed).

<tile-component idx> respect the following policy according to the degree
of priority:
(1) t<idx> c<idx> : Tile-component specification.
(2) t<idx> : Tile specification.
(3) c<idx> : Component specification
(4) <void> : Default specification.

Where the priorities of the specifications are:
(1) > (2) > (3) > (4), ('>' means "overrides")

<idx>: ',' separates indexes, '-' separates bounds of indexes list. (ex:
0,2-4 means indexes 0,2,3 and 4).

The following arguments are recognized:
-u [on|off] (default = off)
Prints usage information. If specified all other arguments (except 'v')
are ignored
-v [on|off] (default = off)
Prints version and copyright information.
-verbose (default = on)
Prints information about the obtained bit stream.
-o <file name>
Mandatory argument. This option specifies the name of the output file
to which the codestream will be written.
-i <image file> [<image file> [<image file> ... ]]
Mandatory argument. This option specifies the name of the input image
files. Supported formats are PGM (raw), PPM (raw) and PGX, which is a
simple extension of the PGM file format for single component data
supporting arbitrary bitdepths. If the extension is '.pgm', PGM-raw
file format is assumed, if the extension is '.ppm', PPM-raw file format
is assumed, otherwise PGX file format is assumed. PGM and PPM files are
assumed to be 8 bits deep. A multi-component image can be specified by
either specifying several PPM and/or PGX files, or by specifying one
PPM file.
-lossless [on|off] (default = off)
Specifies a lossless compression for the encoder. This options is
equivalent to use reversible quantization ('-Qtype reversible') and 5x3
wavelet filters pair ('-Ffilters w5x3'). Note that this option cannot
be used with '-rate'. When this option is off, the quantization type
and the filters pair is defined by '-Qtype' and '-Ffilters'
respectively.
-rate <output bitrate in bpp> (default = 100)
This is the output bitrate in bits per pixel.
-tref <x> <y> (default = 0 0)
Sets the origin of the tile partitioning on the reference grid, with
respect to the canvas origin. The value of 'x' ('y') specified can not
be larger than the 'x' one specified in the ref option.
-ref <x> <y> (default = 0 0)
Sets the origin of the image in the canvas system. It sets the
coordinate of the top-left corner of the image reference grid, with
respect to the canvas origin
-tiles <nominal tile width> <nominal tile height> (default = 0 0)
This option specifies the maximum tile dimensions to use. If both
dimensions are 0 then no tiling is used.
-tile_parts <packets per tile-part> (default = 0)
This option specifies the maximum number of packets to have in one
tile-part. 0 means include all packets in first tile-part of each tile
-pfile <filename of arguments file>
Loads the arguments from the specified file. Arguments that are
specified on the command line override the ones from the file.
The arguments file is a simple text file with one argument per line of
the following form:
<argument name>=<argument value>
If the argument is of boolean type (i.e. its presence turns a feature
on), then the 'on' value turns it on, while the 'off' value turns it
off. The argument name does not include the '-' or '+' character. Long
lines can be broken into several lines by terminating them with ''.
Lines starting with '#' are considered as comments. This option is not
recursive: any 'pfile' argument appearing in the file is ignored.
-pph_main [on|off] (default = off)
Packs the packet headers in the main header.
-pph_tile [on|off] (default = off)
Packs the packet headers in the tile headers.
-file_format [on|off] (default = off)
Puts the JPEG 2000 codestream in a JP2 file format wrapper.
-disable_jp2_extension [on|off] (default = off)
JJ2000 automatically adds .jp2 extension when using
'file_format'option. This option disables it when on.
-debug (default = off)
Print debugging messages when an error is encountered.
-Mct [<tile index>] [on|off] ...
Specifies to use component transformation with some tiles. If the
wavelet transform is reversible (w5x3 filter), the Reversible Component
Transformation (RCT) is applied. If not (w9x7 filter), the Irreversible
Component Transformation (ICT) is used.
-Ffilters [<tile-component idx>] <id> [ [<tile-component idx>] <id> ...]
Specifies which filters to use for specified tile-component.
<tile-component idx>: see general note
<id>: ',' separates horizontal and vertical filters, ':' separates
decomposition levels filters. JPEG 2000 part I only supports w5x3 and
w9x7 filters.
-Wwt [full] (default = full)
Specifies the wavelet transform to be used. Possible value is: 'full'
(full page). The value 'full' performs a normal DWT.
-Wlev <number of decomposition levels> (default = 5)
Specifies the number of wavelet decomposition levels to apply to the
image. If 0 no wavelet transform is performed. All components and all
tiles have the same number of decomposition levels.
-Qguard_bits [<tile-component idx>] <gb> [ [<tile-component idx>] <gb>
...] (default = 2)
The number of bits used for each tile-component in the quantizer to
avoid overflow (gb).
-Qstep [<tile-component idx>] <bnss> [ [<tile-component idx>] <bnss> ...]
(default = 0.0078125)
This option specifies the base normalized quantization step size (bnss)
for tile-components. It is normalized to a dynamic range of 1 in the
image domain. This parameter is ignored in reversible coding.
-Qtype [<tile-component idx>] <id> [ [<tile-component idx>] <id> ...]
Specifies which quantization type to use for specified tile-component.
By default (if '-lossless is not specified'), the quantization step
size is 'expounded'.
<tile-component idx> : see general note.
<id>: Supported quantization types specification are : 'reversible' (no
quantization), 'derived' (derived quantization step size) and
'expounded'.
Example: -Qtype reversible or -Qtype t2,4-8 c2 reversible t9 derived.
-Rno_rect [on|off] (default = off)
This argument makes sure that the ROI mask generation is not done using
the fast ROI mask generation for rectangular ROIs regardless of whether
the specified ROIs are rectangular or not
-Rstart_level <level> (default = -1)
This argument forces the lowest <level> resolution levels to belong to
the ROI. By doing this, it is possible to avoid only getting
information for the ROI at an early stage of transmission.<level> = 0
means the lowest resolution level belongs to the ROI, 1 means the two
lowest etc. (-1 deactivates the option)
-Ralign [on|off] (default = off)
By specifying this argument, the ROI mask will be limited to covering
only entire code-blocks. The ROI coding can then be performed without
any actual scaling of the coefficients but by instead scaling the
distortion estimates.
-Rroi [<component idx>] R <left> <top> <width> <height> or [<component
idx>] C <centre column> <centre row> <radius> or [<component idx>] A
<filename>
Specifies ROIs shape and location. The shape can be either rectangular
'R', or circular 'C' or arbitrary 'A'. Each new occurrence of an 'R', a
'C' or an 'A' is a new ROI. For circular and rectangular ROIs, all
values are given as their pixel values relative to the canvas origin.
Arbitrary shapes must be included in a PGM file where non 0 values
correspond to ROI coefficients. The PGM file must have the size as the
image. The component idx specifies which components contain the ROI.
The component index is specified as described by points 3 and 4 in the
general comment on tile-component idx. If this option is used, the
codestream is layer progressive by default unless it is overridden by
the 'Aptype' option.
-Cpp [<tile-component idx>] <dim> <dim> [<dim> <dim>] [ [<tile-component
idx>] ...]
Specifies precinct partition dimensions for tile-component. The first
two values apply to the highest resolution and the following ones (if
any) apply to the remaining resolutions in decreasing order. If less
values than the number of decomposition levels are specified, then the
last two values are used for the remaining resolutions.
-Clen_calc [<tile-component idx>] near_opt|lazy_good|lazy[
[<tile-component idx>] ...] (default = near_opt)
Specifies the algorithm to use in calculating the necessary MQ length
for each decoding pass. The best one is 'near_opt', which performs a
rather sophisticated calculation and provides the best results. The
'lazy_good' and 'lazy' are very simple algorithms that provide rather
conservative results, 'lazy_good' one being slightly better. Do not
change this option unless you want to experiment the effect of
different length calculation algorithms.
-Cterm [<tile-component idx>] near_opt|easy|predict|full[
[<tile-component idx>] near_opt|easy|predict|full ...] (default =
near_opt)
Specifies the algorithm used to terminate the MQ codeword. The most
efficient one is 'near_opt', which delivers a codeword which in almost
all cases is the shortest possible. The 'easy' is a simpler algorithm
that delivers a codeword length that is close to the previous one (in
average 1 bit longer). The 'predict' is almost the same as the 'easy'
but it leaves error resilient information on the spare least
significant bits (in average 3.5 bits), which can be used by a decoder
to detect errors. The 'full' algorithm performs a full flush of the MQ
coder and is highly inefficient.
It is important to use a good termination policy since the MQ codeword
can be terminated quite often, specially if the 'Cbypass' or
'Creg_term' options are enabled (in the normal case it would be
terminated once per code-block, while if 'Creg_term' is specified it
will be done almost 3 times per bit-plane in each code-block).
-Cseg_symbol [<tile-component idx>] on|off[ [<tile-component idx>] on|off
...] (default = off)
Inserts an error resilience segmentation symbol in the MQ codeword at
the end of each bit-plane (cleanup pass). Decoders can use this
information to detect and conceal errors.'on' enables, 'off' disables
it.
-Ccausal [<tile-component idx>] on|off[ [<tile-component idx>] on|off
...] (default = off)
Uses vertically stripe causal context formation. If this is enabled the
context formation process in one stripe is independant of the next
stripe (i.e. the one below it). 'on' enables, 'off' disables it.
-Creg_term [<tile-component idx>] on|off[ [<tile-component idx>] on|off
...] (default = off)
If this is enabled the codeword (raw or MQ) is terminated on a byte
boundary after each coding pass. In this case it is important to use an
efficient termination algorithm, see the 'Cterm' option. 'on' enables,
'off' disables it.
-CresetMQ [<tile-component idx>] on|off[ [<tile-component idx>] on|off
...] (default = off)
If this is enabled the probability estimates of the MQ coder are reset
after each arithmetically coded (i.e. non-lazy) coding pass. 'on'
enables, 'off' disables it.
-Cbypass [<tile-component idx>] on|off[ [<tile-component idx>] on|off
...] (default = off)
Uses the lazy coding mode with the entropy coder. This will bypass the
MQ coder for some of the coding passes, where the distribution is often
close to uniform. Since the MQ codeword will be terminated at least
once per lazy pass, it is important to use an efficient termination
algorithm, see the 'Cterm' option.'on' enables, 'off' disables it.
-Cblksiz [<tile-component idx>] <width> <height> [[<tile-component idx>]
<width> <height>] (default = 64 64)
Specifies the maximum code-block size to use for tile-component. The
maximum width and height is 1024, however the surface area (i.e. width
x height) must not exceed 4096. The minimum width and height is 4.
-Alayers <rate> [+<layers>] [<rate [+<layers>] [...]] (default = 0.015
+20 2.0 +10)
Explicitly specifies the codestream layer formation parameters. The
<rate> parameter specifies the bitrate to which the first layer should
be optimized. The <layers> parameter, if present, specifies the number
of extra layers that should be added for scalability. These extra
layers are not optimized. Any extra <rate> and <layers> parameters add
more layers, in the same way. An additional layer is always added at
the end, which is optimized to the overall target bitrate of the bit
stream. Any layers (optimized or not) whose target bitrate is higher
that the overall target bitrate are silently ignored. The bitrates of
the extra layers that are added through the <layers> parameter are
approximately log-spaced between the other target bitrates. If several
<rate> [+<layers>] constructs appear the <rate> parameters must appear
in increasing order. The rate allocation algorithm ensures that all
coded layers have a minimal reasonable size, if not these layers are
silently ignored.
-Aptype [<tile idx>] res|layer|res-pos|pos-comp|comp-pos [res_start
comp_start layer_end res_end comp_end prog] [[res_start comp_start
ly_end res_end comp_end prog] ...] [[<tile-component idx>] ...]
Specifies which type of progression should be used when generating the
codestream. The 'res' value generates a resolution progressive
codestream with the number of layers specified by 'Alayers' option. The
'layer' value generates a layer progressive codestream with multiple
layers. In any case the rate-allocation algorithm optimizes for best
quality in each layer. The quality measure is mean squared error (MSE)
or a weighted version of it (WMSE). If no progression type is specified
or imposed by other modules, the default value is 'layer'.
It is also possible to describe progression order changes. In this
case, 'res_start' is the index (from 0) of the first resolution level,
'comp_start' is the index (from 0) of the first component, 'ly_end' is
the index (from 0) of the first layer not included, 'res_end' is the
index (from 0) of the first resolution level not included, 'comp_end'
is index (from 0) of the first component not included and 'prog' is the
progression type to be used for the rest of the tile/image. Several
progression order changes can be specified, one after the other.
-Peph [<tile idx>] on|off[ [<tile idx>] on|off ...] (default = off)
Specifies whether end of packet header (EPH) markers should be used.
'on' enables, 'off' disables it.
-Psop [<tile idx>] on|off[ [<tile idx>] on|off ...] (default = off)
Specifies whether start of packet (SOP) markers should be used. 'on'
enables, 'off' disables it.

Send bug reports to: jj2000-bugs@ltssg3.epfl.ch


4.2 JJ2KDecoder参数选项

Usage:
JJ2KDecoder args...

The exit code of the decoder is non-zero if an error occurs.
The following arguments are recongnized:

-Cer [on|off] (default = on)
Specifies if error detection should be performed by the entropy decoder
engine. If errors are detected they will be concealed and the resulting
distortion will be less important. Note that errors can only be
detected if the encoder that generated the data included error
resilience information.
-Cverber [on|off] (default = on)
Specifies if the entropy decoder should be verbose about detected
errors. If 'on' a message is printed whenever an error is detected.
-Rno_roi
This argument makes sure that the no ROI de-scaling is performed.
Decompression is done like there is no ROI in the image
-cdstr_info (default = off)
Display information about the codestream. This information is:
- Marker segments value in main and tile-part headers,
- Tile-part length and position within the code-stream.
-debug (default = off)
Print debugging messages when an error is encountered.
-parsing (default = on)
Enable or not the parsing mode when decoding rate is specified
('-nbytes' or '-rate' options). If it is false, the codestream is
decoded as if it were truncated to the given rate. If it is true, the
decoder creates, truncates and decodes a virtual layer progressive
codestream with the same truncation points in each code-block.
-nbytes <decoding rate in bytes> (default = -1)
Specifies the decoding rate in bytes. The codestream is either parsed
(default) or truncated depending the command line option '-parsing'. To
specify the decoding rate in bits per pixel, use '-rate' options
instead.
-rate <decoding rate in bpp> (default = 100)
Specifies the decoding rate in bits per pixel (bpp) where the number of
pixels is related to the image's original size (Note: this number is
not affected by the '-res' option). The codestream is either parsed
(default) or truncated depending the command line option '-parsing'. To
specify the decoding rate in bytes, use '-nbytes' options instead.
-o <filename>
This is the name of the file to which the decompressed image is
written. If no output filename is given, the image is displayed on the
screen. Output file format is PGX by default. If the extension is
'.pgm' then a PGM file is written as output, however this is only
permitted if the component bitdepth does not exceed 8. If the extension
is '.ppm' then a PPM file is written, however this is only permitted if
there are 3 components and none of them has a bitdepth of more than 8.
If there is more than 1 component, suffices '-1', '-2', '-3', ... are
added to the file name, just before the extension, except for PPM files
where all three components are written to the same file.
-i <filename or url>
The file containing the JPEG 2000 compressed data. This can be either a
JPEG 2000 codestream or a JP2 file containing a JPEG 2000 codestream.
In the latter case the first codestream in the file will be decoded. If
an URL is specified (e.g., http://...) the data will be downloaded and
cached in memory before decoding. This is intended for easy use in
applets, but it is not a very efficient way of decoding network served
data.
-res <resolution level index>
Specifies the resolution level wanted for the decoded image (0 means
the lowest available resolution, the last resolution level gives an
image with original dimension). If given index is greater than the
number of available resolution levels of the compressed image, the
decoded image has the lowest available resolution (among all
tile-components). Note that this option affects only the inverse
wavelet transform and not the number of bytes read by the codestream
parser: this number of bytes depends only on options '-nbytes' or
'-rate'.
-pfile <filename>
Loads the arguments from the specified file. Arguments that are
specified on the command line override the ones from the file.
The arguments file is a simple text file with one argument per line of
the following form:
<argument name>=<argument value>
If the argument is of boolean type (i.e. its presence turns a feature
on), then the 'on' value turns it on, while the 'off' value turns it
off. The argument name does not include the '-' or '+' character. Long
lines can be broken into several lines by terminating them with '\'.
Lines starting with '#' are considered as comments. This option is not
recursive: any 'pfile' argument appearing in the file is ignored.
-verbose [on|off] (default = on)
Prints information about the decoded codestream
-v [on|off] (default = off)
Prints version and copyright information
-u [on|off] (default = off)
Prints usage information. If specified all other arguments (except 'v')
are ignored

Send bug reports to: jj2000-bugs@ltssg3.epfl.ch
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息