I have a 100x100 Matrix with Zeros. I want to add a 10x20 ellipsis around a specific point in the Matrix - lets say at position 40,60. The Ellipsis should be filled with values from 0 to 1. (1 in the center - 0 at the edge) - The numbers should be gaussian-distributed.
Maybe someone can give me a clue, how to start with this problem..
You need to draw samples from a multi-variate gaussian distribution. The function you can use is numpy.random.multivariate_normal
You mean value matrix should be [40, 60]. The covariance C matrix should be 2X2. Regarding its values:
C[1, 1], C[2, 2]: decides the width of the ellipse along each axis. Choose it so that 3*C[i,i] is almost equal to the width of the ellipse along this axis.
The diagonal values are zero if you want the ellipse to be along the axes, otherwise put larger values (keep in mind that C[2, 1]==C[1, 2]
However, keep in mind that, since it is a Gaussian distribution, the output values will be close to 0 at distance 3*C[i,i] from the center, but they will never be truly zero.
Related
My aim is to compute the maximum and minimum diameters of a slice of a 3D tubular shape (as shown in the attached figure).
What I tried : several rotations of the skeleton of the shape to make it parallel to the Z axis and then cut the interpolated tubular shape at some voxel to get the slice.
However these rotations make lot of interpolations. Hence, I would like to avoid these with a method that computes the diameters (max and min) without using rotations.
Any help please?
The specific problem I try to solve is:
I have a binary image binary map that I want to generate a heatmap (density map) for, my idea is to get the 2D array of this image, let say it is 12x12
a = np.random.randint(20, size=(12, 12));
index and process it with a fixed-size submatrix (let say 3x3), so for every submatrix, a pixel percentage value will be calculated (nonzero pixels/total pixel).
submatrix = a[0:3, 0:3]
pixel_density = np.count_nonzero(submatrix) / submatrix.size
At the end, all the percentage values will made up a new 2D array (a smaller, 4x4 density array) that represent the density estimation of the original image. Lower resolution is fine because the data it will be compared to has a lower resolution as well.
I am not sure how to do that through numpy, especially for the indexing part. Also if there is a better way for generating heatmap like that, please let me know as well.
Thank you!
Maybe a 2-D convolution? Basically this will sweep through the a matrix with the b matrix, which is just 1s below. So it will do the summation you were looking for. This link has a visual demo of convolution near the bottom.
import numpy as np
from scipy import signal
a = np.random.randint(2, size=(12, 12))
b = np.ones((4,4))
signal.convolve2d(a,b, 'valid') / b.sum()
I'm using a Gaussian filter with Scipy and I saw this code online which I'm curious about.
imx = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (0,1), imx)
imy = zeros(im.shape)
filters.gaussian_filter(im, (sigma,sigma), (1,0), imy)
For the first Gaussian filter call, the order is (0,1) and according to this link, that should give the the first order derivative of a Gaussian in y-direction. However, on running the code, I can see that the Gaussian is along the X direction.
The same thing applies to imy. Why does the code work that way?
For reference, running:
filters.gaussian_filter(im, (sigma, sigma), (0, 1), output= imx)
on this array:
[[0 3 2]
[1 4 1]
[3 4 2]]
Returns:
[[0.00071801 0.00148952 0.00077151]
[0.0006947 0.00144284 0.00074815]
[0.00067141 0.00139622 0.00072482]]
Which is a Gaussian in the x direction, even though the order (0, 1) suggests that it should be in the y direction. I think I might be missing something.
SciPy works with arrays, not with images. Arrays don't have x-coordinates and y-coordinates, they have indexes. Thus, the behavior of SciPy is explained in terms of indexes:
order : int or sequence of ints, optional
The order of the filter along each axis is given as a sequence of integers, or as a single number. An order of 0 corresponds to convolution with a Gaussian kernel. A positive order corresponds to convolution with that derivative of a Gaussian.
So, [0, 1] is the derivative in the direction of the change of the second index, and [0, 0, 0, 1, 0] is the derivative in the direction of the change of the fourth index.
When a 2D array is represented graphically, it is customary to interpret the first index as "row number" and the second index as "column number". This makes the first index correspond to the vertical downward direction and the second index to the horizontal direction (left to right, unless we are in an RTL environment).
So, (0, 1) is horizontal and (1, 0) is vertical. The author of the page may have made a terminological mistake. Also, there is no law against calling the horizontal axis y and the vertical axis x, so they may have done that.
I am using python to find the covariance matrix between 2 images, e.g. of size (N, N), but numpy.cov or numpy.corrcoef always returns a matrix of the size (2N, 2N), which I dont understand.
Isn't a covariance matrix the same size of a N,N array?
As shown
The upper left square is the covariance within the first image. The bottom right square is the covariance within the second image. The other two squares are the covariance between the images; each should be the same as the other flipped about the main diagonal.
Ok, so I'm doing the power method in python.
Basically, the equation revolves around multiplying a matrix A by a vector (y) like this:
for i in range(0, 100):
y = mult(matrix,y)
y = scalarMult(y, 1.0/y[0][0])
Then you multiply the vector y by 1/(the first element in y). Now, if the matrix is sparse or has a zero in just the right spot, you will get a zero for the first element in a. None of my googling skills have yielded a modification to the power method to avoid this.
For those interested, I'm trying to solve for the eigenvalues of a matrix; and my code works as long as there aren't too many zeros.
Instead of dividing by first element of the vector you can divide by one of its norms.
For example if you use second norm, the length of the vector will always be 1.
norm = sum(e**2 for e in y)**0.5
Norm of the vector is only zero when vector is 0 (has all elements 0), so division by 0 should not happen.