怎么做网站宣传,psd设计网站模板,wordpress在线升级,展厅展馆设计引言
当想要压缩一张彩色图像时#xff0c;彩色图像通常由数百万个颜色值组成#xff0c;每个颜色值都由红、绿、蓝三个分量组成。因此#xff0c;如果我们直接对图像的每个像素进行编码#xff0c;会导致非常大的数据量。为了减少数据量#xff0c;我们可以尝试减少颜色…
引言
当想要压缩一张彩色图像时彩色图像通常由数百万个颜色值组成每个颜色值都由红、绿、蓝三个分量组成。因此如果我们直接对图像的每个像素进行编码会导致非常大的数据量。为了减少数据量我们可以尝试减少颜色的数量从而降低存储需求。
1.主要原理
一颜色聚类Color Clustering
首先使用 KMeans 聚类算法将图像中的颜色值聚类为较少数量的颜色簇。聚类的数量由 n_clusters 参数指定。每个像素被归类到与其最接近的聚类中心所代表的颜色簇。颜色聚类的过程大致如下
图像转换 首先彩色图像被转换为一个包含所有像素颜色值的数据集。每个像素的颜色通常由红、绿、蓝三个分量组成因此数据集中的每个样本都是一个三维向量表示一个像素的颜色。选择聚类数量 在应用 KMeans 算法之前需要确定聚类的数量。这个数量通常由用户指定通过参数 n_clusters 控制。应用 KMeans 算法 将 KMeans 算法应用于颜色数据集将颜色值聚类为指定数量的簇。每个簇的质心代表了该簇的平均颜色。像素映射 每个像素的颜色被映射到最接近的簇的质心所代表的颜色。这样整个图像被转换为由较少数量的颜色值表示的压缩图像。
通过颜色聚类彩色图像的颜色数量得以减少从而实现了数据的压缩。压缩后的图像仍然能够保持视觉上的相似性同时大大降低了存储空间的需求。
二动态规划量化Dynamic Programming Quantization
接下来通过动态规划量化算法对颜色进行压缩。这个算法会进一步减少颜色的数量并尽可能保持图像的质量。参数 max_colors 指定了压缩后图像中的最大颜色数量。算法会尽量选择与原始图像相似的颜色进行保留以最大程度地保持图像的质量。而在这部分动态规划量化过程大致如下
初始化 首先初始化状态数组表示不同颜色数量下的最优颜色组合。通常初始状态可以是一个空数组或者包含少量颜色的数组。状态转移 根据动态规划的思想从初始状态开始逐步扩展计算每个状态下的最优颜色组合。这个过程通常涉及到对每种可能的颜色组合进行评估并根据优化准则选择最优的组合。选择最优解 最终选择最优的颜色组合作为压缩后的图像的颜色集合。这个颜色集合将用于替换原始图像中的颜色从而实现图像的压缩。压缩数据保存 压缩后的图像数据以及相关信息如原始图像的尺寸、选择的颜色集合等被保存为 NumPy 数组并通过 np.savez_compressed() 函数保存到指定路径。
通过动态规划量化我们能够选择一组颜色使得压缩后的图像在尽可能减少颜色数量的情况下仍然能够保持与原始图像相似的视觉效果。这样就实现了对图像数据的进一步压缩。
三压缩数据保存
压缩后的图像数据以及相关信息如原始图像的尺寸、聚类数、最大颜色数、聚类中心颜色等被保存为 NumPy 数组并通过 np.savez_compressed() 函数保存到指定路径。
四解压缩过程
解压缩过程与压缩过程相反。首先加载压缩后的图像数据然后根据聚类中心颜色替换像素颜色最后将重构后的图像数据重塑为原始形状并恢复图像的原始尺寸。
2.彩色图像压缩类
一类结构介绍
将上面所述的一个彩色图像的压缩功能整合为一个名为’ColorfulImageCompressor’的类在这个类中有四个函数它们的函数名称、接受参数以及介绍如下
ColorfulImageCompressor类
__init__(self, n_clusters, max_colors, resize_factor0.5): 初始化彩色图像压缩器对象。compress(self, image_path, compressed_file_path): 压缩彩色图像并保存到指定路径。decompress(self, compressed_file_path): 解压缩彩色图像并返回解压缩后的图像对象。_dynamic_programming_quantization(self, image_array): 动态规划量化将彩色图像颜色量化为指定数量的颜色。
二初始化参数
在创建一个彩色图像压缩类的时候需要传入以下三个参数进行参数的初始化。
n_clusters聚类数用于 KMeans 算法指定图像中的颜色数量。max_colors最大颜色数用于动态规划量化指定压缩后图像中的最大颜色数量。resize_factor缩放因子用于调整图像尺寸默认为 0.5表示将图像尺寸缩小到原始的一半。
三函数介绍
(1)compress(self, image_path, compressed_file_path) 介绍 该函数的作用是压缩彩色图像并保存到指定路径。 参数 image_path原始图像文件路径。 compressed_file_path压缩后的图像文件路径。 函数体 def compress(self, image_path, compressed_file_path):压缩彩色图像并保存到指定路径。参数- image_path原始图像文件路径。- compressed_file_path压缩后的图像文件路径。# 打开图像并转换为 RGB 模式image Image.open(image_path)image image.convert(RGB)# 根据缩放因子调整图像大小new_size (int(image.width * self.resize_factor), int(image.height * self.resize_factor))image image.resize(new_size)# 将图像转换为 NumPy 数组并重塑为二维数组np_image np.array(image)original_shape np_image.shapenp_image np_image.reshape(-1, 3)# 使用动态规划量化对图像进行压缩compressed_data self._dynamic_programming_quantization(np_image)# 保存压缩后的图像数据到指定路径np.savez_compressed(compressed_file_path, np_imagecompressed_data[np_image], original_shapeoriginal_shape, n_clustersself.n_clusters, max_colorsself.max_colors, center_colorscompressed_data[center_colors])(2)decompress(self, compressed_file_path)
介绍 解压缩彩色图像并返回解压缩后的图像对象。参数 compressed_file_path压缩后的图像文件路径。 返回 reconstructed_image解压缩后的图像对象。函数体 def decompress(self, compressed_file_path):解压缩彩色图像并返回解压缩后的图像对象。参数- compressed_file_path压缩后的图像文件路径。返回- reconstructed_image解压缩后的图像对象。# 加载压缩后的图像数据compressed_data np.load(compressed_file_path)np_image compressed_data[np_image].reshape(-1, 3)center_colors compressed_data[center_colors]# 根据聚类中心替换像素颜色for i in range(self.n_clusters):np_image[np_image[:, 0] i] center_colors[i]# 将重构后的图像数据重塑为原始形状original_shape compressed_data[original_shape]reconstructed_image np_image.reshape(*original_shape).astype(uint8)reconstructed_image Image.fromarray(reconstructed_image, RGB)# 恢复图像原始尺寸original_size (int(reconstructed_image.width / self.resize_factor), int(reconstructed_image.height / self.resize_factor))reconstructed_image reconstructed_image.resize(original_size)return reconstructed_image(3)_dynamic_programming_quantization(self, image_array)
介绍 动态规划量化将彩色图像颜色量化为指定数量的颜色。参数 image_array图像数据的 NumPy 数组表示。 返回 compressed_data包含压缩后图像数据及相关信息的字典。函数体 def _dynamic_programming_quantization(self, image_array):动态规划量化将彩色图像颜色量化为指定数量的颜色。参数- image_array图像数据的 NumPy 数组表示。返回- compressed_data包含压缩后图像数据及相关信息的字典。# 使用 KMeans 进行聚类kmeans KMeans(n_clustersself.n_clusters)labels kmeans.fit_predict(image_array)quantized_image np.zeros_like(image_array)# 遍历每个聚类簇for i in range(self.n_clusters):# 获取当前簇的像素颜色及其出现次数cluster_pixels image_array[labels i]unique_colors, color_counts np.unique(cluster_pixels, axis0, return_countsTrue)# 选取出现次数最多的前 max_colors 个颜色作为量化后的颜色color_indices np.argsort(color_counts)[::-1][:self.max_colors]quantized_colors unique_colors[color_indices]# 计算聚类中像素与量化后颜色的距离distances np.linalg.norm(cluster_pixels[:, None] - quantized_colors, axis2)quantized_indices np.argmin(distances, axis1)# 使用量化后颜色替换聚类中的像素颜色quantized_image[labels i] quantized_colors[quantized_indices]# 存储聚类中心颜色center_colors kmeans.cluster_centers_.astype(uint8)return {np_image: quantized_image, n_clusters: self.n_clusters, max_colors: self.max_colors, center_colors: center_colors}(四)使用说明
# 创建压缩器对象
compressor ColorfulImageCompressor(n_clusters4, max_colors2, resize_factor0.5) # 压缩彩色图像
image_path ./img/image2.jpg
compressed_file_path ./npz/compressed_image2_n4_c2.npz
compressor.compress(image_path, compressed_file_path) # 解压缩图像并显示
reconstructed_image compressor.decompress(compressed_file_path)
reconstructed_image.show()
reconstructed_image.save(./img/reconstructed_image2_n4_c2.jpg) 3.测试结果
测试图片我们使用的采用的一张818*818分辨率大小为79.49KB的彩色图片。分别使用不同的聚类数量和颜色数量来进行测试。
原始图片聚类数为8颜色为2的压缩图片
详细运行数据如下表下面文件名中的n为聚类数而c为颜色数
文件名原始大小KB压缩后的中间文件大小KB解压缩后的图片大小 (KB)reconstructed_image2_n4_c279.4929.541.7reconstructed_image2_n4_c479.4949.345.2reconstructed_image2_n4_c879.4970.951.3reconstructed_image2_n4_c1679.4994.359.3reconstructed_image2_n8_c279.4948.348.7reconstructed_image2_n8_c479.4973.352.5reconstructed_image2_n8_c879.4910159.1reconstructed_image2_n8_c1679.4912561.1 结束语
如果有疑问欢迎大家留言讨论你如果觉得这篇文章对你有帮助可以给我一个免费的赞吗你们的认可是我最大的分享动力