How to partition an image to 64 block in matlab

狂风中的少年 提交于 2019-12-09 01:21:58

问题


I want to compute the Color Layout Descriptor (CLD) for each image.. this algorithm include four stages . in the First stage I must Partition each image into 64 block i(8×8)n order to compute a single representative color from each block .. I try to partition the image into 64 block by using (For loop) but I get 64 ting image. I want to get image with (8×8) block in order to complete the algorithm by apply the DCT transformation then Zigzag scanning


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5207960/how-to-partition-an-image-to-64-block-in-matlab

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