How to solve the Issue with subimages having odd rows and columns, block error?

1 view (last 30 days)
Image = ('13100.jpg');
Img = imread (Image);
[oldx oldy z] = size(Img);
x = oldx/4;
y = oldy/4;
if mod(x, 2) ~= 0
x = x + 1;
end
if mod(y, 2) ~= 0
y = y + 1;
end
Img2 = uint8(zeros([x*4 y*4 z]));
Img2(1:oldx, 1:oldy, 1:z) = Img(1:oldx, 1:oldy, 1:z);
Img = Img2;
Image = rgb2gray(im2double(Img));
% Initialize the edge detection masks
H = [];
HorizontalMask = [1 1 1;0 0 0;-1 -1 -1];
VerticalMask = [1 0 -1;1 0 -1;1 0 -1];
DiagonalMask = [0 1 1;-1 0 1;-1 -1 0];
AntiDiagMask = [1 1 0;1 0 -1;0 -1 -1];
% Perform the filtering using the masks to create the edge images
HImage = imfilter(Image, HorizontalMask,'replicate');
VImage = imfilter(Image, VerticalMask,'replicate');
DImage = imfilter(Image, DiagonalMask,'replicate');
AImage = imfilter(Image, AntiDiagMask,'replicate');
% Determine the size of each subimage dynamically
m = size(Image, 1)/4; % Number of rows per subimage.
n = size(Image, 2)/4; % Number of columns per subimage.
column = 1;row = 1;counter = 1; % Variable initializatio
for i = 1:16 % Loop through every subimage
if counter > 4
column = 1;
row = row + m;
counter = 1;
end
subImgH(i).img = subim(HImage, m, n, row, column); % Get subimage for H
subImgV(i).img = subim(VImage, m, n, row, column); % Get subimage for V
subImgD(i).img = subim(DImage, m, n, row, column); % Get subimage for D
subImgA(i).img = subim(AImage, m, n, row, column); % Get subimage for A
column = column + n;
counter = counter + 1;
end
% Compute the blocks and histograms
try
for i = 1:16 % All subimages
HLocal(i,1:5) = 0;
row = 1; column = 1;
for j = 1:ceil(size(subImgH(i).img,1)*size(subImgH(i).img,2)/4) % All 2x2 blocks
if column > size(subImgH(i).img,2)
column = 1;
row = row + 2;
end
% Determine the max edge of the averages in a 2x2 area
[M, I] = max([sum(sum(abs(subImgH(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgV(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgD(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgA(i).img(row:row+1,column:column+1))))/4 ...
Threshold]);
IndexedSub(i).img((row + 1)/2,(column + 1)/2) = I;
HLocal(i,I) = HLocal(i,I) + 1;
column = column + 2;
end
HLocal(i,:) = HLocal(i,:)./(ceil(size(subImgH(i).img,1)*size(subImgH(i).img,2)/4));
end
catch
error('Issue with subimages having odd rows and columns, block error.')
end
It showing me, error('Issue with subimages having odd rows and columns, block error.') How to solve it? Any mistake? Or I need to remove odd rows and columns for subimages?
function s = subim(f, m, n, rx, cy)
% SUBIM Extracts a subimage, s, from a given image, f.
% The subimage is of size m-by-n, and the coordinates of its top,
% left corner are (rx, cy).
s = zeros(m, n);
rowhigh = rx + m - 1;
colhigh = cy + n - 1;
xcount = 0;
for r = rx:rowhigh
xcount = xcount + 1;
ycount = 0;
for c = cy:colhigh
ycount = ycount + 1;
s(xcount, ycount) = f(r, c);
end
end

Answers (1)

Image Analyst
Image Analyst on 13 Apr 2017
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_split_an_image_into_non-overlapping_blocks.3F It can handle images that are not a multiple of the block size.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!