Asked by Makrim
on 1 May 2015

Suppose I have to fragment of an image J : J_out_1 et J_out_2.

J_out_1 = J(1:h,startj:i);

J_out_2 = J(1:h,k:endj);

I would like to compute the mean of the union of those two regions , is it possible ?

m_out = mean2(J_out_1 union J_out_2);

Thank you in advance

Answer by Guillaume
on 1 May 2015

Edited by Guillaume
on 1 May 2015

Accepted Answer

m_out = mean([J_out_1(:); J_out_2(:)])

would be one way to do it assuming the image has only one colour channel. If they are RGB images:

m_out = mean([reshape(J_out_1, 1, [], 3), reshape(J_out_2, 1, [], 3)])

Note that if the two regions are the same size, you could just concatenate them without any reshaping (by colon or reshape).

Answer by Image Analyst
on 1 May 2015

Why not just take the weighted mean of the two?

numerator = numel(J_out_1) * mean2(J_out_1) + numel(J_out_2) * mean2(J_out_2)

denominator = numel(J_out_1) + numel(J_out_2)

m_out = numerator / denominator

If you want, you could make a binary image and use that as a mask to extract all the pixels in just the two regions:

binaryImage = false(size(J));

binaryImage(1:h,startj:i) = true;

binaryImage(1:h,k:endj) = true;

m_out = mean(J(binaryImage))

Makrim
on 2 May 2015

that's another way, Yes , thank you. by the way the idea of using Logical is also brilliant.

Image Analyst
on 2 May 2015

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.