Python image grayscale transformation and image array manipulation
Mingchaosun font: [Increase decrease] Type: Reprint time: 2016-01-27 I want to comment
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.
?
123456789101112 |
# -*- 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
|
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.
?
123456789101112 |
# -*- 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()
|
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:
?
123456789 |
# -*- 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
]
|
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:
?
12345678910111213141516171819202122232425262728293031323334 |
#-*- 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()
|
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 ~
Image grayscale transformation and image array manipulation