Showing posts with label field. Show all posts
Showing posts with label field. Show all posts

Friday, February 19, 2021

Morphological Transformations

I started playing around with the OpenCV library in Python ("cv2"). Here are some of the mathematical morphological operations that can be done on an image of your choosing:

Here is an example of an "erosion" operation done on an image of a gradient. First, here is the input image that I used, of a gradient I also generated programmatically via Python:

And here is the processed image, using the erosion operation with a 3 x 3 kernel of 1s:

As I said, I've been playing around with these mathematical morphological operations, of erosion, dilation, edge detection and sharpening of images, all using the OpenCV library. The functions are pretty simple to use. The library is really powerful, giving you everything you need at the touch of a simple function. One can also use function composition to use a function on top of a function. I wrote some helper functions to facilitate the task of using the morphological transformation functions.

Monday, February 15, 2021

Generating a Gradient Programmatically

I've posted many times on this blog, my experiments in image generation through Python. I've been working on what I call my "Noisefield" project, where I generated a bitmap with random black and white pixels. I have now done something similar, except this time I generated a black to white gradient, using the PIL library in Python.

The idea is pretty simple. You create a vector or array of values from 0 to 255, then you repeat that 256 times, to make a square matrix, or bitmap (because Python puts in the pixels from top-left downwards and towards the right, line by line). So that way you get a gradient. I took the "LINE" variable that I created with a for loop and range() function and then multiplied it by 256 in a variable called MATRIX. I then used an iterator function on the Matrix which I called in the image-generation function by calling next(MATRIX). The result is a smooth matrix.

ADDENDUM: I have found a simpler way of generating a black to white gradient, using the np.linspace function in Numpy.

FOR YOUR INFORMATION: I have written a few blog posts in the past about generating bitmaps with random black and white pixel values, or grey values between 0 and 255. I found a much quicker way to do this using the OpenCV library (cv2). (In passing, this np.random.randint() function gives the same results as the uniform_noise function below, in the second Gist below:

Here's another way to generate noise, Gaussian noise in this case, via OpenCV (cv2). The Gaussian distribution is "smoother" in appearance, as it has mean 128 and standard deviation 20:

With mean 0 and standard deviation 256, we get a "coarser" noise distribution:

As stated, the uniform_noise "randu" function gives the same result as the previous np.random.randint() function, a uniform noise distribution:

If I add the following, "ret,thresh1 = cv2.threshold(uniform_noise,64,255,cv2.THRESH_BINARY)" and then write "cv2.imwrite("Noise.jpg",thresh1)", I get a noisy distribution with many more whites, because of the threshold value being at 64. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value, in this case 255, which is pure white. This way it is possible to adjust the parseness if you will of the noisy distribution using the threshold function:

The opposite would be true if we set the treshold higher, to say 250. Then it's mostly black, or at a pixel value of 0:

Monday, August 8, 2016

Noise Fields, Noise Fields, Noise Fields

Noise Field (512 px x 512 px). An applied noise function. A.G. (c) 2016.

Here we have pretty much the same thing as before, i.e. a function called noisefield() that returns an image. This time what we did, or tried to do, was to generate a "LINE" first that is a list of 512 "RGB tuples" which in this case, as before, are randomly generated (via the noxel() function).

Then, simply enough, all we did was create an entity called "MATRIX" that is made up of 512 "LINEs". We used an "iter" function to create an object called "nixel", as you can see, that iterates over the "MATRIX" object which is basically just a very long sequence of RGB tuples made up of the concatenation of 512 "LINEs"

What you can see in the image above, created via this Python script, is an interesting series of LINEs, each with randomly chosen RGB tuple values.

Here is the code, hosted on Github as a Github Gist.

You might as well ask, since I know you're thinking it: Why go through all the troube? Couldn't you do this much more simply with less code and in a much more idiomatic style, plus much more elegantly? The answer is, yes, of course I could. This is only the beginning. I have structured the code this way because I want to be able to create what I call "mod" functions which are "modulations of the noise field", so to speak. I want to be able to create noise fields and then apply functions to them, either on a pixel-by-pixel basis or else on the whole image, or else on individual "LINEs" which in this case, as vectors if you will of random "RGB tuples", the LINEs that is, are actually "column" vectors and not "row" vectors, if you will. That is to say, the LINEs in the Image, of individual random RGB tuples, appear "horizontally", but the actual LINE in the code ends up being in the "vertical" direction. This is not a glitch, it's because in the code in the 5th line of the noisefield() function, we have (x,y), i.e.

im.putpixel((x, y), nixel.next())

We could just as easily make the uniform-colored lines appear on the vertical axis in the final image just by switching (x,y) to (y,x). To be continued...