Currently, I am using the code below to segment an image into a grid of cellSizeX pixels times cellSizeY pixels:
grid = zeros(cellSizeX, cellSizeY, ColorCount, cellTotalX, cellTotalY);
for i = 1:cellSizeX:(HorRowCount)
for j = 1:cellSizeY:(VertColumnCount)
try
grid(:,:,:,icount, jcount) = img(i:i+cellSizeX-1, j:j+cellSizeY-1, :);
catch
end
jcount = jcount + 1;
end
icount = icount + 1;
jcount = 1;
end
While this code runs fine and satisfactorily, there are things that nag me:
Via some testing with tic and toc, comparing switching index positions such as grid(:,:,:,icount,jcount) and grid(icount,jcount,:,:,:), I see that grid(:,:,:,icount,jcount) is fastest. But can anything be improved here?
The code will work only if the requested cellSizeX and cellSizeY are proportional to the image img. So requesting cellSizeX and cellSizeY of 9 x 9 on image with size 40 x 40 will result in matlab complaining about exceeding matrix's dimension. Any suggestion regarding this? I do not want to simply fill in blank area for those cells. These cells will be used further in Vlfeat SIFT.