Gray-scale transformation of Python image and operation method of image array

Source: Internet
Author: User
This article mainly describes the Python image grayscale transformation and image array operation related data, the need for friends can refer to the following

Use Python and NumPy to perform a series of basic image processing by manipulating the image array directly

NumPy Introduction:

NumPy is a well-known Python Scientific Computing Toolkit that contains a number of useful tools, such as array objects (used to represent vectors, matrices, images, etc.) and linear algebra functions.

Array objects can implement important operations in the array, such as matrix multiplication, transpose, solution equation system, vector product, and normalization. This provides a basis for image distortion, modeling of changes, image classification, image clustering, etc.

In the previous Python basic image operation, when the image was loaded, the image was converted to an NumPy array object by calling the array () method. The array objects in the NumPy are multidimensional and can be used to represent vectors, matrices, and images. A lot of image processing can be done by directly manipulating the array of images.

NumPy's knowledge on the web has a lot of information, as the basis for the scientific calculation of Python, or very worthy of serious study.

Use an image array for basic image manipulation:

Recognize an array of images:

Let's look at a picture array with a grayscale image, and a slice of the numpy array, using these few programs.

#-*-Coding:utf-8-*-from PIL Import imagefrom pylab import * #读取图片并转为数组im = Array (Image.open ("./source/test.jpg")) #输出数组的 Length of each dimension and type print im.shape,im.dtype# output at coordinate 100,100, pixel value for color channel R im[100,100,0] #输出坐标100, 100 RGB value print im[100,100] and type print Im.shape,im.dtype


Operation Result:

(3) Uint8
64
[64 117 195]

What we see is a three-dimensional array that represents the horizontal axis, ordinate, and color channels, respectively.

We can swap the red and blue channels through an array.


#-*-Coding:utf-8-*-from PIL Import imagefrom pylab import * #读取图片并转为数组im = Array (Image.open ("./source/test.jpg")) #红色通道r = im[:,:,0] #交换红蓝通道并显示im [:,:, 0] = im[:,:,2]im[:,:,2] = Rimshow (IM) show ()


Here to use the numpy array of slices, about numpy information online There are many, not too much narrative.

Operation Result:

In the process of converting to an array, we can set the data type, and the image array of the grayscale image also makes sense:


#-*-Coding:utf-8-*-from PIL Import imagefrom pylab import * #读取图片, grayscale, and convert to array im = Array (Image.open ("./source/test.jpg"). C Onvert (' L '), ' F ') #输出数组的各维度长度以及类型print the value of the im.shape,im.dtype# output coordinate 100,100 print im[100,100]


Operation Result:

(float32)
110.0

The extra parameter ' F ' converts the data type of the array to a floating-point number

The shape tuple has only two values because the grayscale map has no color information

The reverse operation of the *array () transformation can be done using PiL's FromArray (), such as Im = Image.fromarray (IM)

Simple application of Image array--grayscale transformation:

Grayscale Image:

Grayscale digital images are images with only one sampled color per pixel. Such images are usually displayed as grayscale from the darkest black to the brightest white.

There are several ways to convert an image to grayscale:

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. Take green only: gray=g;

After any of the above methods to find gray, the original RGB (R,G,B) in the R,g,b unified with gray replace, the formation of a new color RGB (Gray,gray,gray), with it to replace the original RGB (R,G,B) is a grayscale map.

It has been used many times before, using Python to get a grayscale image by using convert (' L ')

Grayscale Transformation:

After the images are read into the NumPy array object, we can perform any mathematical operation on them. A simple example is the grayscale transformation of an image. That is, any function f, which will be 0 ... 255 intervals (or 0 ... 1 intervals) mapped to itself.

There are some simple grayscale transformations in the following program:


#-*-coding:utf-8-*-from PIL Import imagefrom pylab import * #读取图片, grayscale, and convert to array im = Array (Image.open ("./source/test.jpg"). C Onvert (' L ')) im2 = 255-im # inverting the image im3 = (100.0/255) * im + 100 # transform the image pixel value to the 100...200 interval IM4 = 255.0 * (im/255.0) **2 # Yes Image pixel value of the resulting image (two function transformation, so that the darker pixel values become smaller) #2x2显示结果 use the first display of the original grayscale subplot (221) title (' F (x) = X ') gray () imshow (IM) #2x2显示结果 Use the second display inverting diagram subplot (222) title (' F (x) = 255-x ') Gray () imshow (IM2) #2x2显示结果 use the third display 100-200 Figure subplot (223) title (' F (x) = (100/ 255) *x + () Gray () imshow (IM3) #2x2显示结果 use fourth display two function transformation diagram subplot (224) title (' F (x) =255 * (x/255) ^2 ') Gray (Imshow) # The maximum and minimum pixel values in the output graph are 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 ()


Operation Result:

0 255
0 255
100 200
0 255

Can be more obvious to see the results of the grayscale transformation, the second picture is reversed, the third image of the dark lit, the light darkened, the value is limited to 100 to 200, where the last image through a two-function transformation to make the darker pixel values become darker.

Conclusion:

This blog introduces Python to use image array to carry out the process of image manipulation, including a few simple examples, through the array we can do any mathematical operation of the image, image deformation, image classification, image clustering, etc., I hope my blog is helpful to everyone ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.