How to measure the second derivatives of an image [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am at very first step in MATLAB programming and when I read an article about image processing, I see in most of them it has written that the first and/or second derivatives should be estimated.
How I can measure the second derivatives (Gxx,Gxy,Gyy) over the gradient?

Instead of applying successive differences, you could apply the second derivative kernel in each dimension:
Gx = [1 -2 1]; Gy = Gx'; %' y kernel is column vector
img = double(imread('cameraman.tif'));
Dxx = conv2(img,Gx,'same');
Dyy = conv2(img,Gy,'same');
If you were after a non-directional second derivative, you should use the Laplacian. A common kernel is:
L = [0 1 0;
1 -4 1;
0 1 0;] % fspecial('laplacian',alpha=0)
D2 = conv2(img,L,'same');
As in the comment above, you can use fspecial to get variations on the kernel that capture diagonal differences via the alpha parameter. Or you can use del2:
D2 = del2(img);

Look into imgradient from the Image Processing Toolbox. Two applications of that should give you what you want.
Something like this:
im = imread('cameraman.tif');
[Gx,Gy] = imgradientxy(im);
[Gxx,Gxy] = imgradientxy(Gx);
[Gyy,Gyx] = imgradientxy(Gy);
This uses a Sobel filter to compute derivatives. You can also use Prewitt, central differences or intermediate differences by passing this as a string to imgradientxy.
Hope that helps.

Related

How to calculate the Euclidean distance between vectors? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to calculate the Euclidean distance between each pair of num1, num2 and center1 but an error is shown: "double Conversion to double from cell is not possible"
[num1]={4,4,4,4,43,4,34,55,6,6,6,65,5,4,4,43,2,2,3,45,6,67,7,7,7,7,4,5,66,5,4,3,3,2,3,4,5};
[num2]={41,42,43,44,43,4,3,5,62,62,63,65,54,4,4,4,24,24,34,4,6,6,47,47,7,7,4,45,16,51,41,13,3,2,3,4,5};
[center1]={20,30};
Create an array like this:
a = [1 2 3 4];
Using curly braces like you did it creates a cell array.
To get the distance, you may use the MATLAB function pdist:
D = pdist(X)
This computes the Euclidean distance between pairs of objects in m-by-n data matrix X.
To calculate the Euclidean distance between two vectors:
a = [1 2 3 4];
b = [1 2 4 4];
d = pdist([a;b])
For further information, refer to the documentation.
The pdist command requires the Statistics and Machine Learning toolbox. If you don't have that toolbox, you can also do it with basic operations. Euclidian distance between two vectors of points is simply the sqrt(sum( (a-b).^2 ). So, you can do:
a = [1 2]; %2D vector, though any dimension is OK
b = [4 7]; %any values, but must be same size as `a`
dist = sqrt(sum((a-b).^2)); %Euclidian distance
No toolboxes needed!
As mentioned in the other answer, don't use curly braces to enclose your numbers. This is a case where you need plain matlab arrays, which are the square brackets as used above.

Shape recognition in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Guys want write a basic code in matlab that recognizes basic shapes
Have converted a .png image to binary and then applied edge detection (Canny)
what to do next so that i can use the edge detected image to state that the image is a circle.
i = imread('h.png');
i= im2bw(i,0.5);
i=edge(i,'canny');
imshow(i);
what to do next???
you probably should learn about hough transform.
Matlab has already a function for circles imfindcircles (check out the examples!)
You could try using a neural net that was trained on a series of shapes. The nn can return a percentage accuracy so that you can see how well its doing for example.
function scan(img)
files = dir('*.jpg');
hist = [];
for n = 1 : length(files)
filename = files(n).name;
file = imread(filename);
hist = [hist, imhist(rgb2gray(imresize(file,[ 50 50])))]; %#ok
end
som = selforgmap([10 10]);
som = train(som, hist);
t = som(hist); %extract class data
net = lvqnet(10);
net = train(net, hist, t);
like(img, hist, files, net)
end
Can be tweeked to recognize shapes in an image.

Quantification of cepstral coefficients for speech coder [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have been working on Homomorphic speech coder I have obtained the cepstral coefficients of the signal and the next step I have been asked to perform is quantize the coefficients using adaptive quantizer. I am not sure how to quantize the coefficients as its value ranges from -1.5 to 1.5, if i quantise it I just get 0 and 1 which i'm sure is wrong. What is the right way to quantise it.
I think when you are being asked to quantize the coefficients, you are being asked to set a resolution and quantize to that resolution. For example if you are to quantize to a 32-bit number, this would mean you would divide your range into 2^32 bins and quantize your values into those bins. For example:
offset = 1.5;
input_range = 3;
output_range = 2^32
quantized_value = round((value + offset) / input_range * output_range);
If you are being asked to use an adaptive quantizer that would mean that the resolution of the bins would be dynamic or the output bit width is variable. You would need to do more work to find what your goals are if you are going to write an adaptive algorithm for the quantization.

Creat Log-normal Random Variable in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have trouble with the probability density function (p.d.f) of a log-normal distribution. I really need your help. As defined by wikipedia:
http://en.wikipedia.org/wiki/Log-normal_distribution#Probability_density_function
The probability density function of a log-normal distribution is:
My problem is, how to define x variable in MATLAB? Thanks for your help!
If you have the Stats toolbox, you can just use lognpdf:
y = lognpdf(x,mu,sigma);
Though this is a very simple function - fully vectorized, it's effectively just:
y = exp(-0.5*((log(x)-mu)./sigma).^2)./(x.*sqrt(2*pi).*sigma);
But you may want to check that x > 0 and sig > 0. To create this plot on the Wikipedia article that you cited, you can do:
mu = 0;
sigma = [1;0.5;0.25];
x = 0:0.01:3;
y = lognpdf([x;x;x],mu,sigma(:,ones(1,length(x))));
figure; plot(x,y);
When your question asks about defining x, maybe you're actually looking for log-normally distributed random variables, i.e., you want to sample randomly from the log-normal PDF/distribution? In that case you can use lognrnd:
r = lognrnd(mu,sigma);
I'm confused, like you can do this in a one-liner,
fun = #(x,mu,sigma) (1./(x*sigma*sqrt(2*pi))).*exp( -(power((log(x)-mu),2))/(2*power(sigma,2)))
x is any value that satisfies x > 0, the pdf tells you via Wikipedia
In probability theory, a probability density function (pdf), or
density of a continuous random variable, is a function that describes
the relative likelihood for this random variable to take on a given
value.
So any value x given to the log-normal pdf tells you tel relative likelihood that a random variable could be that value.
Consider this toy example:
mu = 1;
sigma = 10;
x = logspace(-2,0,10);
plot( x, fun(x,1,10) )
From this plot as x gets closer to zero, it's relative likelihood of actually taking on that value increases. DISCLAIMER I just threw that function together, it needs to be checked for accuracy, the preceding was for illustration only.

Random number in a certain interval with a exponential distribution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'd like to generate a random number in a certain interval using an exponential distribution. My problem is that if I use exprnd I can't control the interval, I can only give a mean value, but that doesn't suit my needs.
Is there another function or is there some trick that I have to use?
Does this help? (or have I misunderstood the problem?)
%#Set the parameters
T = 2000; %#Number of observations to simulate
Mu = 0.5; %#Exponential distribution parameter
LB = 0; %#Lower bound on exponential distribution
UB = 1; %#Upper bound on exponential distribution
%#Validate the parameters
if LB < 0 || UB < 0; error('Bounds must be non-negative'); end
if Mu <= 0; error('Mu must be positive'); end
%#Determine LB and UB in terms of cumulative probabilities
LBProb = expcdf(LB, Mu);
UBProb = expcdf(UB, Mu);
%#Simulate uniform draws from the interval LBProb to UBProb
Draw = LBProb + (UBProb - LBProb) .* rand(T, 1);
%#Convert the uniform draws to exponential draws using the inverse cdf
X = expinv(Draw, Mu);
Exponential distribution is supported on [0,+\infty). You may want to remap it on [0,1) using some measurable invertible map f, so that Y = f(X) is a random variable supported on [0,1).
Problem: you have to build such an f.
My suggestion is
f(x) = 2/pi * arctan(x).
The function arctan maps (-\infty,\infty) to (-pi/2,pi/2). Because you are considering just positive samples (because your X goes exponentially) you will obtain samples in [0,pi/2); thus, you have to rescale by 2/pi. Moreover, because the MacLaurin expansion of arctan is x+o(x), you have samples that go exactly exponentially close enough to the origin.
Now, if you sample from whatever exponential (i.e. using possibly any value of \lambda - preferably small) and you evaluate f on the sample, you get samples that concentrate as you like (i.e. close to 0 and nearly exponential).
Here's a suggestion:
Sample from the exponential distribution with lambda=1, and reject any number outside of your intended interval. If your interval is [0,1], you have a probability of ~0.63 to get a number in that interval. That means a 99% probability of getting a "good" number after 10 samples.
Another possibility is to choose a high enough number n, such that the probability of sampling something over n is sufficiently small. For lambda = 1, n=1000 would suffice. Then you just sample from the exponential and transform it to your random sample by a+(b-a)*(sample/n)