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.

 Accepted Answer

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

4 Comments

meanImg = sum(sum(Img, 1), 3) / 16; wat does this statement do??
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???

Sign in to comment.

More Answers (2)

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

Asked:

on 16 Jan 2013

Community Treasure Hunt

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

Start Hunting!