Fitting a distribution for array with zeros - matlab

I have data from stimulation of subjects with different intensities (say we have 54 different intensities), and what follows is the percentage of them recognizing the respective stimulation:
x = [0 0 0 0.50 0 0 0 0 0 0 0.5 0 0 0 0 0 0 0 0.125000000000000 0 0.333333333333333 0 0 0.111111111111111 0 0.428571428571429 0 0.285714285714286 0.166666666666667 0 0.1 0 0.400000000000000 0.5 0.4 0.25 0.6 0.727272727272727 0.714285714285714 0.25 0.666666666666667 0.777777777777778 1 0.75 0 1 0.9375 1 1 1 1 1 0.92 0.92]
Say the first index is the weakest stimulation, and the last index the strongest, as visible the stronger the stimulation the more likely the subject recognizes it.
I want to fit now a distribution to these values, to get something called a psychophysical curve (looks like this).
What I have tried is:
pd = fitdist(x,distribution);
but this throws an error, I assume because of the 0's in the x array. What could I do alternatively?

As suggested in
Fit a sigmoid to my data using MATLAB
"I think you can use "Curve fitting" App in Matlab. you can find it in APPS, in "Math, statistics and optimization" section."
What you have to do is define two vector of the same length:
one with the stimuli
one with the respone
After, looking at your file, you can try ,using the "Curve Fitting" app in matlab, to fit a sigmoid.
After pressing the generate code button, matlab will create a fuction that will give the same result.

Related

How do I populate matrix with a vector, considering Matrix as chart and vector as line

Consider following values
result=zeros(11,11);
line=(4:0.4:8);
Imagine result as a 11x11 X-Y chart paper. So initially we have a blank chart paper. As in a chart plot, I want to populate values of line in result matrix so that we get an upward sloping line when we display matrix.
Consider following figure which I want as result.
Here, result matrix can be visualized as chart paper with origin at bottom left corner. Now, for X=1, line(1)=4; for X=2, line(2)=4.4,.. and so on.
I have written following code which serves the purpose.
result=zeros(11,11);
line=(4:0.4:8);
for i=1:length(line)
temp=floor(line(i));
result(length(line)-temp+1,i)=line(i);
end
Is there a more efficient way to implement this solution? (I shall be working with 20000x20000 matrix, so method needs to be fast)
As suggested in comments, Problem Description is as follows:
I have lets say 1000 lines. All of these lines have different slopes and intercept. I know the x range of the lines and y range of the lines. There is not much I can infer from data if I plot these lines simultaneously on a single plot. The resulting image will be something like this:
Not much can be inferred about this plot. However, if I can get this information saved in a big matrix, then I can analyse where maximum lines are passing through at a particular X index and make further analysis accordingly.
Further Details
I am discretinizing Y axis into 1000 equally spaced interval:
sample code as follows:
range=max(data)-min(data);
percent=0.20;
outerRange= max(data)+range*percent - (min(data)-range*percent);
outerRangeValues=min(data)-range*percent:outerRange/1000:max(data)+range*percent;
Even though it is entirely possible that a particularly steep line will pass through 2 or more rows in a single column, I'll only select only one of the rows to be filled by line in a single column. This can be done by taking average of rows values for a particular column and assigning single row to be its value for that column
You can use sub2ind to keep things vectorized and avoid loops.
The idea is to find all the row and column indices which will have to be modified.
For X axis it is easy, it is simply one per column so the X indices will be 1,2,3,...,np.
For the Y axis, you have to bin the line values into the Y grid. Since indices have to be integers, you have to convert your floating point values into integers. For that you can choose between round, floor and ceil. Each will place some values slightly differently, it is up to you to define which rounding method makes sense for your problem.
Once you have your indices [row_indices,column_indices], you convert them to linear indices into the matrix by using sub2ind, then you assign the values of line into these linear indices.
In code:
line=(4:0.4:8); % your input (line vector)
np = numel(line) ; % determine size of matrix/chart
% identify column and row indices to modify
idCol = 1:np ;
idRow = fliplr( round( line ) ) ; % choose "round", "floor" or "ceil"
% build the result
result = zeros(np);
linearInd = sub2ind( [np,np], idRow, idCol ) ;
result(linearInd) = line ;
Gives you:
>> result
result =
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 7.2 7.6 8
0 0 0 0 0 0 6.4 6.8 0 0 0
0 0 0 5.2 5.6 6 0 0 0 0 0
0 4.4 4.8 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

