clc; a=imread('11.jpg'); imshow(uint8(a)); b=rgb2gray(a); [wid,hei]=size(b); qt=zeros(wid/2+1,hei/2+1); i1=1; j1=1; for i=1:2:wid for j=1:2:hei qt(i1,j1)=b(i,j); j1=j1+1; end i1=i1+1; j1=1; end figure imshow(uint8(qt)); qt1=zeros(wid/4+1,hei/4+1); i1=1; j1=1; for i=1:4:wid for j=1:4:hei qt1(i1,j1)=b(i,j); j1=j1+1; end i1=i1+1; j1=1; end figure imshow(uint8(qt1));
时间: 2024-04-06 22:32:45 浏览: 14
根据您提供的代码,我看到您的程序是将原始图像进行降采样,生成了两个不同程度的缩小版图像。如果您需要修改输入大小的限制,可以尝试修改代码中的像素步长,即将代码中的 `1` 和 `4` 改为其他的整数,从而更改缩小程度。例如,将 `1` 和 `4` 分别改为 `2` 和 `8`,即可生成更小的图像。另外,您还可以使用更高级的图像处理算法,如卷积神经网络,来实现更灵活的图像缩放。
相关问题
为以下代码给出注释:clc;clear all;close all; a=imread('E:\desk\gezhongzuoye\shuzi\Lena.tif'); c=zeros(size(a)); pos=find((a>=60) & (a<105)); c(pos)=a(pos); b(:,:,3)=c; c=zeros(size(a)); pos= find((a>=105) & (a<150)); c(pos)=a(pos); b(:,:,2)=c; c=zeros(size(a)); pos= find((a>=150)); c(pos)=a(pos); b(:,:,1)=c; b=uint8(b); figure; subplot(1,2,1),imshow(a,[]),title('原始图像') subplot(1,2,2),imshow(b,[]),title('灰度转彩色图像')
这段代码是使用 MATLAB 编写的,主要功能是将灰度图像转换为彩色图像。以下是每行代码的注释:
clc; % 清除命令行窗口中的内容
clear all; % 清空当前工作空间中的所有变量
close all; % 关闭所有的图形窗口
a=imread('E:\desk\gezhongzuoye\shuzi\Lena.tif'); % 读取名为 "Lena.tif" 的图像
c=zeros(size(a)); % 生成与图像 a 相同尺寸的全零矩阵 c
pos=find((a>=60) & (a<105)); % 找到灰度值在 60~104 之间的像素位置
c(pos)=a(pos); % 将这些像素的灰度值赋给矩阵 c
b(:,:,3)=c; % 将矩阵 c 的值赋给彩色图像 b 的蓝色通道
c=zeros(size(a)); % 重置矩阵 c
pos= find((a>=105) & (a<150)); % 找到灰度值在 105~149 之间的像素位置
c(pos)=a(pos); % 将这些像素的灰度值赋给矩阵 c
b(:,:,2)=c; % 将矩阵 c 的值赋给彩色图像 b 的绿色通道
c=zeros(size(a)); % 重置矩阵 c
pos= find((a>=150)); % 找到灰度值大于等于 150 的像素位置
c(pos)=a(pos); % 将这些像素的灰度值赋给矩阵 c
b(:,:,1)=c; % 将矩阵 c 的值赋给彩色图像 b 的红色通道
b=uint8(b); % 将彩色图像 b 的数据类型转换为 uint8(8 位无符号整数)
figure; % 新建一个图形窗口
subplot(1,2,1),imshow(a,[]),title('原始图像') % 在第一个子图中显示原始图像 a,并显示标题
subplot(1,2,2),imshow(b,[]),title('灰度转彩色图像') % 在第二个子图中显示转换后的彩色图像 b,并显示标题
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)]);
```