【问题标题】:How to get mean and standard deviation of each channel (RGB) in an image如何获得图像中每个通道(RGB)的均值和标准差
【发布时间】:2014-03-15 06:18:03
【问题描述】:

我正在尝试输出图像中对象中每个通道的均值和标准差。目前,我的代码将平均值和标准 RGB 生成为一个数字,但相反,我想分别输出 R、G、B 的平均值和标准。

样本数据:https://drive.google.com/file/d/0B_M7fjkKw1r3ZnM3N0I1aGkzbjA/edit?usp=sharing

这是我的代码:

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

【问题讨论】:

    标签: matlab image-processing rgb mean standard-deviation


    【解决方案1】:
    mask = false(size(grayImage));
    mask(objectPixels) = true;
    
    %on R channel
    tmp = orginalImage(:,:,1);
    objectMeanR = mean(tmp(mask));
    objectStandard = std2(tmp(mask));
    

    同样,您可以在 G 和 B 通道上实现 meanstd2

    【讨论】:

    • 非常感谢先生!再次!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 2020-05-22
    • 2017-04-28
    • 2022-12-06
    • 2012-10-15
    • 2021-06-01
    • 2021-06-17
    • 2016-02-07
    相关资源
    最近更新 更多