Easiest way to find the mean of a group of pixels in an image
Show older comments
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
on 8 Dec 2015
Answers (1)
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!