我正在做一个基本的图像压缩。我想在图像中获取 8x8 窗口,然后将该窗口进一步细分为 2x2,然后必须找到它们的平均值。完成此操作后,我必须将此平均值矩阵与阈值平均值(整个图像的平均值或该 8x8 窗口的平均值)进行比较。如果元素大于或等于阈值,如果不是 0,则应分配 1。我为此编写了下面的代码。但我被困在第一步本身。它在这一行显示错误“索引超出矩阵尺寸”P=J(i:(i+7),j:(j+7));
请帮助,我不确定如何比较和分配 0 和 1 到最终矩阵。我试过了X=bsxfun(@ge,M,thr)
..它不工作..我的代码似乎很大..我是在写吗?请建议。我认为它可以变得更简单。我是matlab新手,请帮助我学习。这是我的代码:
I=imread('C:\Users\Prem\Documents\MATLAB\mandrill.jpg');
G=rgb2gray(I);
J=imresize(G,[256 256]);
thr=mean(J(:));
[m,n]=size(J); % Reading the size of the image
for i=1:m
for j=1:n
P=J(i:(i+7),j:(j+7)); % Reading 8x8 window
% Sub dividing the 8 x 8 window into four 4x4 sub windows
tl = P(1:4, 1:4); % top left sub-window
tr = P(1:4, 5:8); % top right sub-window
bl = P(5:8, 1:4); % bottom left sub-window
br = P(5:8, 5:8); % bottom right sub-window
% Sub dividing the 4 x 4 window into four 2x2 sub windows
newtl_1 = tl(1:2, 1:2); % top left sub-window
newtr_1 = tl(1:2, 3:4); % top right sub-window
newbl_1 = tl(3:4, 1:2); % bottom left sub-window
newbr_1 = tl(3:4, 3:4); % bottom right sub-window
% Sub dividing the 4 x 4 window into four 2x2 sub windows
newtl_2 = tr(1:2, 1:2); % top left sub-window
newtr_2 = tr(1:2, 3:4); % top right sub-window
newbl_2 = tr(3:4, 1:2); % bottom left sub-window
newbr_2 = tr(3:4, 3:4); % bottom right sub-window
% Sub dividing the 4 x 4 window into four 2x2 sub windows
newtl_3 = bl(1:2, 1:2); % top left sub-window
newtr_3 = bl(1:2, 3:4); % top right sub-window
newbl_3 = bl(3:4, 1:2); % bottom left sub-window
newbr_3 = bl(3:4, 3:4); % bottom right sub-window
% Sub dividing the 4 x 4 window into four 2x2 sub windows
newtl_4 = br(1:2, 1:2); % top left sub-window
newtr_4 = br(1:2, 3:4); % top right sub-window
newbl_4 = br(3:4, 1:2); % bottom left sub-window
newbr_4 = br(3:4, 3:4); % bottom right sub-window
% median values of the four sub windows
m1=mean(newtl_1(:));
m2=mean(newtr_1(:));
m3=mean(newbl_1(:));
m4=mean(newbr_1(:));
% median values of the four sub windows
m5=mean(newtl_2(:));
m6=mean(newtr_2(:));
m7=mean(newbl_2(:));
m8=mean(newbr_2(:));
% median values of the four sub windows
m9=mean(newtl_3(:));
m10=mean(newtr_3(:));
m11=mean(newbl_3(:));
m12=mean(newbr_3(:));
% median values of the four sub windows
m13=mean(newtl_4(:));
m14=mean(newtr_4(:));
m15=mean(newbl_4(:));
m16=mean(newbr_4(:));
M=[m1 m2 m3 m4; m5 m6 m7 m8; m9 m10 m11 m12; m13 m14 m15 m16];
%X=bsxfun(@ge,M,thr)
end
end
imshow(M)