Easiest way to find the mean of a group of pixels in an image

3 views (last 30 days)
I am attempting to analyze an image of a scanned thin section where I have isolated crystals of a certain chemical composition. My goal is to define groups of pixels at the cores of these crystals in order to find the mean value of them. Using the mean values I hope to create a frequency distribution so that I can define crystal populations based on the groupings in the distribution.
So far, I have been struggling with the "inpolygon" function to define groups of pixels. My attempts have gone along the lines of:
>a = imread('image.jpg');
>imagesc(a) %Here I zoom into my area of interest (the crystal core)
>aa = xlim; %Gives me [xmin,xmax]
>ab = ylim; %Gives me [ymin,ymax]
>ac = inpolygon(xmin,ymin,xmax,ymax)
This is as far as I've gotten. "ac" has been returning as 0, and I don't know where to go from here. Any advice would be greatly appreciated.
  1 Comment
Apatschinn
Apatschinn on 8 Dec 2015
UPDATE:
I have reviewed the input arguments definitions (<http://www.mathworks.com/help/matlab/ref/inpolygon.html#zmw57dd0e373179>) and I have realized that I am totally off. So I made another attempt.
>ac = inpolygon(aa,ab,aa,ab);
I tried defining it this way because my query points are the same as my polygon points. I got a 1x4 vector of ones (yay, locicals).
So now do I just define a matrix with the xlim,ylim points and then just find the mean of it?

Sign in to comment.

Answers (1)

Chad Greene
Chad Greene on 8 Dec 2015
Try this:
% Here's an image:
Z = double(imread('tire.tif'));
% It has these corresponding rows and columns:
[cols,rows] = meshgrid(1:size(Z,2),1:size(Z,1));
% Perhaps you're interested in all the stuff in this polygon:
coli = [50 53 120 170];
rowi = [40 140 141 60];
% These are the indices of all the values inside the polygon:
ind = inpolygon(cols,rows,coli,rowi);
% Get the mean of the Z values inside the polygon like this:
mean(Z(ind))
image(Z)
colormap(gray)
hold on
plot(coli,rowi,'go-')
% Or you could set all the Z values inside the polygon to zero:
Z(ind) = 0;

Categories

Find more on Crystals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!