I need to calculate the normalized colour histigram (in HSV colourspace) of an image. Using histcn in Matlab with 8 bins for Hue and 4 bins for each of Saturation and Value I get a 8x4x4 histogram. How could I normalize it?
Yes, #ASantosRibeiro is right, you just need to divide by number of elements like
HSV_hist ./ length(InitialData);
or
HSV_hist ./ sum(HSV_hist(:)); % To count the number of element use to create your hist.
Related
I have a set of 100 datapoints that I have split into 10 bins and displayed as a histogram, however I would like to display it as a relative frequency histogram so I need to scale the height of each bin by 100. How do I do this?
M = readmatrix('Data.csv');
y= M(:,1);
histogram(y,10)
would it be possible to divide the resulting bins in some simple way?
Use the probability normalization,
histogram(y,10,'Normalization','probability')
See the histogram documentation for the other normalizations available.
I have a uint8 matrix
A = (1:512,1:512,1:3)
which contains information about an 512x512 RGB image. I Also have a matrix B which is in the format of
B = (1:512,1:512)
which holds complex numbers. Now I would like to plot every complex number
B(x,y)
on the complex plane with RGB color of
A(x,y,1:3)
How can I achieve that?
1) Make your color matrix of the size Mx3 (where M is the total number of your points):
A=reshape (A,512*512,3);
2) Use Scatter plot:
scatter(real(B(:)), imag(B(:)), [], A/255)
Note that here your colormap should be from 0 to 1. Assuming that your original A contained the values from 0 to 255, you need to divide by the maximum value.
for r=1:512
for c=1:512
plot( B(r,c) ,'Color',reshape(A(r,c,1:3),1,3),'LineStyle','none','Marker','o')
end
end
I couldnt figure out how to make the reshape work in vectorized form without an error. This works though (albeit very slowly)!
I have three variables, e.g., latitude, longitude and temperature. For each latitude and longitude, I have corresponding temperature value. I want to plot latitude v/s longitude plot in 5 degree x 5 degree grid , with mean temperature value inserted in that particular grid instead of occurring frequency.
Data= [latGrid,lonGrid] = meshgrid(25:45,125:145);
T = table(latGrid(:),lonGrid(:),randi([0,35],size(latGrid(:))),...
'VariableNames',{'lat','lon','temp'});
At the end, I need it somewhat like the following image:
Sounds to me like you want to scale your grid. The easiest way to do this is to smooth and downsample.
While 2d histograms also bin values into a grid, using a histogram is not the way to find the mean of datapoints in a smooth grid. A histogram counts the occurrence of values in a set of ranges. In a 2d example, a histogram would take the input measurements [1, 3, 3, 5] and count the number of ones, the number of threes, etc. A 2d histogram will count occurrences of pairs of numbers. (You might want to use histogram to help organize a measurements taken at irregular intervals, but that would be a different question)
How to smooth and downsample without the Image Processing Toolbox
Keep your data in the 2d matrix format rather than reshaping it into a table. This makes it easier to find the neighbors of each grid location.
%% Sample Data
[latGrid,lonGrid] = meshgrid(25:45,125:145);
temp = rand(size(latGrid));
There are many tools in Matlab for smoothing matrices. If you want to have the mean of a 5x5 window. You can write a for-loop, use a convolution, or use filter2. My example uses convolution. For more on convolutional filters, I suggest the wikipedia page.
%% Mean filter with conv2
M = ones(5) ./ 25; % 5x5 mean or box blur filter
C_temp = conv2(temp, M, 'valid');
C_temp is a blurry version of the original temperature variable with a slightly smaller size because we can't accurately take the mean of the edges. The border is reduced by a frame of 2 measurements. Now, we just need to take every fifth measurement from C_temp to scale down the grid.
%% Subsample result
C_temp = C_temp(1:5:end, 1:5:end);
% Because we removed a border from C_temp, we also need to remove a border from latGrid and lonGrid
[h, w] = size(latGrid)
latGrid = latGrid(5:5:h-5, 5:5:w-5);
lonGrid = lonGrid(5:5:h-5, 5:5,w-5);
Here's what the steps look like
If you use a slightly more organized, temp variable. It's easier to see that the result is correct.
With Image Processing Toolbox
imresize has a box filter method option that is equivalent to a mean filter. However, you have to do a little calculation to find the scaling factor that is equivalent to using a 5x5 window.
C_temp = imresize(temp, scale, 'box');
I am working on developing a CBIR system where I am using HSV histogram as one of my features. For each image I want to compute a histogram which has say n1 bins for hue, n2 for saturation and n3 for value. I want a vector which will be n1xn2xn3 dimensional having all possible combinations of these bins.
for example: If I take a tuple (8, 12, 3) bins for hue, saturation and value respectively, then I want to compute a 8x12x3=288 dimensional vector.
In openCV we have the calcHist() function to do so but I failed to find a similar function in matlab.
here is what I have done
%roi1 is my region of interest, y1 is the vector
y1=[imhist(roi1(:,:,1),8)' imhist(roi1(:,:,2),12)' imhist(roi1(:,:,3),3)'];
but y1 would be 23 dimensional rather than the desired 288 dimensional. Please help me on this, if there is a function similar to calcHist() of openCV then suggest me so.
What you have computed is a series of single-dimension histograms, rather than the multi-dimensional histogram. For example, imhist(roi1(:,:,1),8)' is the counts of only the hue values, ignoring corresponding saturation or value of the pixel. There is code on the Mathworks site to compute an n-dimensional histogram, which should provide what you're after: http://www.mathworks.com/matlabcentral/fileexchange/23897-n-dimensional-histogram
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.