包含四段程序
CODE1:
clear;
I=imread('xingshi32.bmp');
if(isgray(I)==0)
disp('请输入灰度图像,本程序用来处理128 *128的灰度图像!'); else
if (size(I)~=[128,128])
disp('图像的大小不合程序要求!'); else
H.color=[1 1 1]; %设置白的画布 figure(H);imshow(I); title('原图像');
zeroImage=repmat(uint8(0),[128 128]);
figure(H); %为分裂合并后显示的图设置画布 meansImageHandle=imshow(zeroImage); title('块均值图像');
%%%%%设置分裂后图像的大小由于本图采用了128像素的图 blockSize=[128 64 32 16 8 4 2];
%%设置一个S稀疏矩阵用于四叉树分解后存诸数据 S=uint8(128); S(128,128)=0;
threshold=input('请输入分裂的阈值(0--1):');%阈值 threshold=round(255*threshold); M=128;dim=128; tic
%%%%%%%%%%%%%%%%% 分裂主程序%%%%%%%%%%% while (dim>1) [M,N] = size(I);
Sind = find(S == dim); numBlocks = length(Sind); if (numBlocks == 0) %已完成 break; end
rows = (0:dim-1)'; cols = 0:M:(dim-1)*M; rows = rows(:,ones(1,dim)); cols = cols(ones(dim,1),:); ind = rows + cols;
ind = ind(:);
tmp = repmat(Sind', length(ind), 1); ind = ind(:, ones(1,numBlocks)); ind = ind + tmp;
blockValues= I(ind);
blockValues = reshape(blockValues, [dim dim numBlocks]);
if(isempty(Sind)) %已完成 break; end [i,j]=find(S);
set(meansImageHandle,'CData',ComputeMeans(I,S)); maxValues=max(max(blockValues,[],1),[],2); minValues=min(min(blockValues,[],1),[],2);
doSplit=(double(maxValues)-double(minValues))>threshold; dim=dim/2; Sind=Sind(doSplit);
Sind=[Sind;Sind+dim;(Sind+M*dim);(Sind+(M+1)*dim)]; S(Sind)=dim; end
[i,j]=find(S); % 用来寻找四叉机分解结果中大小为S的块的位置
set(meansImageHandle,'CData',ComputeMeans(I,S)); % 显示分解结果块均值图像 Numberofbloks=length(i); %计算块数 %sizev=size(v); end end toc
CODE2:
function means = ComputeMeans(I, S) % 用来计算给定图像和稀疏矩阵的块均值 % I: 为给定的图像 % S: 为稀疏矩阵 means = I;
for dim = [128 64 32 16 8 4 2 1]; values = getblk(I, S, dim);
if (~isempty(values))
%%%%%以下的句子是将小块的平均值来代替原图像中相应的块处的像素%%%% if (min(min(values))>=60)
means = setblk(means, S, dim, 0); %用于合并时的阈值 else
%means = setblk(means, S, dim, sum(sum(values,1),2) ./ dim^2+std2(values)); %means = setblk(means, S, dim, sum(sum(values,1),2) ./ dim^2); %means = setblk(means, S, dim, mean2(values)); means = setblk(means, S, dim, max(max(values,1),2)); end end end
CODE3:
function [val,r,c] = getblk(A,S,dim) % I:为待处理的图像
% S:为四叉树分解后返回的稀疏矩阵包含四叉树结构
% Val是dim * dim*k数组, 包含图像I的四叉树分解中的每个 dim *dim 块 % k是四叉树分解的dim *dim块的数量 % 如果没有指定大小的块那么返回一个空矩阵 [M,N] = size(A);
Sind = find(S == dim); numBlocks = length(Sind);
if (numBlocks == 0) % 没有找到任何模块 val = zeros(dim,dim,0); % 返回空矩阵 r = zeros(0,1); c = zeros(0,1); return; end
% 为dim *dom的块计算索引%%%%%%%%%%%%%%%%% rows = (0:dim-1)'; cols = 0:M:(dim-1)*M; rows = rows(:,ones(1,dim)); cols = cols(ones(dim,1),:); ind = rows + cols; ind = ind(:);
% 计算索引矩阵
tmp = repmat(Sind', length(ind), 1); ind = ind(:, ones(1,numBlocks)); ind = ind + tmp;
val = A(ind);
val = reshape(val, [dim dim numBlocks]);
CODE4:
function B = setblk(A,S,dim,val) % I 为待处理的图像
% S:为四叉树分解后的稀疏矩阵包含四叉树结构 % Val:是dim * dim *k数组
% K :是四叉树分解的dim * dim 大小块的个数
% setblk : 用val中相应的dim * dim块的值取代图像 A 的四叉树分解中的每个 % dim *dim 块 [M,N] = size(A);
blocks = find(S == dim)'; numBlocks = length(blocks);
if (~isequal([size(val,1) size(val,2) size(val,3)], [dim dim numBlocks])) if (prod(size(val)) == numBlocks)
val = repmat(val(:)',[dim^2 1]); end end val = val(:);
% 为每一个块算出一个索引 rows = (0:dim-1)'; cols = 0:M:(dim-1)*M; rows = rows(:,ones(1,dim)); cols = cols(ones(dim,1),:); ind = rows + cols; ind = ind(:);
% 依照索引进行替换%%%%% blocks = blocks(ones(length(ind),1),:); ind = ind(:, ones(1,numBlocks)); ind = ind + blocks; B = A; B(ind) = val;
因篇幅问题不能全部显示,请点此查看更多更全内容