Simple 2D Perlin Noise in Python - python-3.x

There is lots of different implementations of 2D perlin noise in Python.
My question is there a simple implementation of perlin noise in Python that fits in 1 function or 1 class? Or maybe there is easier-to-implement 2D noise that is similar to perlin noise?

Does it need to be integers, or is double floating point precision good enough? Can you use Cython? There is a Cython wrapper for FastNoiseLite here: https://github.com/tizilogic/PyFastNoiseLite . You can convert the integers to doubles, with plenty of precision left over.
I would also suggest using the OpenSimplex2 or OpenSimplex2S noise option, rather than Perlin. Perlin as a base noise is very grid-aligned looking. Simplex/OpenSimplex2(S) directly address that.

The simplest implementation of Perlin noise I have found has been this.
https://pypi.org/project/perlin-noise/
Once installed, and initialised at the top of your code, simply calling the function noise(float) returns the value at that point of the noise field. Additionally, with "unlimited coordinate space", you can simply add more values to the noise function noise(float,float) to change to a 2D, 3D, or higher dimensional noise field.
They provide a couple of basic examples on the website which I found very helpful and sufficient to then be able to implement the library.

Related

Cross-Correlation between 3d fields numerically

I have a two 3D variables for a each time step (so I have N 3d matrix var(Nx,Ny,Nz), for each variables). I want to construct the two point statistics but I guess I'm doing something wrong.
Two-point statistics formula, where x_r is the reference point and x is the independent variable
I know that the theoretical formulation of a two-point cross correlation is the one written above.
Let's for sake of simplicity ignore the normalization, so I'm focusing on the numerator, that is the part I'm struggling with.
So, my two variables are two 3D matrix, with the following notation phi(x,y,z) = phi(i,j,k), same for psi.
My aim is to compute a 3d correlation, so given a certain reference point Reference_Point = (xr,yr,zr), but I guess I'm doing something wrong. I'm trying that on MATLAB, but my results are not accurate, and by doing some researches online it does seem that I should do convolutions or fft, but I don't find any theoretical framework that explains how to do that and why the formulation above in practices should be implemented by the use of a conv or fft. Moreover I would like to implement my cross-correlation in the spatial domain and not in the frequency domain, and with the convolution I don't understand how to choose the reference point.
Thank you so much in advance for reply

How does a graphics engine figure out how to place pixels to make a 3d image?

I was wondering what procedure a simple 3d program uses to draw 2d pixels so that they appear 3d. I'm really interested in this for drawing purposes since if a program can figure out how to use a flat screen to produce images with depth then maybe I could use those techniques in my drawing.
Are there any basic 3d engine out there I can look at? Without any 2d to 3d abstractions?
Two notions may interest you:
The perspective projection, which is the mathematical transformation which takes 3D points (or vertices) and the characteristics of your camera (position, orientation, frustrum, ...) and gives you the 2D projection of the point on your chosen medium (screen).
Wikipedia - 3D Projection
StackOverflow - Transform GPS-Points to Screen-Points with Perspective Projection in Android (I made a detailed answer)
The Painter's algorithm (since you seem to ask for drawing-related techniques), a rendering method which sorts by depth all the elements of your scene after their projection, and draws them on your medium by decreasing depth, to ensure a realistic output ("far objects hidden behind closer ones" - imitating painters method). This algorithm has however some limits (far from efficient in its basic implementation, can't easily deal with elements intersecting or circularly overlapping each others), so most of the days a more efficient method is used, the Z-buffering, which deals with depth conflicts on a pixel-to-pixel basis.
Wikipedia - Painter's algorithm
Wikipedia - Z-buffering
By combining those notions, you can actually implement your own simple 3D engine (in the other StackOverflow thread I'm pointing, I gave a link to an article I made about creating such an engine easily).
If you want to look at more complex engines and notions, you can take a look at the GPU Gems 3 by Nvidia for instance, or look at articles about OpenGL.
Hope it helped, bye !

Canny algorithm is enough for creating a feature descriptor image and giving for SVM?

i retrieve contours from images by using canny algorithm. it's enough to have a descriptor image and put in SVM and find similarities? Or i need necessarily other features like elongation, perimeter, area ?
I talk about this, because inspired by this example: http://scikit-learn.org/dev/auto_examples/plot_digits_classification.html i give my image in greyscale first, in canny algorithm style second and in both cases my confusion matrix was plenty of 0 like precision, recall, f1-score, support measure
My advice is:
unless you have a low number of images in your database and/or the recognition is going to be really specific (not a random thing for example) I would highly recommend you to apply one or more features extractors such SIFT, Fourier Descriptors, Haralick's Features, Hough Transform to extract more details which could be summarised in a short vector.
Then you could apply SVM after all this in order to get more accuracy.

Rotation vectors vs quaternions

A rotation vector represent rotations by directly storing the axis of rotation and the angle magnitude.
Quaternions seem to be used much more to represent rotations. Why are quaternions preferred over rotation vectors in computer graphics?
Quaternions are much easier to compute with, for the computer of course (as a human you shouldn't bother with 3D rotations anyway):
What do you do when you want to concatenate two rotations in vector representation? You have to convert them to quaternion or matrix form (using costly trigonometrics) to do that (and maybe back again), whereas quaternions can be concatenated efficiently by using the classical quaternion multiplication.
What do you do when you want to rotate a point/vector using a rotation in vector-format, or send it to GL/D3D as matrix? You convert it into a matrix (again using costly trigonometrics). A quaternion on the other hand is quite efficiently converted into a matrix, since it already encodes the needed sines and cosines.
So matrices and quaternions are much more appropriate rotational representations. From those two quaternions are more compact and they are also quite easy to convert into an axis-angle representation (and back again), though using trigonometrics. So if you need axis-angle information at the peripherals (it's only us humans who sometimes need an actual rotation axis and angle, the computer doesn't really care) you can still use it, but for internal representation and computation quaternions or matrices are a much better choice.
If quaternions seem a bit heavy at first with their "3-dimensional complex number" explanation, don't bother with their exact mathematical underpinnings. Just start to understand how they work and how to use them. Pragmatically they are just a kind of axis-angle representation, but with implicitly encoded sines and cosines, which are needed for efficient transformation and computation.
For a good explanation of potential reasons why quaterions are used and sometimes preferred over vectors, see this very intersting article. In this lengthy but insightful thread you will find opposing opinions on the usefulness of quaternions.
TL;DR - the author's view is that we rather don't really need quaterions but because of their intricate and complex nature they seem to be very appealing to programmers. All operations exressed using quaternions can be expressed using vectors. This opinion is quite controversial though.

Geometric/Shape Recognition ( Odd Shape )

I would like to do some odd geometric/odd shape recognition. But I'm not sure how to do it.
Here's what I have so far:
Convert RGB image to Monochrome.
Otsu Threshold
Hough Transform.
I'm not sure what to do next.
For geometric information, you could do a raster to vector conversion to convert your image into coordinated vectors (lines and points) and finite element analysis to look for known shapes. Not easy but libraries should be available for both.
Edit: Note that there are sometimes easier practical solutions, but they depend on the image and types of errors. For example, removing perspective, identifying a 3d object from a 2d image, significance of colour, etc... You often see registration markers added to the real world object to overcome
this and allow much easier identification. Looking up articles on feature extraction techniques might help.

Resources