Question

I am trying to output mean and standard deviation for each channel in the object located in the image. At the moment, my code produce mean and standard RGB as one figure, but instead, I would like to output mean and standard for R, G, B individualy.

Sample data: https://drive.google.com/file/d/0B_M7fjkKw1r3ZnM3N0I1aGkzbjA/edit?usp=sharing

This is my code:

function findspuds( image )
%findspuds function locates object boundaries and displays details in Command
%Windows

%read image
orginalImage = imread(image);

%convert to grayscale
grayImage = rgb2gray(orginalImage);

%convert to black/white with low threshold
blackWhiteImage = im2bw(grayImage,6/255);

%reverse binary values
reversedBlackWhiteImage = imcomplement(blackWhiteImage);

%compute distance transform to the nearest 0 pixel in the binary image
calReversedBlackWhiteImage = bwdist(reversedBlackWhiteImage);

%reverse back binary values 
reversed2BlackWhiteImage = imcomplement(calReversedBlackWhiteImage);

%suppress pixel depth minimum
shallowImage = imhmin(reversed2BlackWhiteImage,10);

%compute a label matrix of watershed regions
waterImage = watershed(shallowImage);

%use watershed matrix to separate the touching objects in the binary image
blackWhiteImage(waterImage == 0) = 0;

%remove elements with fewer then 500 pixels
clnImage = bwareaopen(blackWhiteImage,500);

%remove holes in objects
filledImage = imfill(clnImage,'holes');

%get object boundaries
objectBoundries = bwboundaries(filledImage);

%label each object to get its measurements
labeledImage = bwlabel(filledImage);     

%get all the object properties
objectMeasurements = regionprops(labeledImage, grayImage, 'all'); 

%align object labels in the centre of the object
labelShiftX = -7;

%display object boundaries on original image
imshow(orginalImage)
hold on

%print table header line in the command window
fprintf(1,'Object #  Centroid         Size      Major/minor axis     Circularity    Mean/standard deviation\n');

%for each labelled object...
for thisObject = 1:length(objectBoundries)

    %get and print its boundries
    boundary = objectBoundries{thisObject}; 
    plot(boundary(:,2), boundary(:,1), 'b', 'LineWidth', 2)

    %get centroid
    objectCentroid = objectMeasurements(thisObject).Centroid;

    %get area
    objectSize = objectMeasurements(thisObject).Area;

    %get major axis length
    objectMajorAxisLength = objectMeasurements(thisObject).MajorAxisLength;

    %get minor axis length
    objectMinorAxisLength = objectMeasurements(thisObject).MinorAxisLength;

    %get circularity
    objectCircularity = objectMeasurements(thisObject).Perimeter;

    %get list of pixels in current object
    objectPixels = objectMeasurements(thisObject).PixelIdxList;  

    %get mean intensity of grayImage
    objectMean = mean(grayImage(objectPixels)); 

    %get standard intensity of grayImage
    objectStandard = std2(grayImage(objectPixels)); 

    %print object properties
    fprintf(1,'#%2d %8.1f %8.1f %10.1f %10.1f  %8.1f %12.1f %15.1f  %8.1f\n', thisObject, objectCentroid, objectSize, objectMajorAxisLength, objectMinorAxisLength, objectCircularity, objectMean, objectStandard);

    %print object labels
    text(objectCentroid(1) + labelShiftX, objectCentroid(2), num2str(thisObject), 'FontSize', 14, 'FontWeight', 'Bold', 'Color','w');

end

end
Was it helpful?

Solution

mask = false(size(grayImage));
mask(objectPixels) = true;

%on R channel
tmp = orginalImage(:,:,1);
objectMeanR = mean(tmp(mask));
objectStandard = std2(tmp(mask));

Similarly you can implement mean and std2 on G and B channels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top