Laplacian and Gaussian Filter - image

I am trying to do some image processing and I would like to apply the LoG kernel. I know the formula, which is :
But I didn't understand how to obtain the kernel matrix with this formula. From what I have read, I have a matrix of n x n and I apply this formula to every cell in that matrix, but what should be the starting values within that matrix in the first place.
Also, I have the same question with the Laplacian filer. I know the formula, which is:
and also, from what I have read, the 3 x 3 filter should be the matrix:
x = [1 1 1; 1 -4 1; 1 1 1]
but can you please tell me how to apply the formula in order to obtain the matrix, or at least indicate me a tutorial of how to apply this.

Basically, we are just going from continuous space to discrete space. The first derivative in continuous time (space) is analogous to the first difference in discrete time (space). To compute the first difference of a discrete-time signal, you convolve [1 -1] over the signal. To compute the second difference, you convolve a signal with [1 -2 1] (which is [1 -1] convolved with itself, or equivalently, convolving the signal with [1 -1] twice).
To calculate the second difference in two dimensions, you convolve the input image with the matrix you mentioned in your question. That means that you take the 3-by-3 mask (i.e, the matrix you mentioned), multiply all nine numbers with nine pixels in the image, and sum the products to get one output pixel. Then you shift the mask to the right, and do it again. Each shift will produce one output pixel. You do that across the entire image.
To get the mask for a Gaussian filter, just sample the two-dimensional Gaussian function for any arbitrary sigma.
This may help: convolution matrix, Gaussian filter

Related

openGL, the inverse of the transformations

If I have 3 different matrices, one for rotation (R), one for translation(T), and one for scaling (S), how to achieve the reverse effect of these matrices by manipulating the one caused it?
What I gathered so far is if I transposed the rotation matrix, I will achieve what I desire (is this correct?). And what about the other two?
And if there is a common way, are there any special cases where these ways will not suffice?
The inverse of the rotation matrix R is indeed its transpose RT.
The inverse of the scaling matrix S is easy as it contains only diagonal elements (the first three rows, as the last row is always equals to (0 0 0 1))
So you just replace each diagonal si with 1/si.
Finally the translation matrix T is an identity matrix with the translation vector on the last column. The inverse is achieved by replacing those elements with their negative.
Also the inverse of the product of 3 matrices, is the reverse product
(S T R)-1 = R-1 T-1 S-1

FFT Input array number?

I want to ask a few questions.
URL of the video:
https://www.youtube.com/watch?v=9SwuRRe-2Jk&lc=UgyiumSTV11t3SQGNU94AaABAg
1.why is the code
double x = new double[] {1, -2, 3, 4 ,5 ,-6 ,7, 8}
only inside -2 and -6 to add a minus sign then other numbers don't need
2.I don’t know much about the results in this URL of the video.
Can explain it?
Thank you master
enter image description here
why in the code double x = new double[] {1, -2, 3, 4 ,5 ,-6 ,7, 8}
only -2 and -6 have a minus sign while other numbers don't need
This is only a test vector which would be replaced by whatever signals you want to transform using the FFT. Such signals could all sorts of values, so a test vector that includes somewhat arbitrary positive and negative numbers is reasonable.
I don’t know much about the results in this URL of the video. Can you explain it?
A full explanation of the theory behind the Discrete Fourier Transform (and the efficient Fast Fourier Transform algorithm to compute it) is beyond the scope of this post. However, from the test performed there are a few things that could and should easily be noticed:
Computing the inverse transform on the FFT of the input gives you back the original input
Computing the FFT of a real signal gives you a signal with Hermitian symmetry: first line is purely real, as is line N/2+1 (in this case where N=4, the third line is purely real), and the other lines pair up in complex conjugate symmetry (in this case where N=4, line 2 & 4)
Computing the FFT of an complex signal does not gives you a signal with Hermitian symmetry

Showing two images with the same colorbar in log

