How do i import an image into a matrix in python?

An image may be defined as a two-dimensional function $f(x,y)$, where $x$ and $y$ are spatial coordinates, and the value of $f$ at any pair of coordinates $(x,y)$ is called the intensity of the image at that point. For a gray-range image, the intensity is given by just one value (one channel). For color images, the intensity is a 3D vector (three channels), usually distributed in the order RGB.

An image may be regarded as continuous with respect to $x$ and $y$, and also in intensity (analog image). Or as a discrete function defined on a discrete domain (digital image). Both viewpoints are useful for image processing.

Converting an analog image to digital form requires both the coordinates and the intensity to be digitized. Digitizing the coordinates is called sampling, while digitizing the intensity is referred to as quantization. Thus, when all this quantities are discrete, we call the image a digital image.

The opposite operation, converting from digital to analog, is also possible and called interpolation.

The result of sampling and quantization is a matrix of real numbers. The size of the image is the number of rows by the number of columns, $M\times N$. The indexation of the image in Python follows the usual convention:

$$\left(\begin{array}{cccc}a(0,0) & a(0,1) & \cdots & a(0,N-1) \\a(1,0) & a(1,1) & \cdots & a(1,N-1) \\\cdots & \cdots & \cdots & \cdots \\ a(M-1,0) & a(M-1,1) & \cdots & a(M-1,N-1)\end{array}\right)$$

$ conda install -c conda-forge opencv

Now you can import it with the other modules

Reading, displaying and writing images¶

Python supports most usual image formats. Let us load the lena.jpg image

It is a BGR image. We swap channels R and B to have an RGB image

The usual data type of an image is uint8, i.e. 8-bit unsigned integer. This gives $2^8 = 256$ intensity values which are distributed in the interval $[0,255]$. We'll comment on data types later on.

The variable a2 is a numpy array.

It has 512 rows, 512 columns and 3 layers.

And its elementes are unsigned integers of 8 bits.

For converting to other formats, in this case to a gray scale image

Image types and conversions¶

There are three main types of images:

  • Intensity image is a data matrix whose values have been scaled to represent intensities. When the elements of an intensity image are of class uint8 or class uint16, they have integer values in the range $[0,255]$ and $[0,65535]$, respectively. If the image is of class float32, the values are single-precission floating-point numbers. They are usually scaled in the range $[0,1]$, although it is not rare to use the sclae $[0,255]$ too.
  • Binary image is a black and white image. Each pixel has one logical value, $0$ or $1$.
  • Color image is like intensity image but with three chanels, i.e. to each pixel corresponds three intensity values (RGB) instead of one.

When performing mathematical transformations of images we often need the image to be of double type. But when reading and writing we save space by using integer codification. We use the following commands

This command converts the uint8 matrix into a float32 matrix.

This command first converts float32 numpy array a into a uint8 numpy array The second command saves the image.

Once we have the image defined as a float32 matrix, we may start working with it (processing).

Example.

We are going to

  • extract a part of the image by index restriction,
  • make a plot,
  • save the result.

We start with the extraction.


Exercise 1

Write a function with

  • input: an image of any class and some ranges for its $(x,y)$ pixels.

  • output: the matrix (float32) corresponding to the original image restricted to the given indices, and a figure of it.

Apply the function to extract the cameraman's head from cameraman.tif.


Exercise 2

Masks are geometric filters on an image. For instance, if we want to extract a region of an image, we may do it by multiplying the matrix of the original image by a matrix of equal size containing $1's$ in the region we want to keep and $0's$ otherwise. In this exercise we extract a circular region of the image lena_gray_512.tif of radious 150. Follow these steps:

  • Read the image and convert it to double.
  • Create a matrix of the same dimensions filled with zeros.
  • Modify the above matrix to contain $1's$ in a circle of radious 150, i.e. if $(j-c_x)^2+(i-c_y)^2<150^2$, where $(c_x,c_y)$ is the center of the image.
  • Multiply the image by the mask (they are matrices!)
  • Show the result.

When multiplying by zero, you set to black the pixels out of the circle. Modify the program to make visible those pixels with half the intensity.


Exercise 3

Linear degradation is the well known effect of darkening an image vertically (or horizontally). We may do this with a mask which is constant by columns but take decreasing values in rows, from 1 in the first row to zero in the last.

Construct such matrix and apply it to Lena's image. Save the resulting image to a file.

Hint: you may use loops and if's. But vetorizing saves execution time. Explore the commands np.linspace to make the degradation and numpy.tile to construct a repited (tiled) matrix from the linspace vector output.

If you open the book cited in the References at page 13, you will see the functions, methods and attributes defined for the module Image of PIL, such as crop, getextrema, getpixel, histogram, resize, or rotate.


Exercise 4

Create, as a numpy array, the image of a chess checkerboard, where the squares have a size of $250 \times 250$ pixels. Show the result in iPyhon terminal. You may use the command numpy.tile. Save the resulting image to a file.


Exercise 5

Create, as a numpy array, the image of concentric circels. The image has a size of $500 \times 500$ pixels and each circunference line is approximately $10$ pixels wide. Show the result in iPyhon terminal. Save the resulting image to a file.


Exercise 6

Create, as a numpy array, the image of the napkin. The squares have a size of $10 \times 10$. Show the result in iPyhon terminal. You may use the command numpy.tile. Save the resulting image to a file.


Exercise 7

Create, as a numpy array, the image shown below. The image a size of $500 \times 500$. Each circle has a radius of $10$ pixels and their centers are spaced $50$ pixels. Show the result in iPyhon terminal. You may use the command numpy.tile. Save the resulting image to a file.

How an image is converted to matrix?

A digital grayscale image is presented in the computer by pixels matrix. Each pixel of such image is presented by one matrix element – integer from the set . The numeric values in pixel presentation are uniformly changed from zero (black pixels) to 255 (white pixels).

How do you store an image in an array in Python?

Save NumPy Array as Image in Python.
Use the Image.fromarray() Function to Save a NumPy Array as an Image..
Use the imageio.imwrite() Function to Save a NumPy Array as an Image..
Use the matplotlib.pyplot.imsave() Function to Save a NumPy Array as an Image..
Use the cv2.imwrite() Function to Save a NumPy Array as an Image..

How do I load an image into a NumPy array?

How to Convert images to NumPy array?.
Convert a NumPy array to an image..
Reading images in Python..
Working with Images in Python..
Python PIL | Image.open() method..
Python PIL | Image.save() method..
OpenCV C++ Program for Face Detection..
Opencv Python program for Face Detection..

How do I import a JPEG into Python?

There are four libraries that are usually used for loading images..
Matplotlib — plt.imread().
OpenCV — cv2.imread().
Pillow — Image.open().
scikit-image — io.imread().