Question

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:

  1. 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?

  2. 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.

Was it helpful?

Solution

  1. How about converting the image into a cellarray with each cell of size CellSizeX x CellSizeY x ColorCount, then stacking all these cells to a single array grid?

    ca = mat2cell( img, cellSizeY * ones(1, cellTotalY), ...
    cellSizeX * ones(1, cellTotalX), ...
    ColorCount );
    grid = reshape( cat( 4, ca{:} ),...
    cellSizeX, cellSizeY, ColorCount, cellTotalX, cellTotalY);

  2. It is accustomed in the image processing community to pad image with non-zero values depending on the values of the image at the boundary. Look at the function padarray for more information. You may pad your input image such that its padded size will be proportional to CellSizeX and CellSizeY (padding does not have to be identical at both axes).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top