I have two sparse matrices "Matrix1" and "Matrix2" of the same size p x n.
By sparse matrix I mean that it contains a lot of exactly zero elements.
I want to show the two matrices under the same colormap and a unique colorbar. Doing this in MATLAB is straightforward:
bottom = min(min(min(Matrix1)),min(min(Matrix2)));
top = max(max(max(Matrix1)),max(max(Matrix2)));
subplot(1,2,1)
imagesc(Matrix1)
colormap(gray)
caxis manual
caxis([bottom top]);
subplot(1,2,2)
imagesc(Matrix2)
colormap(gray)
caxis manual
caxis([bottom top]);
colorbar;
My problem:
In fact, when I show the matrix using imagesc(Matrix), it can ignore the noises (or backgrounds) that always appear with using imagesc(10*log10(Matrix)).
That is why, I want to show the 10*log10 of the matrices. But in this case, the minimum value will be -Inf since the matrices are sparse. In this case caxis will give an error because bottom is equal to -Inf.
What do you suggest me? How can I modify the above code?
Any help will be very appreciated!
A very important point is that the minimum value in your matrix will always be 0. Leveraging this, a very simple way to address your problem is to add 1 inside the log operation so that values that map to 0 in the original matrix also map to 0 in the log operation. This avoids the -Inf error that you're encountering. In fact, this is a very common way of visualizing the Fourier Transform if you will. Adding 1 to the logarithm ensures that the transform has no negative values in the output, yet the derivative or its rate of change remains intact as the effect is simply a translation of the curve by 1 unit to the left.
Therefore, simply do imagesc(10*log10(1 + Matrix));, then the minimum is always bounded at 0 while the maximum is unbounded but subject to the largest value that is seen in Matrix.

Normalized Graph Cuts Image Segmentation

I'm implementing the normalized graph-cuts algorithm in MATLAB. Can someone please explain how to proceed after bi-partitioning the second smallest eigen vector. Now I have 2 segments, what is the meaning of "recursively bi-partitioning the segmented parts?"
Recursively bi-partitioning means that you need to write a recursive function. this function should bi-partition your segment in each iteration.
you have image I , you make that image to two partition I1 and I2 , then you need to bi-partition each one ,you can call
I11,I12
and
I21, I22
and then again bi-partition each segment , you can call that
I111,I112
and
I121,I122
, and
I21,I22
, and
I221 ,I222
and continue in this way...
I assume you use Matlab function to solve generalized eigenvalue problem with 'sm' option:
[eigvectors,eigvalues]=eigs(L,D,6,'sm')
L is laplacian matrix = D-W
D is diagonal matrix
W is your weight matrix
number 6 means that you are looking for 6 eigen vectors , and 'sm' means you are looking for smallest magnitude.
My thesis for my Master degree in AI was about Improving segmentation using Normalized cut ; feel free to ask any questions.

Usage of the gaussian blur

I read now tons of different explanations of the gaussian blur and I am really confused.
I roughly understand how the gaussian blur works.
http://en.wikipedia.org/wiki/Gaussian_blur
I understood that we choose 3*sigma as the maxium size for our mask because the values will get really small.
But my three questions are:
How do I create a gaussian mask with the sigma only?
If I understood it correctly, the mask gives me the weights, then
I place the mask on the top left pixel. I multiply the weights for
each value of the pixels in the mask. Then I move the mask to the
next pixel. I do this for all pixels. Is this correct?
I also know that 1D masks are faster. So I create a mask for x and a
mask for y. Lets say my mask would look like this. (3x3)
1 2 1
2 4 2
1 2 1
How would my x and y mask look like?
1- A solution to create a gaussian mask is to setup an N by N matrix, with N=3*sigma (or less if you want a coarser solution), and fill each entry (i,j) with exp(-((i-N/2)^2 + (j-N/2)^2)/(2*sigma^2)). As a comment mentioned, taking N=3*sigma just means that you truncate your gaussian at a "sufficiently small" threshold.
2- yes - you understood correctly. A small detail is that you'll need to normalize by the sum of your weights (ie., divide the result of what you said by the sum of all the elements of your matrix). The other option is that you can build your matrix already normalized, so that you don't need to perform this normalization at the end (the normalized gaussian formula becomes exp(-((i-N/2)^2 + (j-N/2)^2)/(2*sigma^2))/(2*pi*sigma))
3- In your specific case, the 1D version is [1 2 1] (ie, both your x and y masks) since you can obtain the matrix you gave with the multiplication transpose([1 2 1]) * [1 2 1]. In general, you can directly build these 1D gaussians using the 1D gaussian formula which is similar as the one above : exp(-((i-N/2)^2)/(2*sigma^2)) (or the normalized version exp(-((i-N/2)^2)/(2*sigma^2)) / (sigma*sqrt(2*pi)))

Resources