how to divide an image into 4*4 blocks and find the mean of each block ?

we are doing our graduation project on depth map generation.we have read an coloured image and converted it into a grsay scale.next we have to divide the image into 4*4 blocks and find the mean of each block.

 Réponse acceptée

Img = rand(640, 480); % Test data
Img = reshape(Img, 4, 160, 4, 120);
meanImg = sum(sum(Img, 1), 3) / 16;
meanImg = squeeze(meanImg)

4 commentaires

meanImg = sum(sum(Img, 1), 3) / 16; wat does this statement do??
maria
maria le 16 Jan 2013
Modifié(e) : maria le 16 Jan 2013
thanks a lot.. can u pls tell me how to find the mean of each 4*4 block after the following codes
clc;clear all;close all;
i=importdata('C:\Users\Public\Pictures\Sample Pictures\Koala.jpg'); Img=rgb2gray(i);
[r c]=size(Img);
x=r/4;y=c/4;
Img = reshape(Img, 4, x, 4, y);
meanImg = sum(sum(Img, 1), 3) / 16;
meanImg = squeeze(meanImg);
@Maria: This statement calculates the sum twice, at first along the 1st dimension, than along the 3rd dimension. You can test this step by step using a rand(8,8) matrix.
I do not understand, what you want to get. What does "group" exactly mean here?
how to find the differnce between all the adjacent elements in an array???

Connectez-vous pour commenter.

Plus de réponses (2)

Using this
meanImg = cellfun(@(c) mean(c(:)), mat2tiles(rgb2gray(Img),[4,4]) );

2 commentaires

maria
maria le 16 Jan 2013
Modifié(e) : maria le 16 Jan 2013
i am not able to acess MAT2TILES fn and wat is 'c'?

Connectez-vous pour commenter.

imshow(blkproc(rgb2gray(I), round([size(I,1) size(I, 2)]/4), ...
@(x) mean2(x)*ones(size(x)) ))

Community Treasure Hunt

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

Start Hunting!

Translated by