i want to calculate the color features of soap only for that i mask BW ROI on an RGB soap image , i did the folowing code but the values are different when i calculte the color features byi croping only the soap image(with out the background)?Hlp
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
solomon adenew
le 9 Août 2014
Modifié(e) : solomon adenew
le 16 Août 2014
I want to calculate the color features of soap only for that i mask BW ROI on an RGB soap image , i did the following code but the values are different when i calculate the color features by cropping only the soap image (without the background)? Hlp
 
clear all
clear
RGB=imread('soap170.jpg');
I=rgb2gray(RGB);
% Step 2: Create a Binary Image
level=graythresh(I);
BWgth = im2bw(I,level);
maskThreeChannel = repmat(BWgth , [1, 1, 3]);
RGB2=RGB;
RGB2(~maskThreeChannel)=0;
figure,imshow(RGB2); title('masked RGB')
R = double(RGB2(:, :, 1));
G = double(RGB2(:, :, 2));
B = double(RGB2(:, :, 3));
% compute 3 first color moments from each channel
meanR = mean( R(:) );
stdR = std( R(:) );
skewnessR = skewness(R(:));
meanG = mean( G(:) );
stdG = std( G(:) );
skewnessG = skewness(G(:));
meanB = mean( B(:) );
stdB = std( B(:) );
skewnessB = skewness(B(:));
% construct output vector
colorMoments = zeros(1, 9);
colorMoments(1, :) = [meanR stdR skewnessR meanG stdG skewnessG meanB stdB skewnessB]
3 commentaires
Réponse acceptée
Yu Jiang
le 13 Août 2014
Modifié(e) : Yu Jiang
le 14 Août 2014
Hi Solomon
From what I understand, you would like to perform some masking operation on the original figure and would like to compare its color features with another figure, which is obtained by removing the background from the original figure. The issue you are facing is that the features of both figures are not the same while you are expecting them to be the same.
It seems that by masking the image, you set the background portion of the matrix RGB2 to be zero. However, RGB2 may not have the same color features as the cropped image. Let us take the channel R for example. The summation of R(:) will be the same as the summation of the corresponding vector R(:) in the cropped figure, since the values for the background are all zeros. However, when you are calculating the mean value of R(:) and its corresponding matrix in the cropped figure, the results will be different because they have different denominators (number of pixels). The same is true for the variance. Therefore, the color features will be different.
On the other hand, it seems that the masking operation you are trying to perform does not work, or at least is not necessary. Indeed, after executing the code you provided, if you try the following command in MATLAB
>> find((RGB2-RGB)~=0)
you will see the returned variable is an empty matrix, indicating RGB2 and RGB are identical. Therefore, the color features you calculated from the “masked” image are in fact the same color features of the original image. But I understand by performing this masking operation, you would like to assure that the figure’s background is purely black.
To help you achieve your goal, I would like to suggest you get rid of the zero elements in R, G, and B before calculating the color features, to do this, you may add the following three lines in your code, right before you calculate meanR.
R(R==0)=[];
G(G==0)=[];
B(B==0)=[];
Then, you should have color features similar to the ones from the cropped figure.
-Yu Jiang
1 commentaire
Plus de réponses (1)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!