How to find standard deviation of image in MATLAB - matlab

I have an image and I want to find the standard deviation for each row of the image and I will use the SD value for each row to calculate average SD of the image. I know the function to find SD (std) but I have no idea how to start/to do.

Images in MATLAB are still just matrices. Since you want to take the standard deviation for the rows, you can use std(A,0,2) to take the standard deviations along rows. Then you can use std once more on the resulting vector to get what I think you are looking for.

If you have a greyscale image, use
mean(std(img.'))
If you have a color image (i.e., ndims(img) == 3), you'll have to repeat the above on each page of the array:
squeeze( mean(std(img,0,2)) )
which will result in the mean standard deviation of each row of each colour layer.

Instead of calculating the std of each row and then taking the mean, isn't it more accurate (and simpler) to just calculated the std of the entire image (all pixel values)? that is:
std(img(:));
Taking the std of each row and then taking the mean is not exactly the std of all the pixel value of the image...
For example:
>> a=peaks(100);
>> mean(std(a.'))
ans =
1.4223
>> std(a(:))
ans =
1.8882

Related

Sum of MATLAB Gaussian distribution of an image is greater than 1

I am using the below code to calculate the probabilities of pixel intensities for the image given below. However, the total sum of probabilities sum(sum(probOfPixelIntensities)) is greater than 1.
I'm not sure where the mistake may be. Any help in figuring this out would be greatly appreciated. Thanks in advance.
clear all
clc
close all
I = imread('Images/cameraman.jpg');
I = rgb2gray(I);
imshow(I)
muHist = 134;
sigmaHist = 54;
Iprob = normpdf(double(I), muHist, sigmaHist);
sum(sum(Iprob))
What you are doing is computing the PDF values for every pixel in the image. Iprob is not a normal distribution but you are simply using the image pixels to sample from the distribution of a known mean and standard deviation.
Essentially, you are just performing a data transformation where the image pixel intensities get mapped to values on a normal PDF with a known mean and standard deviation. This is not the same as a PDF and that's why the sum is not 1. On top of this, the image pixel intensities don't even follow a normal distribution itself so there wouldn't be any way that the sum of the distribution is 1.
Not much more to say other than the output of normpdf is not what you are expecting it to be. You should opt to read the documentation of normpdf more carefully: http://www.mathworks.com/help/stats/normpdf.html
If it is your desire to determine the actual PDF of the image, what you need to do is find the histogram of the image, and not do a data transformation. You can do that with imhist. Once you do that, assuming that encountering the intensities is equiprobable, you would divide each histogram entry by the total size of the image and then sum along all bins. You should get the sum to be 1 in this case.
Just to verify, let's use the image you provided in your post. We'll read this in from StackOverflow. Once we do that, compute the PDF and then sum over all bins:
%// Load in image
im = rgb2gray(imread('http://i.stack.imgur.com/0XiU5.jpg'));
%// Compute PDF
h = imhist(im) / numel(im);
%// Sum over all bins
fprintf('Total sum over all bins is: %f\n', sum(h));
We get:
Total sum over all bins is: 1.000000
Just to be absolutely sure you understand, this is the PDF of the image. What you did before was perform a data transformation where you transformed all image pixel intensities that conforms to a Gaussian distribution with a known mean and standard deviation. This will not give you a sum of 1 as you expect.
Remember that PDF is only the probability density function $p(x)$. Function which is restricted to range $[0, 1]$ is the integral over all domain of that function $\int_D p(x)dx$.
Refer to the Matlab manual, Y = normpdf(X,mu,sigma) computes the pdf at each of the values in X using the normal distribution with mean mu and standard deviation sigma.
The sum of the pdf is equal to 1.
The sum of the output is not.

Image Parameters (Standard Deviation, Mean and Entropy) of an RGB Image

I couldn't find an answer for RGB image.
How can someone get a value of SD,mean and Entropy of RGB image using MATLAB?
From http://airccse.org/journal/ijdms/papers/4612ijdms05.pdf TABLE3, it seems he got one answer so did he get the average of the RGB values?
Really in need of any help.
After reading the paper, because you are dealing with colour images, you have three channels of information to access. This means that you could alter one of the channels for a colour image and it could still affect the information it's trying to portray. The author wasn't very clear on how they were obtaining just a single value to represent the overall mean and standard deviation. Quite frankly, because this paper was published in a no-name journal, I'm not surprised how they managed to get away with it. If this was attempted to be published in more well known journals (IEEE, ACM, etc.), this would probably be rejected outright due to that very ambiguity.
On how I interpret this procedure, averaging all three channels doesn't make sense because you want to capture the differences over all channels. Doing this averaging will smear that information and those differences get lost. Practically speaking, if you averaged all three channels, should one channel change its intensity by 1, and when you averaged the channels together, the reported average would be so small that it probably would not register as a meaningful difference.
In my opinion, what you should perhaps do is treat the entire RGB image as a 1D signal, then perform the mean, standard deviation and entropy of that image. As such, given an RGB image stored in image_rgb, you can unroll the entire image into a 1D array like so:
image_1D = double(image_rgb(:));
The double casting is important because you want to maintain floating point precision when calculating the mean and standard deviation. The images will probably be of an unsigned integer type, and so this casting must be done to maintain floating point precision. If you don't do this, you may have calculations that get saturated or clamped beyond the limits of that data type and you won't get the right answer. As such, you can calculate the mean, standard deviation and entropy like so:
m = mean(image_1D);
s = std(image_1D);
e = entropy(image_1D);
entropy is a function in MATLAB that calculates the entropy of images so you should be fine here. As noted by #CitizenInsane in his answer, entropy unrolls a grayscale image into a 1D vector and applies the Shannon definition of entropy on this 1D vector. In a similar token, you can do the same thing with a RGB image, but we have already unrolled the signal into a 1D vector anyway, and so the input into entropy will certainly be well suited for the unrolled RGB image.
I have no idea how the author actually did it. But what you could do, is to treat the image as a 1D-array of size WxHx3 and then simply calculate the mean and standard deviation.
Don't know if table 3 is obtain in the same way but at least looking at entropy routine in image toolbox of matlab, RGB values are vectorized to single vector:
I = imread('rgb'); % Read RGB values
I = I(:); % Vectorization of RGB values
p = imhist(I); % Histogram
p(p == 0) = []; % remove zero entries in p
p = p ./ numel(I); % normalize p so that sum(p) is one.
E = -sum(p.*log2(p));

matlab code for perceptual hashing

I need a matlab code for a perceptual hashing algorithm descried here:
http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
Basically I want this to remove deatails in an image and only leave the major structure components information.
To do so, I think I need the following steps:
1. Reduce the DCT. Suppose the DCT is 32x32 (), just keep the top-left 8x8. Those represent the lowest frequencies in the picture.
Compute the average value. Like the Average Hash, compute the mean DCT value (using only the 8x8 DCT low-frequency values and excluding the first term since the DC coefficient can be significantly different from the other values and will throw off the average).
Further reduce the DCT. Set the 64 hash bits to 0 or 1 depending on whether each of the 64 DCT values is above or below the average value. The result doesn't tell us the actual low frequencies; it just tells us the very-rough relative scale of the frequencies to the mean. The result will not vary as long as the overall structure of the image remains the same; this can survive gamma and color histogram adjustments without a problem.
reconstruct image after the processing.
Anyone can help on any one of above steps?
I have tried some code that gives some results (in the below link), it is not yet perfect:
https://stackoverflow.com/questions/26748051/extract-low-frequency-from-dct-coeffecients-of-an-image-in-matlab
Try this:
% read image
I = imread('cameraman.tif');
% cosine transform and reduction
d = dct2(I);
d = d(1:8,1:8);
% compute average
a = mean(mean(d));
% set bits, here unclear whether > or >= shall be used
b = d > a;
% maybe convert to string:
string = num2str(b(:)');

How to calculate per-page STD in matlab?

Suppose I have matrix A of 100x200x300. Third dimension is called "page" in Matlab and this matrix has 300 pages then.
Now I want to calculate standard deviation within each page and get a result matrix of 1x1x300.
I can't just do
std(std(A,0,1),0,2)
because normalization will be wrong as I think.
You need to collapse the first two dimensions into one (i.e. into columns) using reshape; and then compute std along each column:
Ar = reshape(A, size(A,1)*size(A,2), size(A,3));
result = std(Ar);
This will give you a 1x300 vector as result. If you really need it to be 1x1x300, use
result = shiftdim(result, -1);

how to find standard deviation of noise in an image using matlab?

As a part of my project, initially find low resolution of the input image. Then as a second step i need find the noise in the low-resolution image. How to find noise in an image and its standard deviation using matlab?
You can use the std matlab function which returns the standard deviation of a matrix.
std_deviation = std(image);
This will give you the standard deviation of the whole image. However you cannot calculate the noise std since you don't have the original filtered image.
Possible solution: (Not accurate) : This suppose thaht your noise is gaussian
Well, you can render several Noise matrices and test them:
(choose your mean_vector and std_vector)
for i = 1 : length(mean_vector) % or length(std_vector)
Noise(:,:,i) = mean_vector(i) + std_vector(i).*randn(size(your_image))
% extracting the possibly filtered image
filtered_img(:,:,i) = your_image - Noise(:,:,i);
end
Then display every filtered_img and choose the one that looks the less noisy.
You can denoise the image, compute the difference between the raw image and the denoised version, and then compute the standard deviation of the difference.
For instance:
a=imread('input');
a=double(a);
b=imsharpen(a); %you may need to tune the parameters
diff=b-a;
noise=std2(diff);
You can find the variance of noise in the image assuming that you know the distribution. You will know this if you have read some of the great works in image denoising field by Donoho & John.
To find the noise std. dev. of noise in an image with Gaussian contamination (additive), you can use the Median Absolute Deviation (MAD) estimator on the derivative of the image using the following kernel:
I am writing the python code for this, you can easily write it in Matlab:
def find_stddevs(img):
k = np.asmatrix([[-1.0/9, -1.0/9 ,-1.0/9],
[-1.0/9, 8.0/9 , -1.0/9],
[-1.0/9, -1.0/9 ,-1.0/9]])
filtered = convolve(img,k,mode='reflect')
median_a = np.median(filtered)
stddev = np.median(np.absolute(filtered - median_a))/0.67449 #Gaussian noise assumption
return stddev
You can see the derivation and the logic Here on Wikipedia.
Again, most people think it can be done. This is true iff you do not have any prior about the type of contamination in the image.