How to partition an image to 64 block in matlab

一个人想着一个人 提交于 2019-12-01 00:41:29

Here some pieces of code that I wrote for the exact same problem (8x8 blocks, DCT coefficients, etc) sometime ago...

img=imread('filename')
[img_x,img_y]=size(img);

block_size=8;
slide_len=1;

for ix=block_size/2:slide_len:img_x-block_size/2
    for jy=block_size/2:slide_len:img_y-block_size/2
        current_block=img((ix-block_size/2+1):(ix+block_size/2),(jy-block_size/2+1):(jy+block_size/2));
        dct_coeff=reshape(dct2(current_block),1,block_size^2);

        <insert any other code you want to run here>
    end
end

slide_len sets the offset between one block and the next. In this case it offsets by one pixel each time. however, if you want non-overlapping blocks, you should set it to 8. usually in this application, you use some overlaps.

One way to partition your image into blocks and then run some processing on it is to use the built-in function BLOCKPROC (called blkproc in older versions of Matlab).

%# find block length in order to get 64 blocks
imageSize = size(img);
blockLen = round(imageSize(1:2)/8);

%# apply a function to each block
out = blocproc(img,blockLen,@myFunction)

myFunction is the function that you'd like to apply to each block. You can define it as a subfunction of your code, or a separate m-file, or an anonymous function. The output will be catenated in an 8x-by-8x array, where x is the size of the output of your function. myFunction should expect a single input argument, blockStruct, which is a structure with fields data containing the pixel values of the block, as well as fields border, blockSize, imageSize, and location.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!