Hello geeks and welcome in today’s article, we will discuss NumPy ogrid and its uses in detail. NumPy is a numerical module of python, and Ogrid is one of its functions that stands for the open grid. It helps us by providing a way to act on a particular pixel of an image based on its Index number. As we move further in this article, we will try to understand Numpy ogrid through its syntax. We will also look at its applications and a couple of examples.
SYNTAX OF NUMPY OGRID
The general syntax NumPy ogrid is:
<numpy.lib.index_tricks.OGridClass object>
nd_grid instance which returns an open multi-dimensional “meshgrid”
USES OF NUMPY OGRID
Suppose we have a matrix that is 5 * 7. Here the ogrid function will take a range of coordinates(0 to 4) for x and (0 to 6) for y. Then it will store the value into variables x and y. We can observe that indexing the matrix with ogrid gives back the same matrix with this. Further, we can filter our results by index numbers. This gives us an array of true and false. This condition is called a MASK. Now we can use this mask to act only on the matrix elements where the value holds (or false as per our assumption).
You must be wondering what this has to do with a particular pixel of an image and how we can work on it using this. Now instead of a matrix, let us consider an image of dimension 1920*1080. After this using the same technique, we can create a mask for it. Then we can use that mask to act only on the pixels that concern us.
RETURNS
It returns an open mesh grid so that only one dimension of each returned array is greater than 1. The dimensions and number of output are equal to the number of indexing dimensions.
MESH-GRID: ndarrays with only one dimension not = 1
EXAMPLES OF NUMPY OGRID
Not let us look at some examples of Numpy ogrid:
#code from numpy import ogrid ogrid[-1:1:5j] array([-1,-.5,0,0.5,1,1.5]) ogrid[0:4,0:6]
Output:
[array([[0],
[1],
[2],
[3]]),array([[0,1,2,3,4,5]])]
EXPLANATION
In the above example of we have used the ogrid function. Here we have defined an array of 6 elements. In the next step, we have defined our criteria for which we need the value. In the end, we get our desired value, with one array having 4 values and the other having 6.
Let us look at one more example
#code from numpy import ogrid ogrid[-1:1:5j] array([-1,-.5,0,0.5,1,1.5,2,2.5,3]) ogrid[0:3,0:9]
Output:
[array([[0],
[1],
[2]]),array([[0,1,2,3,4,5,6,7,8]])]
EXPLANATION
In the above example, we have again used the ogrid function with a different set of parameters. Here we can that the output is restricted to 3 and 9. So we get an output concerning it. We can see how useful the ogrid function is, and it how reduces our labor.
Must Read
- Understanding the Numpy mgrid() function in Python
- Matrix Addition in Python | Addition of Two Matrices
CONCLUSION
In this article, we read about the NumPy ogrid and its syntax, which uses an example. It helps us by providing a way to act on the particular pixel of the image. I hope this article was able to answer all your queries. If any doubt remains, feel to comment down below. Done reading it, why not cover numpy variance up next.
The post NumPy ogrid and Its Uses in Python appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment