Digital Image Processing Notes (Python) [1]

It has been a while since the study of digital image processing books. This article is a summary of what I have learned in the past. On the one hand, it is to deepen the understanding of digital image processing related knowledge. On the other hand, I also hope to get rid of the information cocoon of books and program more "freely". It is knowledge that has not been learned before. This person has little knowledge, and the operations involved in this article are relatively simple. The fundamental purpose is to lay a solid foundation for possible future scientific research work, and readers are requested to bear with them.

Table of contents

1. Image reading, modification and writing

1. Common methods of image reading

(1)PIL

(2)matplotlib

(3)opencv-python 

2. Image modification operation

(1) Pixel color changes

(2) Image scaling, translation and rotation

3. Image save operation

(1) Use the PIL library

(2) Use the OpenCV library


Reading, modifying and writing of an image

1. Common methods of image reading

(1)PIL

Using the Image function in PIL, this function does not return a numpy object and needs to be converted and output.

import numpy as np
from PIL import Image

img1 = Image.open('image/1.jpg')
print(img1)
# 显示图像
img1.show()
# Image函数读取出的不是array格式(不返回numpy对象),因此需要进行转换
img1_array = np.array(img1)
# 输出分辨率及通道数
print(img1_array.shape)

The information that can be obtained is: the image format is RGB, the resolution is 1078×1078, and three channels.

(2)matplotlib

This method reads out a numpy object, and directly outputs the result as the pixel value of the image.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img1 = mpimg.imread('image/1.jpg')
print(img1.shape)
print(img1)
plt.imshow(img1)
plt.show()

In addition to the resolution and the number of channels, the output also gets a large number of matrices, which actually represent the pixel values ​​​​of the image.
Taking this image as an example, the resolution of 1078×1078 means that the image is composed of 1078 (rows)×1078 (columns) pixels, and the color of each pixel of the RGB image is composed of three color components: R, G, and B. The range of component values ​​is [0,255]. This principle is reflected in the output results, that is, the red box matrix in the figure indicates the RGB value of the first pixel, the blue box indicates the RGB value of the first row of pixels, and the yellow box indicates the RGB value of all pixels in the image.

(3)opencv-python 

The read image format is numpy, and the image data is stored in BGR format.

import cv2
img1 = cv2.imread('image/1.jpg')
print(img1)
# 命名窗口 namedWindow()函数的使用:https://blog.csdn.net/xykenny/article/details/90513480
cv2.namedWindow("AvA")
# 显示图像
cv2.imshow("AvA",img1)
cv2.waitKey()

It can be seen that the output matrix value has changed, and it is output in BGR.
opencv can also read different image color types:

import cv2
img1 = cv2.imread('image/1.jpg',1)# 读取彩色图像
img1 = cv2.imread('image/1.jpg',0)# 读取灰度图像
img1 = cv2.imread('image/1.jpg',-1)# 按图片原有格式进行读取

Since imread reads a three-channel image by default, for a binary image, it can only be read in the original format, otherwise the image will be automatically converted to a three-channel image.

2. Image modification operation

(1) Pixel color changes

The following is an operation to modify the pixel color of the read image, which mainly involves reading the image size and changing the RGB value of the corresponding pixel.

from PIL import Image

img1 = Image.open('image/1.jpg')
pix = img1.load()   # 导入像素
width = img1.size[0]   # 获取宽度
height = img1.size[1]   # 获取长度
temp = 1
for x in range(width):   # 遍历宽度
    temp += 1
    for y in range(height):   # 遍历长度
        if temp % 5 == 0:   # 每隔5个像素点就涂黑一个点
            img1.putpixel((x,y),(0,0,0))
img1.show()

(2) Image scaling, translation and rotation

import cv2
import numpy as np

img1 = cv2.imread('image/1.jpg')
rows,cols,channel = img1.shape   # 获取原始长宽通道数

img11 = cv2.resize(img1,(500,300))   # 缩放为500×300分辨率

img12 = cv2.resize(img1,(int(cols*0.3),int(rows*0.6)))   # 按比例缩放

M1 = cv2.getRotationMatrix2D((cols/2,rows/2),60,0.5)   # 定义旋转参数,第一个参数为旋转中心,第二个为角度,第三个为旋转后缩放比例
img13 = cv2.warpAffine(img1,M1,(cols,rows))   # 第一个参数为图像,第二个为旋转参数,第三个为图像宽高

M2 = np.float32([[1,0,0],[0,1,300]])   # 定义平移参数
img14 = cv2.warpAffine(img1,M2,(cols,rows))

img15 = cv2.flip(img1,0)   # 0以x轴为对称轴翻转,>0以y轴为对称轴翻转,<0以x轴y轴翻转

cv2.imshow("resize1",img11)
cv2.imshow("resize2",img12)
cv2.imshow("rotation",img13)
cv2.imshow("move",img14)
cv2.imshow("flip",img15)
cv2.waitKey()

If there are too many pictures, I won't post them. Readers can refer to the comments in the code segment, modify some parameters, and observe the effect.

3. Image save operation

(1) Use the PIL library

Take the result of modifying pixels as an example, save the image locally. We read pictures in jpg format, but they can also be saved in other formats.

from PIL import Image

img1 = Image.open('image/1.jpg')
pix = img1.load()   # 导入像素
width = img1.size[0]   # 获取宽度
height = img1.size[1]   # 获取长度
temp = 1
for x in range(width):   # 遍历宽度
    temp += 1
    for y in range(height):   # 遍历长度
        if temp % 5 == 0:   # 每隔5个像素点就涂黑一个点
            img1.putpixel((x,y),(0,0,0))
img1.save('image/11.png')   # 保存png图片

(2) Use the OpenCV library

import cv2

img1 = cv2.imread('image/1.jpg')
img_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)   # 转化为灰度图
print(img_gray)   # 输出灰度图像素值
cv2.imwrite('image/11.jpg', img_gray)
img11 = cv2.imread('image/11.jpg')
print(img11)   # 输出保存图像像素值

imread reads three-channel images, and imwrite can only save BGR three-channel images, so even if the original image is converted into a grayscale image, the saved image is still an RGB image. This can be verified by outputting images. One value of img_gray corresponds to one pixel, which is a single channel; three values ​​of img11 correspond to one image, which is three channels.

Guess you like

Origin blog.csdn.net/fjyalzl/article/details/126990753