Finding mean pixel value within boundaries
5 次查看(过去 30 天)
显示 更早的评论
I am trying to analyse the different NDVI pixel values of several different plants by getting the mean pixel value for each plant. I have used bwboundaries to find the boundaries of all the plants but i was wondering how you get the mean pixel value within each boundary. I have inserted the image and the code i have done to this point.
%%read in original image as grayscale
original = rgb2gray(imread("c:/Users/simon/Documents/ProjIM/Proj Im/ASI/2206-1-3/NDVI_1.png"))
imshow(original)
%% change to type double because of NVDI values and threshold
doubleImage = im2double(original)
threshValue = 0.05
binaryIm = doubleImage > threshValue
binaryIm = imfill(binaryIm,'holes')
imshow(binaryIm)
%%filter out 12 largest areas for 12 plants
filtered = bwareafilt(binaryIm,12,8)
imshow(filtered)
%% get boundaries with bwboundaries
[boundaries , labelled] = bwboundaries(filtered)
%% plot boundaries on original image to check they are correct
numberOfBoundaries = size(boundaries)
imshow(original)
hold on
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off
0 个评论
采纳的回答
Image Analyst
2020-6-24
You can use mean():
[rows, columns] = size(original)
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
x = thisBoundary(:,2);
y = thisBoundary(:,1)
plot(x, y, 'g', 'LineWidth', 2);
mask = poly2mask(x, y, rows, columns);
theMeans(k) = mean(original(mask));
end
Or (much better), you can get the means for each blob from regionprops(), instead of using masks inside the loop:
props = regionprops(binaryIm, original, 'MeanIntensity');
theMeans = [props.MeanIntensity]
2 个评论
Image Analyst
2020-6-24
The line
blackmasked(~BW2) = 0;
is not needed since you're not looking outside the BW2 mask anyway. Get rid of it to save a very tiny bit of time. Plus you can take the imshow(original) and drawnow out of the loop and put it before since it doesn't change at all during the loop.
更多回答(1 个)
Monalisa Pal
2020-6-24
I am not sure whether my answer is the best way to do it but here's an attempt using the concept of connected component labelling:
%% getting mean within boundaries
[labelRegions, numberOfRegions] = bwlabel(filtered, 8); % using 8-connectivity
% Note that numberOfRegions == numberOfBoundaries
regionwiseMeanPixel = zeros(1, numberOfRegions);
for k = 1 : numberOfRegions
mask = (L == k);
region_k = uint8(mask) .* original;
regionwiseMeanPixel(k) = sum(region_k(:)) / sum(mask(:));
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!