Python image grayscale transformation and image array operation methods

高洛峰
Release: 2017-03-13 18:14:49
Original
2238 people have browsed it

This article mainly introduces the related information of Pythonimage grayscale transformation and imagearrayoperation. Friends in need can refer to the following

Using python and numpy Complete a series of basic image processing by directly operating image arrays

Introduction to numpy:

NumPy is a very famous Python scientific computing toolkit , which contains a large number of useful tools, such as arrays objects (used to represent vectors, matrices, images, etc.) and linear algebra functions .

Array objects can implement important operations in arrays, such as matrix products, transposes, solving equation systems, vector products, and normalization. This provides the basis for image deformation, modeling changes, image classification, image clustering, etc.

In the previous article on basic python image operations, when loading an image, the image is converted into a NumPy array object by calling the array() method. Array objects in NumPy are multidimensional and can be used to represent vectors, matrices, and images. A lot of image processing can be done by directly operating on arrays of images.

There is a lot of information about numpy on the Internet. As the basis of python scientific computing, it is worth studying seriously.

Use image arrays for basic image operations:

Understand image arrays:

Through the following programs we take a look at the image arrays of images and grayscale images, as well as numpy A slice of an array.

# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片并转为数组
im = array(Image.open("./source/test.jpg"))
#输出数组的各维度长度以及类型
print im.shape,im.dtype
#输出位于坐标100,100,颜色通道为r的像素值
print im[100,100,0]
#输出坐标100,100的rgb值
print im[100,100]及类型
print im.shape,im.dtype
Copy after login


Run result:

(600, 500, 3) uint8
64
[64 117 195]

What we see is a three-dimensional array, representing the abscissa, ordinate and color channel respectively.

We can exchange the red and blue channels through the array


# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片并转为数组
im = array(Image.open("./source/test.jpg"))
#红色通道
r = im[:,:,0]
#交换红蓝通道并显示
im[:,:,0] = im[:,:,2]
im[:,:,2] = r
imshow(im)
show()
Copy after login


The numpy array slicing method is used here, about There is a lot of information on numpy online, so I won’t go into details.

Run result:

Python image grayscale transformation and image array operation methods

In the process of converting to an array, we can set the data type, and the grayscale image at the same time Arrays are also meaningful:


# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片,灰度化,并转为数组
im = array(Image.open("./source/test.jpg").convert('L'),'f')
#输出数组的各维度长度以及类型
print im.shape,im.dtype
#输出坐标100,100的值
print im[100,100]
Copy after login


Running results:

(600, 500) float32
110.0

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

Since the grayscale image has no color information, the shape tuple has only two values.

*The opposite operation of array() transformation can be completed using PIL's fromarray(), such as im = Image.fromarray(im)

Simple application of image array - grayscale transformation:

Grayscale images:

A grayscale digital image is an image with only one sampled color per pixel. Such images typically appear as grayscales ranging from darkest black to lightest white.

You can convert the image to grayscale through the following methods:

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. Only take green: Gray=G;

After obtaining Gray through any of the above methods, replace the R, G, and B in the original RGB (R, G, B) with Gray to form a new color RGB (Gray, Gray, Gray), using It replaces the original RGB (R, G, B) with a grayscale image.

It has been used many times before. Using python, you can obtain the grayscale image by using convert('L')

Grayscale transformation:

After reading the images into NumPy array objects, we can perform arbitrary mathematical operations on them. A simple example is the grayscale transformation of an image. That is, any function f, which maps the 0...255 interval (or the 0...1 interval) to itself.

There are some simple grayscale transformations in the following program:


#-*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片,灰度化,并转为数组
im = array(Image.open("./source/test.jpg").convert('L'))
im2 = 255 - im # 对图像进行反相处理
im3 = (100.0/255) * im + 100 # 将图像像素值变换到 100...200 区间
im4 = 255.0 * (im/255.0)**2 # 对图像像素值求平方后得到的图像(二次函数变换,使较暗的像素值变得更小)
#2x2显示结果 使用第一个显示原灰度图
subplot(221)
title('f(x) = x')
gray()
imshow(im)
#2x2显示结果 使用第二个显示反相图
subplot(222)
title('f(x) = 255 - x')
gray()
imshow(im2)
#2x2显示结果 使用第三个显示100-200图
subplot(223)
title('f(x) = (100/255)*x + 100')
gray()
imshow(im3)
#2x2显示结果 使用第四个显示二次函数变换图
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()
Copy after login


Running results:

Python image grayscale transformation and image array operation methods

0 255
0 255
100 200
0 255

You can clearly see the result of the grayscale transformation, No. The two images are displayed in reverse phase. The dark parts of the third image become brighter and the bright parts become darker. The values ​​are limited between 100 and 200. The last image uses a quadratic function to transform the darker pixel values. Darker.

Conclusion:

This blog introduces the process of using image arrays to perform image operations in Python, including several simple examples. Through arrays, we can perform any mathematical operations on images, which is the basis for image deformation, image classification, image clustering, etc. , I hope my blog will be helpful to everyone~

The above is the detailed content of Python image grayscale transformation and image array operation methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template