我正在进行基本的图像压缩。我想在图像中采用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)
答案 0 :(得分:0)
当循环中的i
从1变为图像大小时,i+7
将最终对应于超出图像大小的值。调整循环的限制(到imageSize-7
),或在运行代码之前填充图像。
如果要创建一个具有大于或等于阈值的数组的数组,否则为零,则编写X = M>thr
。这是Matlab的一个很好的功能。
请注意,mean
取平均值,median
取中位数。
答案 1 :(得分:0)
最后,我在matlab中使用blockproc函数完成了它。点击下面的链接查看代码。
How to store data out of the loop in MATLAB- Image Compression