NumPy ogrid and Its Uses in Python

Hello geeks and welcome in today’s article, we will discuss NumPy ogrid and its uses in detail. NumPy is one of the important modules in python that allows you to manage arrays in effective ways.

NumPy 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 the 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.

What is the Difference between Numpy Ogrid and Mgrid?

The main difference between them is that ogrid returns open mesh grid whereas mgrid returns a dense mesh grid. In mgrid, it forms a large array containing all the meshes whereas in ogrid it forms an array of straight arrays.

Code:

from numpy import ogrid, mgrid

print("ogrid")
print(ogrid[0:5,0:5])
print("mgrid")
print(mgrid[0:5,0:5])

Output:

ogrid
[array([[0],
       [1],
       [2],
       [3],
       [4]]), array([[0, 1, 2, 3, 4]])]
mgrid
[[[0 0 0 0 0]
  [1 1 1 1 1]
  [2 2 2 2 2]
  [3 3 3 3 3]
  [4 4 4 4 4]]

 [[0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]
  [0 1 2 3 4]]]

Must Read

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments