Python Tutorial: NumPy for images

Learn how NumPy handles images as arrays, where a color image is height × width × 3. Extract RGB channels, flip images, and build histograms by flattening each channel and plotting pixel intensity frequencies from 0 to 255.
Chapters
Now that you know basic concepts, you will learn some of the things that you can do with NumPy. With NumPy, we can practice simple image processing techniques such as flipping images, extracting features and analyzing them. Imagine that we have an image and we load it using MapGoLib's mread function. If you check its type using the Type Python function, you can see that it is a numpy and the array object. Because images can be represented by NumPy Multidimensional Arrays, also known as ND arrays. NumPy methods for manipulating arrays work well on these images. Remember that a color image is a NUMPY array with a third dimension. For color channels, we can slice the multi dimensional array and obtain these channels separately. Here we can see the individual color intensities along the image. For example, we obtain the red color of an image by keeping the height and width pixels and selecting only the values of the first color layer. Here we use matplotlib to display them. With the default color map we can observe the different intensities in their tone. We can also display them using the gray color map, specifying it with the CMAP attribute of the EMSHOW function. We can still see the different intensities along the images, one for each the red, green and blue. Just like with NUMPY arrays, we can get the shape of images. This material picture is 426 pixels high and 640 pixels wide. It has three layers for color representation. It's an RGB3 image, so it has a shape of 426, 640 and 3 and a total number of pixels of 870,920. We can flip the image vertically by using the flip path method. As you saw in the previous video, we are using the showimage function to display an image. You can flip the image horizontally using the flip error method. The histogram of an image is a graphical representation of the amount of pixels of each intensity value from 0 being pure black to 255 pure white. The first image is really dark, so most of the pixels have a low intensity from 0 to 50, while the second one is lighter and has most of the pixels close to 200 and 255. We can also create histograms from RGB3 colored images. In this case, each channel red, green and blue will have a corresponding histogram. We can learn a lot about an image by just looking at its histogram. Histograms are used to threshold images, an important topic in computer vision that we will cover later on in the course to alter brightness and contrast and to equalize an image, which we'll also cover later on in the course. Matplotlib has a histogram method. It takes an input array, frequency and bins as parameters. The successive elements in bin array act as the boundary of each bin. We obtain the red color channel of the image by slicing it in. Here we then use the histogram function use Ravel to return a continuous flattened array from the color values of the image, in this case red, and pass this rubble and the bins as parameters. We set bins to 256 because we will show the number of pixels for every pixel value that is from 0 to 255, meaning you need 256 values to show the histogram. So to display it. Once we obtain the blue color of the image and use the histogram method by passing the array and the beans to put in the graphic, you plot it using plotshow. Now let's.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







