关于图像压缩JPEG2000的Python代码实现

刚开始参考了别人的代码,发现跑不起来,然后就从算术编码的数学逻辑一步一步推到反推去实现,但实际的计算机数据处理受到精度和逻辑的限制,出现了很多情况,就自己从底层去写远离和处理一些“数学的bug”,详细的注释都在代码里了

代码的txt文件

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Changes from version 2.2.2 to version 2.2.3 ------------------------------------------- * Extremely minor changes to avoid mixed use of formatted and unformatted calls to "ostream" objects. These appear to excite a bug in GCC version 3.0. The only file affected is "params.cpp" in "coresys/parameters". Changes from version 2.2.1 to version 2.2.2 ------------------------------------------- Note: none of these have any impact whatsoever on executable code or DLL's. * Renamed the "core" directory as "coresys". A trivial change and my appologies for those whom this might adversely affect. However, the use of the name "core" was causing some people difficulties, since it is identical to the name of a Unix core dump file. * Made the Linux MMX-optimized functions "extern C" instead of "extern", so as to avoid problems caused by different name mangling conventions between various versions of gcc. * Eliminated multi-line comments from assembler files so as to avoid problems created by earlier versions of the gnu assembler. Changes from version 2.2 to version 2.2.1 ----------------------------------------- * Replaced the C++ I/O routines used for image and compressed data transfers with ANSI C I/O functions. This was motivated by the fact that the new-style ANSI C++ I/O package is unbelievably slow, at least as implemented in Microsoft's Visual C++ compiler. The change has no impact whatsoever on the Kakadu core system; it affects only the implementation of a few application level objects -- the core system abstracts all such I/O considerations through interface classes which are implemented by applications. Everything now runs a little faster than it did in version 2.1 and quite a bit faster than it did in the first release of version 2.2. * Made provision for compiling under versions of GCC prior to version 3.0. To use this, you should define GCC_VERSION_LESS_THAN_3. Changes from version 2.1 to version 2.2 --------------------------------------- * Extensive support for ROI (Region of Interest) specification at encode time (see "kakadu.pdf" for more on this). * Migrated from the old-style C++ iostream package to the new standard iostream package -- minimal use of "using namespace std" and never used in common header files, so this should enhance namespace protection and portability. * Added AT&T style versions of the small amount of Pentium assembly code to enable compilation of a high speed version under GCC as well as MSVC. * Some minor bug fixes. Changes from version 2.0.2 to version 2.1 ----------------------------------------- * Extensive support for working with the JP2 file format. The "kdu_show" application demonstrates the capabilities required of a conformant JP2 reader: palette mapping; interpolation of components to a common resolution; application of registration offsets in the CRG marker segment; and colour conversion to an appropriate rendering space (sRGB here). The "kdu_region_decompressor" object provides extensive support for general purpose interactive rendering applications, performing all of the above tasks in a platform independent manner. * It is now possible to directly control rate-distortion slope thresholds used in the construction of quality layers. This capability may also be used to significantly increase compression speed, if a suitable threshold is known, since the encoder then incrementally predicts the point at which there is no point in coding further coding passes. * A number of improvements to the "kdu_show" application, including the ability to arbitrarily zoom into images. * A number of minor bug fixes, including one important bug reported by Aaron Deever of Kodak, and a bug which occasionally manifested itself in the incremental rate prediction heuristic (reported by Jim Andrew of CISRA). * Improved documentation. Changes from version 2.0.1 to version 2.02 ------------------------------------------ * A PDF document (documentation.pdf) has been prepared to guide the new user into some of the more important aspects of the Kakadu system. The first draft is included here. * A very simple compression example and a very simple decompression example have been added to assist developers in familiarizing themselves with the Kakadu system -- the existing demo apps provide perhaps too much functionality to be quickly understood. * A full BIBO (Bounded Input Bounded Output) numerical analysis of the DWT and other processing steps is used to establish the best utilization of limited precision sample data representations. The new version should not be able to fall prey to numerical overflow or underflow difficulties under any circumstances (this could just have been possible with the last version). It also provides slightly higher accuracy. * The automatic heuristic for generating quality layer rate targets has been substantially improved. * A number of minor bugs/limitations were corrected, although these had not manifested themselves in any real examples. Changes from version 2.0 to version 2.01 ---------------------------------------- * One line change in each of "kdu_expand.cpp" and "kdu_compress.cpp" to correct a minor rare bug in these demo applications. * Minor changes in "kdu_show.cpp" to correct a rare bug and improve the user interface in regard to image rotation. * Four lines added to each of "encoder.cpp" and "decoder.cpp" to fix a minor memory leak.
### 回答1: 以下是一个使用OpenCV库实现JPEG2000压缩的示例代码: ```python import cv2 # 读取图像 img = cv2.imread('input.jpg') # 定义压缩参数 encode_param = [int(cv2.IMWRITE_JPEG2000_COMPRESSION), 90] # 压缩图像并保存 cv2.imwrite('output.jp2', img, encode_param) ``` 在上面的代码中,我们使用`cv2.imread()`函数读取输入图像。然后,我们定义压缩参数`encode_param`,其中第一个参数表示压缩算法(这里使用JPEG2000),第二个参数表示压缩质量(0-100之间的整数,90表示高质量)。最后,我们使用`cv2.imwrite()`函数将压缩后的图像保存到输出文件中。 需要注意的是,使用JPEG2000进行压缩时,输出图像的文件扩展名通常是`.jp2`,而不是`.jpg`。 ### 回答2: JPEG2000是一种高效的图像压缩算法,可以将图像以较小的文件大小保存,并保持较高的图像质量。下面是一个用Python实现JPEG2000压缩的简单示例代码: 首先,我们需要安装PyDCT库,这是一个用于计算离散余弦变换(DCT)的库。可以使用以下命令安装: ``` pip install pydct ``` 接下来,我们可以使用以下代码实现JPEG2000压缩: ```python import pydct import numpy as np from PIL import Image # 加载图像 image = Image.open('input_image.jpg') data = np.array(image) # 离散余弦变换(DCT) dct_data = pydct.dct_2d(data) # 量化 quantization_matrix = np.array([[16, 11, 10, 16, 24, 40, 51, 61], [12, 12, 14, 19, 26, 58, 60, 55], [14, 13, 16, 24, 40, 57, 69, 56], [14, 17, 22, 29, 51, 87, 80, 62], [18, 22, 37, 56, 68, 109, 103, 77], [24, 35, 55, 64, 81, 104, 113, 92], [49, 64, 78, 87, 103, 121, 120, 101], [72, 92, 95, 98, 112, 100, 103, 99]]) quantized_data = np.round(dct_data / quantization_matrix) # 反量化 reconstructed_data = quantized_data * quantization_matrix # 反离散余弦变换(IDCT) reconstructed_image = pydct.idct_2d(reconstructed_data) # 保存压缩后的图像 compressed_image = Image.fromarray(reconstructed_image.astype(np.uint8)) compressed_image.save('compressed_image.jp2') ``` 以上代码中,首先我们使用PIL库加载需要压缩的图像,并将其转换为numpy数组以便进行处理。然后,我们使用PyDCT库中的`dct_2d`函数对图像进行离散余弦变换(DCT)。接下来,我们将DCT系数进行量化,并使用预定义的量化矩阵进行除法运算。然后,我们将量化后的数据进行反量化,并使用`idct_2d`函数进行反离散余弦变换(IDCT)。最后,我们使用PIL库将压缩后的图像保存为JPEG2000文件。 请注意,以上代码只是一个简单的示例,实际应用中可能需要更复杂的处理,如色彩空间转换,压缩率控制等。同时,也有一些专业的库可以用于JPEG2000实现,如OpenJPEG库。以上代码只是提供了一个简单的入门方法。 ### 回答3: Python实现JPEG2000压缩代码可以使用一些第三方库来方便地实现。一个常用的库是OpenCV,下面是一个简单的例子: ```python import cv2 def compress_jpeg2000(image_path, output_path, compression_level=90): # 读取原始图像 img = cv2.imread(image_path) # 创建JPEG2000编码器 j2k = cv2.JPEG2000Encoder_create() # 设置JPEG2000编码参数 params = [cv2.IMWRITE_JPEG2000_COMPRESSION_X1000, compression_level] # 压缩图像并保存到输出路径 cv2.imwrite(output_path, img, params) print("JPEG2000压缩完成!") # 调用压缩函数 compress_jpeg2000("input.jpg", "output.jp2", compression_level=500) ``` 在上述代码中,我们使用了OpenCV库来实现JPEG2000的压缩功能。我们首先使用`cv2.imread`函数读取输入图像,然后创建一个`JPEG2000Encoder`对象。接下来,我们可以通过设置一些参数来控制压缩质量,其中`cv2.IMWRITE_JPEG2000_COMPRESSION_X1000`设置压缩级别,值越低代表压缩率越高。最后,我们使用`cv2.imwrite`将压缩后的图像保存到输出路径。 需要注意的是,上述代码需要安装OpenCV库,并且可能需要安装额外的依赖库来支持JPEG2000编码器。 希望这个简单的例子能够帮助你理解Python中如何实现JPEG2000压缩代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值