for i = 0:255 P(i+1) = length(find(I1 == i))/(M*N); end
时间: 2024-05-17 17:16:23 浏览: 11
这行代码是一个 for 循环语句,用于计算像素值在 0~255 范围内每个灰度级别的出现概率。其中,"i" 是循环变量,从 0 循环到 255。"P" 是一个之前定义的长度为 256 的数组,用于记录每个灰度级别的出现概率。"I1" 是之前展开成列向量的原始图像数据,"M" 和 "N" 分别是原始图像数据的行数和列数。
循环体中的语句 "P(i+1) = length(find(I1 == i))/(M*N)" 的意义是:
1. 首先使用 "find" 函数找到 "I1" 中所有像素值等于 "i" 的位置,"length" 函数用于计算这些位置的数量,即该灰度级别在图像中出现的像素数量。
2. 将这个像素数量除以图像像素总数 "M*N",得到该灰度级别在图像中出现的概率。
3. 将这个概率赋值给数组 "P" 中对应的元素 "P(i+1)",注意这里要将 "i" 加一,是因为 MATLAB 中数组的下标是从 1 开始的,而不是从 0 开始。
相关问题
clear; clc; close all; A=imread('1.jpg'); I=rgb2gray(A);%将彩色图像转为灰度图像 [M,N] = size(I);%将图像转为二维矩阵 I1 = I(:);%转为一维向量 P = zeros(1,256); %获取各符号的概率; for i = 0:255 P(i+1) = length(find(I1 == i))/(M*N); end k = 0:255; dict = huffmandict(k,P); %根据灰度级k和概率数组P生成Huffman字典 enco = huffmanenco(I1,dict); %哈夫曼编码 deco = huffmandeco(enco,dict); %哈夫曼解码 Ide = col2im(deco,[M,N],[M,N],'distinct'); %把向量重新转换成图像块; subplot(1,2,1);imshow(I);title('原始图像'); subplot(1,2,2);imshow(uint8(Ide));title('解码图像'); B=length(enco);%原始图像比特长度 sumcode=length(deco);%编码后比特长度 CR=sumcode/B;%计算压缩率 disp(['原始图像 Bit: ',num2str(B),' bit']); disp(['压缩图像 Bit: ',num2str(sumcode),' bit']); disp(['压缩率: ',num2str(CR)]);
好的,我进行修改后的代码如下:
```matlab
clear; clc; close all;
img = imread('1.jpg');
grayImg = rgb2gray(img); %将彩色图像转为灰度图像
[M, N] = size(grayImg); %将图像转为二维矩阵
I1 = grayImg(:); %转为一维向量
probabilities = zeros(1,256); %获取各符号的概率
for i = 0:255
probabilities(i+1) = length(find(I1 == i))/(M*N);
end
symbols = 0:255;
dict = huffmandict(symbols, probabilities); %根据灰度级symbols和概率数组probabilities生成Huffman字典
encoded = huffmanenco(I1, dict); %哈夫曼编码
decoded = huffmandeco(encoded, dict); %哈夫曼解码
decodedImg = col2im(decoded, [M, N], [M, N], 'distinct'); %把向量重新转换成图像块
subplot(1,2,1);imshow(grayImg); title('原始图像');
subplot(1,2,2);imshow(uint8(decodedImg)); title('解码图像');
originalBitLength = length(I1)*8; %原始图像比特长度
compressedBitLength = length(encoded); %编码后比特长度
compressionRatio = originalBitLength/compressedBitLength; %计算压缩率
disp(['原始图像 Bit: ',num2str(originalBitLength),' bit']);
disp(['压缩图像 Bit: ',num2str(compressedBitLength),' bit']);
disp(['压缩率: ',num2str(compressionRatio)]);
```
I = imread('C:\Users\86150\Desktop\4\b.png'); [M,N] = size(I); I1 = I(:);% 将图像矩阵转化成向量 P = zeros(1,256); %获取各符号的概率; for i = 0:255 P(i+1) = length(find(I1 == i))/(M*N); end k = 0:255; dict = huffmandict(k,P); % 利用Matlab自带的huffmandict函数生成哈夫曼树及映射表 enco = huffmanenco(I1,dict); % 利用生成的映射表对图像向量进行Huffman编码 deco = huffmandeco(enco,dict); % 利用生成的字典对Huffman编码进行解码 Ide = col2im(deco,[M,N],[M,N],'distinct'); %把向量重新转换成图像块; subplot(1,2,1);imshow(I);title('原图'); subplot(1,2,2);imshow(uint8(Ide));title('解码后');% 显示还原后的图像
这段代码是用 Matlab 实现的 Huffman 编码和解码,可以将一张图片进行压缩。其中,将图像矩阵转化成向量的步骤可以方便地进行数据处理,利用 huffmandict 函数生成哈夫曼树及映射表,再利用 huffmanenco 函数对图像向量进行 Huffman 编码。在解码时,利用 huffmandeco 函数和生成的字典对 Huffman 编码进行解码,最后把向量重新转换成图像块。最后,subplot 函数用于将原图和还原后的图像进行对比显示。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)