Creating gray-level co-occurrence matrix from 16-bit image

I have a data set of images that are 16-bit and I want to create GLCM matrix from them to extract GLCM features.
However, the resulting matrix shows one value (as shown in the picture below), I wonder why.
I tried using the same image but converted to 8-bit, the resulted GLCM show several values.
Note: I used the following Matlab function:
glcm_matrix = graycomatrix(image.tif);
Here is a cropped sample from the 16-bit image:
Note: The image used in the computations can be downloaded from here. The original image is very low contrast and looks totally dark. The image shown above has its contrast stretched and is intended only for visualization purposes.
EDIT:
I used
glcm_matrix = graycomatrix(image.tif, 'GrayLimits', []);
and it gives me the following results:
It was a binning/scaling problem.
Let's take a peek inside:
edit graycomatrix
In this case we're interested in the two options, 'NumLevels' and 'GrayLimits'
% 'NumLevels' An integer specifying the number of gray levels to use
% when scaling the grayscale values in I. For example,
% if 'NumLevels' is 8, GRAYCOMATRIX scales the values in
% I so they are integers between 1 and 8. The number of
% gray levels determines the size of the gray-level
% co-occurrence matrix (GLCM).
%
% 'NumLevels' must be an integer. 'NumLevels' must be 2
% if I is logical.
%
% Default: 8 for numeric
% 2 for logical
%
% 'GrayLimits' A two-element vector, [LOW HIGH], that specifies how
% the values in I are scaled into gray levels. If N is
% the number of gray levels (see parameter 'NumLevels')
% to use for scaling, the range [LOW HIGH] is divided
% into N equal width bins and values in a bin get mapped
% to a single gray level. Grayscale values less than or
% equal to LOW are scaled to 1. Grayscale values greater
% than or equal to HIGH are scaled to NumLevels. If
% 'GrayLimits' is set to [], GRAYCOMATRIX uses the
% minimum and maximum grayscale values in I as limits,
% [min(I(:)) max(I(:))].
So in other words the function was binning your data into 8x8 bins and assuming that the scaling range was the full uint16 range (0-65535). However that sample image I you gave has a minimum of 305 and a maximum of 769, making it fall into the first bin (0-8192 or so). When I call A = graycomatrix(I) it gives me the following matrix :
A =
6600 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
However when A = graycomatrix(I,'GrayLimits', []) is called the scaling range is taken as min(I) - max(I), and the function works as expected :
A =
4 2 1 0 0 0 0 0
1 1 2 2 0 0 0 0
2 2 4 7 1 0 0 0
0 1 7 142 72 1 0 0
0 0 0 65 1711 252 0 0
0 0 0 0 230 3055 178 0
0 0 0 0 0 178 654 8
0 0 0 0 0 0 8 9
In your original example the single value is in the middle of the 8x8 matrix most likely because your original images are int16 and not uint16, so the graycomatrix is symmetric to take into account the possibility of negative values.
You can also of course scale the original images to fit their datatypes. For example percentile scaling might be a good idea if you expect outliers etc.
I'd just like to build on #Tapio's excellent answer.
The GLCM yielded by graycomatrix when you use the name/value pair GrayLimits', [] in the function call looks good. However, this approach might not be valid for your application. If you compute the GLCMs for a set of images in this way, the same elements of two different GLCMs corresponding to two different images are likely to have a different meaning. Indeed, as the intensity is being rescaled differently for each image, the components of the GLCM are actually encoding different co-occurrences from one image to another.
To avoid this you could first calculate the minimum and maximum intensities over the whole image dataset (for example minImgs and maxImgs) and then use those values to rescale the intensity of all the images that make up the dataset in the exact same way:
glcm_matrix = graycomatrix(image_tif, 'GrayLimits', [minImgs maxImgs]);

How to find centroid of an object in divided image [duplicate]

