Maayan
Maayan

Reputation: 149

Calculate the average of part of the image

How can i calculate the average of a certain area in an image using mat-lab? For example, if i have an intensity image with an area that is more alight and i want to know what is the average of the intensity there- how do i calculate it? I think i can find the coordinates of the alight area by using the 'impixelinfo' command. If there is another more efficient way to find the coordinates i will also be glad to know. After i know the coordinates how do i calculate the average of part of the image?

Upvotes: 0

Views: 3391

Answers (1)

nkjt
nkjt

Reputation: 7817

You could use one of the imroi type functions in Matlab such as imfreehand

I = imread('cameraman.tif');
h = imshow(I);
e = imfreehand;
% now select area on image - do not close image

% this makes a mask from the area you just drew
BW = createMask(e);

% this takes the mean of pixel values in that area
I_mean =  mean(I(BW));

Alternatively, look into using regionprops, especially if there's likely to be more than one of these features in the image. Here, I'm finding points in the image above some threshold intensity and then using imdilate to pick out a small area around each of those points (presuming the points above the threshold are well separated, which may not be the case - if they are too close then imdilate will merge them into one area).

se = strel('disk',5);
BW = imdilate(I>thresh,se);
s = regionprops(BW, I, 'MeanIntensity');

Upvotes: 1

Related Questions