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

JPG学习笔记3(附完整代码)

2021-02-18 02:53 866 查看
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, 75, 1), 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: rgba(255, 255, 255, 1); font-family: "微软雅黑", "宋体", "黑体", Arial; font-size: 15px; font-weight: bold; height: 24px; line-height: 23px; margin: 12px 0 !important; padding: 5px 0 5px 10px; text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) } #topics h1 span { font-weight: bold; line-height: 1.5; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; text-decoration: underline; color: rgba(201, 27, 67, 1); text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) }

  采样后,需要对8*8block进行DCT(离散余弦变换)。为什么要进行DCT?第一点是余弦变化后的图片能量主要集中在低频,我们只需要保存低频数据,默认高频0。第二点是,DCT后的图片很适合哈夫曼压缩,对于原图而言,区域相连的pixle数值差不多,哈夫曼压缩效果差。全部代码在 https://github.com/Cheemion/JPEG_COMPRESS

function [ out ] = g( u,v )
p = zeros(8,8);
for i = 0:7
for j = 0:7
p(i + 1,j + 1) = cos((
56c
i + 0.5) * u * pi / 8) * cos((j + 0.5) * v * pi / 8);
end
end
out = p;
end
matlab画图代码
clc; clear; close all;
figure;
maximum = 0;
minimum = 100;
for u = 0:7
for v = 0:7
pp = g(u,v)
subplot(8, 8, u * 8 + v + 1);
imshow((pp + 1) ./ 2);
end
end
matlab画图代码主函数 乘以系数c(u)c(v)是使我们的G(i, j)变成单位向量。公式剩余部分就是点乘的过程。

 

 

 IDCT逆变换

3.代码

void DCT(Block& block) {
Bloc
ad8
k temp;
std::memcpy(&temp, &block, sizeof(Block)); //copy from original
//8 rows
//DCT行变化
for(uint i = 0; i < 8; i++) {
double* f = &temp[i * 8]; //one dimension Array , and will perform DCT on it
for(uint k = 0; k < 8; k++) {
double sum = 0.0;
for(uint n = 0; n < 8; n++){
sum = sum + f
 * std::cos(((n + 0.5) * M_PI * k / 8));
}
sum = (k == 0) ? (sum * std::sqrt(1.0 / 8)) : (sum * std::sqrt(2.0 / 8));
block[i * 8 + k] = sum;
}
}

std::memcpy(&temp, &block, sizeof(Block)); //copy from
//DCT列变化
for(uint i = 0; i < 8; i++) {
double* f = &temp[i]; //one dimension Array with 8 steps increment , and will perform DCT on it
for(uint k = 0; k < 8; k++) {
double sum = 0.0;
for(uint n = 0; n < 8; n++){
sum = sum + f[n * 8] * std::cos(((n + 0.5) * M_PI * k / 8));
}
sum = (k == 0) ? (sum * std::sqrt(1.0 / 8)) : (sum * std::sqrt(2.0 / 8));
block[i + k * 8] = sum;
}
}
}

 

 以上全部的代码在https://github.com/Cheemion/JPEG_COMPRESS/tree/main/Day3

 完结

  Thanks for reading.

   wish you have a good day.

                                                                                                                                                                                                                                                              >>>> JPG学习笔记4(附完整代码)

 

参考资料

[1]https://github.com/Cheemion/JPEG_COMPRESS/blob/main/resource/Compressed%20Image%20File%20Formats%20JPEG%2C%20PNG%2C%20GIF%2C%20XBM%2C%20BMP%20-%20John%20Miano.pdf

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