I have spent all day reading up on the above MATLAB functions. I can't seem to find any good explanations online, even on the MathWorks website!
I would be very grateful if anyone could explain bwlabel, regionprops and centroid. How do they work if applied to a grayscale image?
Specifically, they are being used in this code below. How do the above functions apply to the code below?
fun=#minutie; L = nlfilter(K,[3 3],fun);
%% Termination LTerm=(L==1);
figure; imshow(LTerm)
LTermLab=bwlabel(LTerm);
propTerm=regionprops(LTermLab,'Centroid');
CentroidTerm=round(cat(1,LTerm(:).Centroid));
figure; imshow(~K)
set(gcf,'position',[1 1 600 600]); hold on
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro')
That's quite a mouthful to explain!... nevertheless, I'd love to explain it to you. However, I'm a bit surprised that you couldn't understand the documentation from MathWorks. It's actually quite good at explaining a lot (if not all...) of their functions.
BTW, bwlabel and regionprops are not defined for grayscale images. You can only apply these to binary images.
Update: bwlabel still has the restriction of accepting a binary image but regionprops no longer has this restriction. It can also take in a label matrix that is usually output from bwlabel as well as binary images.
Assuming binary images is what you want, my explanations for each function is as follows.
bwlabel
bwlabel takes in a binary image. This binary image should contain a bunch of objects that are separated from each other. Pixels that belong to an object are denoted with 1 / true while those pixels that are the background are 0 / false. For example, suppose we have a binary image that looks like this:
0 0 0 0 0 1 1 1 0 0
0 1 0 1 0 0 1 1 0 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1
0 0 1 1 1 1 0 0 1 1
You can see in this image that there are four objects in this image. The definition of an object are those pixels that are 1 that are connected in a chain by looking at local neighbourhoods. We usually look at 8-pixel neighbourhoods where you look at the North, Northeast, East, Southeast, South, Southwest, West, Northwest directions. Another way of saying this is that the objects are 8-connected. For simplicity, sometimes people look at 4-pixel neighbourhoods, where you just look at the North, East, South and West directions. This woudl mean that the objects are 4-connected.
The output of bwlabel will give you an integer map where each object is assigned a unique ID. As such, the output of bwlabel would look something like this:
0 0 0 0 0 3 3 3 0 0
0 1 0 1 0 0 3 3 0 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 4 4
0 0 2 2 2 2 0 0 4 4
Because MATLAB processes things in column major, that's why the labelling is how you see above. As such, bwlabel gives you the membership of each pixel. This tells you where each pixel belongs to if it falls on an object. 0 in this map corresponds to the background. To call bwlabel, you can do:
L = bwlabel(img);
img would be the binary image that you supply to the function and L is the integer map I just talked about. Additionally, you can provide 2 outputs to bwlabel, and the second parameter tells you how many objects exist in the image. As such:
[L, num] = bwlabel(img);
With our above example, num would be 4. As another method of invocation, you can specify the connected pixel neighbourhoods you would examine, and so you can do this:
[L, num] = bwlabel(img, N);
N would be the pixel neighbourhood you want to examine (i.e. 4 or 8).
regionprops
regionprops is a very useful function that I use daily. regionprops measures a variety of image quantities and features in a black and white image. Specifically, given a black and white image it automatically determines the properties of each contiguous white region that is 8-connected. One of these particular properties is the centroid. This is also the centre of mass. You can think of this as the "middle" of the object. This would be the (x,y) locations of where the middle of each object is located. As such, the Centroid for regionprops works such that for each object that is seen in your image, this would calculate the centre of mass for the object and the output of regionprops would return a structure where each element of this structure would tell you what the centroid is for each of the objects in your black and white image. Centroid is just one of the properties. There are other useful features as well, but I'm assuming you don't want to do this. To call regionprops, you would do this:
s = regionprops(img, 'Centroid');
The above code will calculate the centroids of each of your objects in the image. You can specify additional flags to regionprops to specify each feature that you want. I do highly encourage that you take a look at all of the possible features that regionprops can calculate, as there are many that are useful in a variety of different applications and situations.
Also, by omitting any flags as input into the function, you would calculate all of the features in your image by default. Therefore, if we were to declare the image that we have seen above in MATLAB, this is what would happen after I run regionprops. After, let's calculate what the centroids are:
img = logical(...
[0 0 0 0 0 1 1 1 0 0;
0 1 0 1 0 0 1 1 0 0;
0 1 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 1 1;
0 0 1 1 1 1 0 0 1 1]);
s = regionprops(img, 'Centroid');
... and finally when we display the centroids:
>> disp(cat(1,s.Centroid))
3.0000 2.6000
4.5000 6.0000
7.2000 1.4000
9.6000 5.2000
As such, the first centroid is located at (x,y) = (3, 2.6), the next centroid is located at (x,y) = (4.5, 6) and so on. Take special note that the x co-ordinate is the column while the y co-ordinate is the row.
Hope this is clear!

