how to find mean for the values in an image

i have to divide my image into n number of small 2X2 matrix and find the mean value and replace that 2x2 matrix with the mean value . example
x=
[2 3 4 5
2 3 4 5
1 2 3 4
1 2 3 4]
i have to find the mean of
[2 3
2 3]
similarly for others and replace that concerned matrix with that mean value
the output should get is
[2 2 4 4
2 2 4 4
1 1 3 3
1 1 3 3]

답변 (3개)

Wayne King
Wayne King 2013년 7월 20일
편집: Wayne King 2013년 7월 20일

0 개 추천

Do you have the Image Processing Toolbox? You can use blockproc(), but I'm not sure how you are getting your expected output above. You say you need the mean of 2x2 chunks submatrices like [2 3; 2 3]. Do you mean that you want the column means of those, which are 2 and 3, or you want the mean of all 4 elements which is 2.5? How are you taking the mean and ending up with a matrix the same size as the original?
I'll show you how to use blockproc() to return the column means for each 2x2 submatrix in x. That means for each 2x2 submatrix, you'll get back a 1x2 with the two column means. Accordingly, a 4x4 matrix reduces to a 2x4.
fun = @(block_struct) mean(block_struct.data);
Y = blockproc(x,[2 2],fun);
If you want the overall mean of each 2x2 submatrix, giving you a 2x2 matrix at the end:
fun = @(block_struct) mean(block_struct.data(:));
Y = blockproc(x,[2 2],fun);

댓글 수: 5

Sharen H
Sharen H 2013년 7월 20일
Thanks a lot
i am getting an error when i execute
The error message is
? Undefined function or method 'blockproc' for input arguments of type 'function_handle'.
Wayne King
Wayne King 2013년 7월 20일
"Do you have the Image Processing Toolbox? You can use blockproc(),..."
It sounds like you do not have the Image Processing Toolbox.
Sharen H
Sharen H 2013년 7월 20일
i am using matlab 7.6 .i have image processing tool box the command here is blkproc()
Wayne King
Wayne King 2013년 7월 20일
I think you do not have the blockproc() function from the Image Processing Toolbox, blockproc() was only added to the Image Processing Toolbox in R2009b, which is 7.9
Sharen H
Sharen H 2013년 7월 20일
okok thank you

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2013년 7월 20일
편집: Andrei Bobrov 2013년 7월 23일

0 개 추천

for youe case without blockproc (without Image Processing Toolbox):
kron(cellfun(@mean2,mat2cell(x,2*[1 1],2*[1 1])),[1 1; 1 1])
or use Peter J. Acklam' idea. Thanks to the cyclist for this link.
m = [2 2]; % size your small block
s = size(x);
n = s./m;
a = permute(reshape(x,m(1),n(1),n(2),[]),[1 3 2 4]);
out = kron(reshape(mean(reshape(a,[],prod(m))),n(1),[]),ones(n))
use function blkproc
sizeblock = [2 2];
Y = blkproc(x,sizeblock,@mean2);
out = kron(Y,ones(size(x)./sizeblock));

댓글 수: 2

Sharen H
Sharen H 2013년 7월 23일
can u please help me to generalize as [n,n] matrix
Sharen H
Sharen H 2013년 7월 23일
when i try for a [3,3] small block i am not able to process...

댓글을 달려면 로그인하십시오.

Sujit Singh
Sujit Singh 2017년 3월 15일

0 개 추천

We can assume given image size is 4X4 and you want to divide and calculate the mean of 2X2 block size and then get the mean processed image, so for this we can use blkproc in your case: 1.Img=imread('Img.tiff')% Read the image 2.meanFilterFunction = @(x)mean2(x(:))*ones(size(x),class(x)); % this is mean function 3. BS=[2 2] % block size 2X2 4.Mean_image = blkproc(Img, BS, meanFilterFunction); 5. imshow(Mean_image, []);
I hope this will help you , now you can change the size of image and size of block also .

태그

질문:

2013년 7월 20일

답변:

2017년 3월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by