调用Java版图像压缩工具:-4.1
0. 简介 0.1. JPEG与
大家知道JPEG是基于离散余弦变换(DCT)的图像压缩标准 , 而是基于小波变换(DWT)的图像压缩标准 , 当然不同的还有编码算法 。JPEG与及其扩展 由 JointGroup 组织创建和维护 , 更多信息参见维基百科: , 或者JPEG官网: , 里面提供了C++、C、Java等语言的实现 , 请根据需要自行下载 。
0.2. 简介
实现的是标准中的part1部分 , 纯由Java语言写成 , 项目官网: , 但该网址本人试图访问多次 , 未果;在JPEG官网也提供了下载链接: , 不过版本号是5.1 , 此外 Code明年将关闭服务 , 本人将其导出到了 , 下载链接点我 。对于文章使用的版本下载 , 待会介绍 。
1. 工具
硬件:PC机
软件:、、Java虚拟机、-4.1
Note:软件工具下载链接请百度或谷歌 , 当然 , 、-4.1、以及基于-5.1修改的的- , 也可以点此下载 。
是写的一个调用-4.1.jar工具包的GUI 程序 , 为方便使用 , 本人进行了一定的修改 。
2. 步骤与方法 2.0 安装软件工具
没有安装 和 Java虚拟机的 , 请自行安装 。Java虚拟机根据自己需要选择 , 有: JVM , Sun‘s Java等等 , ’其环境变量的配置 , 见2.1软件配置 。
和-4.1无需安装 , 解压即可 。
2.1 软件配置 2.1.1 Java虚拟机的环境变量配置
按照提示安装好Java虚拟机后 , 还需要配置环境变量 , 按照下述流程打开环境变量设置页:
控制面板( Panel)——>系统()——> 高级系统设置()——> 高级()——> 环境变量( )
文章插图
图2-1 环境变量设置界面
如上图所示 , 包含两类环境变量:用户环境变量 和 系统环境变量 。我们需要在用户环境变量中添加环境变量 , 在系统变量里添加环境变量 和 新建(如果没有的话)或修改(如果有的话)PATH环境变量 。它们的值如下面绿色部分:
=(是Java的安装路径 , 如E:\ Files\Java\jdk1.7.0_51)
PATH=.;%%\bin(等同于E:\ Files\Java\jdk1.7.0_51\bin)
=.;%%\lib\dt.jar;%%\lib\tools.jar;
【Matlab调用Java版JPEG2000图像压缩工具:jj2000-4.1】下面 , 给出图解
参照上图2-1 , 点击“用户变量(Userfor ...)” 区域的“新建(New)” , 在弹出窗口中 , 按下图输入 , 点击OK , 创建环境变量 , 注意你的JAVA安装路径 。
文章插图
图2-2环境变量
参照上图2-1 , 点击“系统环境变量( )” 区域的“新建(New)” , 在弹出窗口中 , 输入环境变量名 , 及其对应的值 , 点击OK创建 。
参照上图2-1 , 找到“系统环境变量( )” 区域PATH环境变量 , 点击“编辑(Edit)” , 在变量值前端输入PATH的值 , 如下图2-3所示:
文章插图
图2-3 给Path环境变量添加值
这样环境变量就配置好了 。
测试:为了测试环境变量配置是否成功 , 在运行里输入cmd , 打开DOS窗口 , 输入:Java - , 若输出下图所示信息 , 则表明配置成功 。
文章插图
图2-4 Java 的环境变量配置成功
2.1.2 -4.1.jar工具包的环境变量配置
在-4.1文件夹中找到-4.1.jar(没有的话自行编译) , 并将其复制到要存放的路径下 。
-4.1.jar是一个Java类包 , 为了能够在任意路径下自由调用 , 将其路径添加进上面建立的环境变量里去 。假设-4.1.jar的路径是:
D:\\\DIP\\\-4.1.jar , 那么 , 就把它添加进环境变量的值里面 , 用; 隔开 , 这样就添加好了 。
测试:为了测试环境变量配置是否成功 , 在运行里输入cmd , 打开DOS窗口 , 输入:Java-u , 若输出下图所示信息 , 则表明配置成功 , 此命令是查看的使用帮助 。
文章插图
图2-5参数选项
2.-4.1.jar 的使用 找到中的文件 , 使用写字板打开 , 在里面可以找到 , -4.1工具的调用方法 。下面进行简要介绍:
下 , 使用命令添加环境变量:set =/-4.1.jar;%% , 其中 , , 为-4.1.jar所在文件夹路径 。
本文使用的是Sun的JDK , 编解码命令如下 , 其它虚拟机请参见文件 。
2.2.1 DOS下执行编解码
下 , 编码命令:
java
解码命令:
文章插图
java
其中 , , 为可选参数 , 使用Java-u可以查看具体参数选项 , 如-i 是输入文件参数 , -o 是输出文件参数 , -rate用以指定压缩比 。
如下命令 , 以“pict.pgm”文件为输入 , 进行8倍的压缩编码 , 输出文件为“.j2k” 。
Java JJ2KEncoder -i pict.pgm -o pict_JJ2.j2k -rate 1
2.2.2 下通过DOS执行 如果在下调用 , 可以使用自带的 dos 函数 , 将命令加载到DOS运行 , 格式如下 , 其中 , 为命令字符串 。
[s,w] = dos(cmdstr)
2.2.3 下直接执行Java命令
如果想直接在中使用Java命令 , 需要在Java命令前加感叹号! , 如在命令窗口 , 输入:!Java - , 则输出 , 下图所示信息:
图2-6 在中查看 Java 版本信息
同样 , 如果想运行命令 , 可以使用:
!Java JJ2KEncoder -i pict.pgm -o pict_JJ2.j2k -rate 1
2.2. 运行结果示例实现了在下的调用 , 请参考之 , 下面给出本人修改后的一个运行结果图 。
文章插图
图2-7运行结果示例
图中实现的是 16 倍的压缩 , PSNR在23.0dB , 还是很高的!
3. Note
1. 如果出现调用不成功 , 而在DOS下调用成功的情况 , 请注销或重启系统 。
2. 在dos下 输入:Java-u >>C:\help.txt , 可以将结果输出到help.txt文件 。
4. 附录 4.1 参数选项
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. Theseparameters must be provided according to the pattern:"[] " (repeated as many time as needed). respect the following policy according to the degreeof priority:(1) t c : Tile-component specification.(2) t : Tile specification.(3) c : Component specification(4) : Default specification.Where the priorities of the specifications are:(1) > (2) > (3) > (4), ('>' means "overrides"): ',' separates indexes, '-' separates bounds of indexes list. (ex:0,2-4 means indexes 0,2,3 and4).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 Mandatory argument. This option specifies the name of the output fileto which the codestream will be written.-i [ [ ... ]]Mandatory argument. This option specifies the name of the input imagefiles. Supported formats are PGM (raw), PPM (raw) and PGX, which is asimple extension of the PGM file format for single component datasupporting arbitrary bitdepths. If the extension is '.pgm', PGM-rawfile format is assumed, if the extension is '.ppm', PPM-raw file formatis assumed, otherwise PGX file format is assumed. PGM and PPM files areassumed to be 8 bits deep. A multi-component image can be specified byeither specifying several PPM and/or PGX files, or by specifying onePPM file.-lossless [on|off] (default = off)Specifies a lossless compression for the encoder. This options isequivalent to use reversible quantization ('-Qtype reversible') and 5x3wavelet filters pair ('-Ffilters w5x3'). Note that this option cannotbe used with '-rate'. When this option is off, the quantization typeand the filters pair is defined by '-Qtype' and '-Ffilters'respectively.-rate
4.2 参数选项
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 decoderengine. If errors are detected they will be concealed and the resultingdistortion will be less important. Note that errors can only bedetected if the encoder that generated the data included errorresilience information.-Cverber [on|off] (default = on)Specifies if the entropy decoder should be verbose about detectederrors. If 'on' a message is printed whenever an error is detected.-Rno_roiThis 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 isdecoded as if it were truncated to the given rate. If it is true, thedecoder creates, truncates and decodes a virtual layer progressivecodestream with the same truncation points in each code-block.-nbytes (default = -1)Specifies the decoding rate in bytes. The codestream is either parsed(default) or truncated depending the command line option '-parsing'. Tospecify the decoding rate in bits per pixel, use '-rate' optionsinstead.-rate (default = 100)Specifies the decoding rate in bits per pixel (bpp) where the number ofpixels is related to the image's original size (Note: this number isnot affected by the '-res' option). The codestream is either parsed(default) or truncated depending the command line option '-parsing'. Tospecify the decoding rate in bytes, use '-nbytes' options instead.-o This is the name of the file to which the decompressed image iswritten. If no output filename is given, the image is displayed on thescreen. Output file format is PGX by default. If the extension is'.pgm' then a PGM file is written as output, however this is onlypermitted if the component bitdepth does not exceed 8. If the extensionis '.ppm' then a PPM file is written, however this is only permitted ifthere 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', ... areadded to the file name, just before the extension, except for PPM fileswhere all three components are written to the same file.-i The file containing the JPEG 2000 compressed data. This can be either aJPEG 2000 codestream or a JP2 file containing a JPEG 2000 codestream.In the latter case the first codestream in the file will be decoded. Ifan URL is specified (e.g., http://...) the data will be downloaded andcached in memory before decoding. This is intended for easy use inapplets, but it is not a very efficient way of decoding network serveddata.-res Specifies the resolution level wanted for the decoded image (0 meansthe lowest available resolution, the last resolutionlevel gives animage with original dimension). If given index is greater than thenumber of available resolution levels of the compressed image, thedecoded image has the lowest available resolution (among alltile-components). Note that this option affects only the inversewavelet transform and not the numberof bytes read by the codestreamparser: this number of bytes depends only on options '-nbytes' or'-rate'.-pfile Loads the arguments from the specified file. Arguments that arespecified on the command line override the ones from the file.The arguments file is a simple text file with one argument per line ofthe following form:=If the argument is of boolean type (i.e. its presence turns a featureon), then the 'on' value turns it on, while the 'off' value turns itoff. The argument name does not include the '-' or '+' character. Longlines can be broken into several lines by terminating them with '\'.Lines starting with '#' are considered as comments. This option is notrecursive: 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 ignoredSend bug reports to: jj2000-bugs@ltssg3.epfl.ch