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)))
Related
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
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.
I would like to ask about Savitzky–Golay filter on 2D-images.
What are the best coefficient and order to choose for finding local details in the image.
Moreover, if someone has an explanation for coefficients and the orders one the 2D-images, it would be perfect.
Thanks in advance
Please check out this website:
https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter#Two-dimensional_convolution_coefficients
UPDATE: (Thank you for the suggestion, #Rasclatt)
Which has been reproduced here:
Two-dimensional smoothing and differentiation can also be applied to tables of data values, such as intensity values in a photographic image which is composed of a rectangular grid of pixels.[16] [17] The trick is to transform part of the table into a row by a simple ordering of the indices of the pixels. Whereas the one-dimensional filter coefficients are found by fitting a polynomial in the subsidiary variable, z to a set of m data points, the two-dimensional coefficients are found by fitting a polynomial in subsidiary variables v and w to a set of m × m data points. The following example, for a bicubic polynomial and m = 5, illustrates the process, which parallels the process for the one dimensional case, above.[18]
The square of 25 data values, d1 − d25
becomes a vector when the rows are placed one after another.
The Jacobian has 10 columns, one for each of the parameters a00 − a03 and 25 rows, one for each pair of v and w values. Each row has the form
The convolution coefficients are calculated as
The first row of C contains 25 convolution coefficients which can be multiplied with the 25 data values to provide a smoothed value for the central data point (13) of the 25.
check out the below links which use SURE(Stein's unbiased risk estimator) to minimizes the mean squared error between your estimate and the image. This method is useful for denoising and data smoothing.
this link is for optimization of parameters for 1D Savitzky Golay Filter(this will be helpful to understand the 2D part)
https://ieeexplore.ieee.org/abstract/document/6331560/?part=1
this link is for optimization of parameters of 2D Savitzky Golay Filter
https://ieeexplore.ieee.org/document/6738095/
Well am referring the following paper and trying to implement the algorithm as given in matlab
The only problem is how do i find a noisy pixel i.e Pixel with impulse noise?
X seems to be the impulse pixel in an image which i have to calculate
_
____________________________________________
Input – Noisy Image h
_______________________________________________
Step 1: Compute X
for every pixel repeat steps from 2 to 7
Step 2: Initialize w = 3
Step 3: If X(i,j) ≠ Impulse pixel
goto step 7
Step 4: ∆i,j = { h(i1,j1) | i-(w-1)/2 ≤ i1 ≤ i+(w-1)/2,
j-(w-1)/2 ≤ j1 ≤ j+(w-1)/2}
b=no. of black pixels in the window
w=no. of white pixels in the window
Step 5: If ∆i,j ≠ NULL
p(i,j) = mean(∆i,j
)
d(i,j) = | h(i,j) – p(i,j) |
else if (w < wmax)
w=w+2
goto step 4
else
if (b>w)
h(i,j)=0
else
h(i,j)=255
Step 7: Goto next pixel
Step 8: Calculate threshold t, from detailed coefficient
matrix d
for every pixel
Step 9: If (d(i,j)>t)
h(i,j)=p(i,j)
____________________________
Edit: To implement the PSM or the median filter method we
need to set some parameters and a threshold value. This
threshold value is dependent on the image and the noise
density. So, to restore different images we need to check for
a range of threshold values and find out the best one. So, in
our proposed algorithm we removed the need to define a threshold value. The algorithm is intelligent and determines
the threshold automatically.
The article you are trying to implement is obviously badly written...
For instance in the algorithm w means 2 things: the size of the window, and the number of white pixels!!!
Both the step 1 and 7, are refering to the same loop.
Anyway, to me, the "impulse pixels" are all the pixels a which are either equal to 0 or 255.
Basically, they are the pixels which are part of the "salt and pepper" noise.
So basically, you can find them by doing:
[impulsepixelsY,impulasPixelX]=find((im==0)|(im==255));
From the paper it seems that the "impulse pixels" are just the noisy pixels, in the case of salt & pepper noise. Furthermore, it also seems that the algorithm provides an "intelligent" mechanism to calculate the denoised value of a noisy pixel if its value is above a threshold (which it calculates adaptively).
So, what about "If X(i,j) ≠ Impulse pixel " ? Well, apparently, the authors assume to know (!) which pixels are noisy (!!), which makes the whole thing rather ridiculous, since this info is almost impossible to know.
I might also add that the rather stunning results presented in the paper are most probably due to this fact.
P.S. Regarding the argument that <"impulse pixels" are all the pixels a which are either equal to 0 or 255>, it is wrong. The set of pixels that have either 0 or 255 intensity value, includes the noisy pixels as well as proper pixels that just happen to have such a value. In this case, the algorithm will most probably collapse since it will denoise healthy pixels as well.
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