LINES(M) unique colors Matlab

I'm using the LINES(M) Matlab function which returns an M-by-3 matrix containing a colormap. The problem is that sometimes it return the same colour twice. As an example I used LINES(8) in the results as shown below the first and last row are the same, so I'm asking how can the returned matrix has unique set of colours, if anyone could advise?
0 0 1
0 0.500000000000000 0
1 0 0
0 0.750000000000000 0.750000000000000
0.750000000000000 0 0.750000000000000
0.750000000000000 0.750000000000000 0
0.250000000000000 0.250000000000000 0.250000000000000
0 0 1
The lines colormap has a maximum of 7 unique colors after which they start to repeat.
>> lines(8)
ans =
0 0 1
0 0.5 0
1 0 0
0 0.75 0.75
0.75 0 0.75
0.75 0.75 0
0.25 0.25 0.25
0 0 1 % <---- starts to repeat
You can always select one of the other colormaps which use interpolation to build as many colors as you want. See doc colormap for a list of supported ones.
Similarly you can build you own colormap using the same linear interpolation technique between a specified number of stop-points.
For example, the jet colormap is constructed by passing through a series of 9 endpoints as shown here, using linear interpolation in between. The hsv colormap is built in a similar manner, only it interpolates across the hue space rather than in RGB. Here is yet another example showing how to build a custom divergent colormap with red-white-blue endpoints.
The lines color map has only 7 unique colors (as you have already spotted).
If you need more than 7 unique colors, you'll have to create a map by yourself.
One option is using rand:
>> rCmap = rand( n, 3 ); % create a random map with n colors - usually unique.

Animated 3D Scatter-Plot in Matlab

I have a set of data with a total of 4 independent variables, and I figure the only way to represent 4 independent variables and one dependent variables is to animate a 3D scatter-plot.
Let's say I have the following set of data:
W X Y Z Val
0 0 0 0 5.5
0 0 0 1 2.3
0 0 1 0 1.6
0 0 1 1 8.8
0 1 0 0 2.6
0 1 0 1 4.8
0 1 1 0 0.1
0 1 1 1 1.1
1 0 0 0 1.0
1 0 0 1 0.0
1 0 1 0 0.4
1 0 1 1 4.4
1 1 0 0 4.4
1 1 0 1 7.9
1 1 1 0 9.1
1 1 1 1 2.3
And the values were read in and converted to the following arrays:
W = {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}
x = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1}
X = {0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1}
Z = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}
Val = {5.5,2.3,1.6,8.8,2.6,4.8,0.1,1.1,1.0,0.0,0.4,4.4,4.4,7.9,9.1,2.3}
I'm wondering how to create a 3D scatter-plot (scatter3) with X, Y, and Z as the independent variables, Val as represented by the colors of the dots, and to animate with respect with the variable W as time?
Basically, when the different values of Val for when W=0 and W=1 are plotted at different times with respect to X, Y, and Z.
Based on your edit, I have slightly refined my suggestions:
mat=cell2mat([W;x;X;Z;Val])'; %Convert cells into a matrix
colors=prism(numel(mat(:,1)));
scatter3(mat(1,2),mat(1,3),mat(1,4),100,colors(1,:),'filled');
axis tight;
for jj=1:8:numel(mat(:,1))
scatter3(mat(jj:jj+7,2),mat(jj:jj+7,3),...
mat(jj:jj+7,4),100,colors(jj:jj+7,:),'filled');
drawnow
pause(1)
end
In the above example the colors are sequentially assigned, but if you want the colors to be the same where VAL is the same, you could do something like the following:
mat=cell2mat([W;x;X;Z;Val])';
val_new=mat(:,5)/max(mat(:,5)); %0<VAL<1
scatter3(mat(1,2),mat(1,3),mat(1,4),100,[0 val_new(1) 0],'filled');
axis tight;
for jj=1:8:numel(mat(:,1))
scatter3(mat(jj:jj+7,2),mat(jj:jj+7,3),...
mat(jj:jj+7,4),100,[zeros(8,1) val_new(jj:jj+7,:) zeros(8,1)],'filled');
drawnow
pause(1)
end
Of course, both these examples assume that you will consistently have 8 entries at each time. And in the second instance there will be occasions where the differences in the colors are only very slight. If you want to actually save a video or a animated gif, just take a look at getframe and imwrite.