This article mainly introduces Python image grayscale conversion and image array operations. For more information, see python and numpy to complete a series of basic image processing by directly operating the image array.
Numpy introduction:
NumPy is a well-known Python Scientific computing toolkit, which contains a large number of useful tools, such as array objects (used to represent vectors, matrices, images, etc.) and linear algebra functions.
An array object can be used to perform important operations in an array, such as matrix product, transpose, equation solving system, vector product, and normalization. This provides a foundation for image deformation, modeling changes, image classification, and image clustering.
In the previous python basic Image operation, when loading an image, the image is converted to a NumPy array object by calling the array () method. The array objects in NumPy are multidimensional and can be used to represent vectors, matrices, and images. Through direct operations on the array of images, you can complete a lot of image processing.
Numpy-related knowledge has a lot of information on the Internet. as the foundation of python scientific computing, it is worth studying carefully.
Basic Image operations using image arrays:
Recognition Image array:
Through the following programs, we can see an array of images with grayscale images, as well as a segment of the numpy array.
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # read the Image and convert it to an array im = array (Image. open (". /source/test.jpg ") # print im. shape, im. dtype # print im [100,100, 0] # print im [100,100] of the rgb value of output coordinate 100,100 and print im of the type output at coordinate 100,100 and color channel r. shape, im. dtype
Running result:
(600,500, 3) uint8
64
[64 117 195]
We see a three-dimensional array, which represents the abscissa, ordinate, and color channels.
We can exchange red and blue channels through arrays.
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # read the Image and convert it to an array im = array (Image. open (". /source/test.jpg ") # red Channel r = im [:,:, 0] # swap the red and blue channels and display im [:, 0] = im [:, :, 2] im [:,:, 2] = rimshow (im) show ()
The numpy array slicing method is used here. There are a lot of information about numpy on the Internet.
Running result:
In the process of converting to an array, we can set the data type, and the image array of the grayscale image is also meaningful:
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # read the Image, grayscale, and convert it to array im = array (Image. open (". /source/test.jpg "). convert ('L'), 'F') # print im of the dimension length and type of the output array. shape, im. dtype # print im [100,100]
Running result:
(600,500) float32
110.0
The extra parameter 'F' converts the data type of the array to a floating point number.
The grayscale image has no color information, so the shape tuples have only two values.
* The opposite operation of array () transformation can be completed using fromarray () of PIL, for example, im = Image. fromarray (im)
Simple application of image arrays-grayscale transformation:
Grayscale image:
A grayscale digital image is an image with only one sampling color per pixel. This type of image is usually displayed as a gray scale from the shortest Black to the brightest white.
You can use the following methods to convert an image to a gray scale:
1. Floating point algorithm: Gray = R * 0.3 + G * 0.59 + B * 0.11
2. integer method: Gray = (R * 30 + G * 59 + B * 11)/100
3. shift method: Gray = (R * 76 + G * 151 + B * 28)> 8;
4. average method: Gray = (R + G + B)/3;
5. green only: Gray = G;
After obtaining Gray through any of the above methods, replace R, G, and B in the original RGB (R, G, B) with Gray to form a new color RGB (Gray, gray, Gray), use it to replace the original RGB (R, G, B) is a grayscale image.
It has been used many times before. you can use convert ('L') to obtain grayscale images using python.
Grayscale conversion:
After reading images into NumPy array objects, we can perform any mathematical operations on them. A simple example is the grayscale transformation of the image. That is, the function f converts 0... 255 intervals (or 0... 1) ING to itself.
The following program has some simple grayscale transformations:
#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # read the Image, grayscale, and convert it to array im = array (Image. open (". /source/test.jpg "). convert ('L') im2 = 255-im # reverse image processing im3 = (100.0/255) * im + 100 # transform the image pixel value to 100... 200 interval im4 = 255.0 * (im/255.0) ** 2 # The image obtained after the pixel value is calculated by Square (quadratic function transformation to make the darker pixel value smaller) #2x2 display result use the first display original grayscale image subplot (221) title ('f (x) = X') gray () imshow (im) #2x2 display result: use the second display inverse diagram subplot (222) title ('f (x) = 255-X') gray () imshow (im2) #2x2 display result use third display 100-200 figure subplot (223) title ('f (x) = (100/255) * x + 100') gray () imshow (im3) #2x2 display result use the fourth display quadratic function transform graph subplot (224) title ('f (x) = 255 * (x/255) ^ 2') gray () imshow (im4) # print int (im. min (), int (im. max () print int (im2.min (), int (im2.max () print int (im3.min (), int (im3.max () print int (im4.min ()), int (im4.max () show ()
Running result:
0 255
0 255
100 200
0 255
You can see the result of the gray scale transformation. The second image is displayed in reverse phase. the dark part of the third image is highlighted and the bright part is dimmed. The value is limited to 100 to 200, the last image uses quadratic function transformation to make the darker pixel values darker.
Conclusion:
This blog introduces how python uses image arrays to perform image operations, including several simple examples. through arrays, we can perform any mathematical operations on images, it is the foundation of image deformation, image classification, and image clustering. I hope my blog will help you ~