Python data analysis and mining - image processing

Series Article Directory



foreword

With the popularity of big data today, data processing and analysis have become crucial aspects. 2 What we are going to learn in this section is to deal with pictures. Use code to process some images. and to recognize the nature of pictures.

Mosaic of pictures

We all know that in life we ​​sometimes try not to reveal our information. Photos sometimes use mosaics to hide our basic appearance information. Although a lot of software has been launched on mobile phones to perform mosaic processing on pictures, most of the software mosaics are already set and their values ​​cannot be changed. And today we can use the matplotlib module in Python to realize the mosaic of pictures.

1. Install matplotlib, numpy and other modules

When writing python, VS code is used by some people, and there is also a pycharm compiler. The vs code compiler is introduced here.
1. Press and hold the win+R key, open the control terminal, and enter the cmd command;
insert image description here
2. Enter the command in the opened user control interface pip install [模块];
insert image description here
as shown in the figure above, enter the module to be downloaded, and the module can be used after the download is successful. For students who use the pycharm compiler, just open the pycharm terminal and enter pip install [模块]
.
3. Using the module
We mainly use the image in matplotlib, so we import the module;

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

After importing the module, we can process the image.

2. Mosaic pictures

1. Import pictures

matplotlib.image中的imread()函数就是用来读取图片的编码的。我们可以提前将要使用的图片放在该目录下,也可直接导入,这里博主就使用上次爬虫得到的图片。具体的图片你自己根据自己的情况来定
data_jpg=mpimg.imread('D:\CSDN图片\work.jpg') #导入图片,解码
insert image description here
Let's print his value, we will find that the picture is a three-dimensional array after decoding. And our operations on him are all operations on this array.

2. Positioning area

We locate the position that needs to be mosaicked, ( imshow()函数)
insert image description here
We will find that a scale appears around the picture we imported, and we can estimate the position we want to code based on the scale on it. After finding the coding position, cut it;
data1=data_jpg[50:320,150:350]
insert image description here
then we will get a cut picture. We now perform fine pixel segmentation on this image.
data1=data1[::27,::40]Define the value of the segmentation according to your own needs. Here, it means that the pixels of the picture are divided into one pixel at an interval of 27 (y-axis) and 40 (x-axis). Mosaic will be formed.
insert image description here

3. Synthesis of pictures

Finally, copy the image area that has been mosaicked above to the original image;

for i in range(10):
    for j in range(5):
        data2[50+27*i:50+27+27*i,150+40*j:150+40+40*j]=data1[i,j]

Finally, just display the picture, if you need to save it, just use the savefig()function;
now let's take a look at the final effect;
insert image description here
源代码展示;

import matplotlib.image as mpimg
import math
import numpy as np
import matplotlib.pyplot as plt
data_jpg=mpimg.imread('D:\CSDN图片\work.jpg')
#plt.imshow(data_jpg)
#马赛克;
data2=data_jpg.copy()
data1=data_jpg[50:320,150:350]
#plt.imshow(data1)
data1=data1[::27,::40]
#plt.imshow(data1)
for i in range(10):
    for j in range(5):
        data2[50+27*i:50+27+27*i,150+40*j:150+40+40*j]=data1[i,j]
plt.imshow(data2)

picture stitching

The processing of python squadron pictures, in addition to mosaicing, can also perform functions such as discoloration, virtualization, gray image processing, picture stitching, etc.
concatenate()函数
The stitching of pictures is realized by the concatenate() function;
insert image description here

import matplotlib.image as mpimg
import math
import numpy as np
import matplotlib.pyplot as plt
data_jpg=mpimg.imread('D:\CSDN图片\work.jpg')
data3=np.concatenate([data_jpg,data_jpg],axis=1)
plt.imshow(data3)

grayscale image

1. max() method

As mentioned above, the composition of the picture is a three-dimensional array, and the numbers in it represent their image characteristics. Therefore, the grayscale of the image can also change the color and shape of the image by changing their position, value, and dimension.
We can see the following example;

import matplotlib.image as mpimg
import math
import numpy as np
import matplotlib.pyplot as plt
data_jpg=mpimg.imread('D:\CSDN图片\work.jpg')
print(data_jpg)
[[[215 221 211]
  [215 221 211]
  [215 221 211]
  ...
  [204 189 182]
  [203 188 181]
  [202 187 180]]

 [[215 221 211]
  [215 221 211]
  [215 221 211]
  ...
  [204 189 182]
  [203 188 181]
  [202 187 180]]

 [[215 221 211]
  [215 221 211]
  [215 221 211]
  ...
  [204 189 182]
  [203 188 181]
  [201 186 179]]

 ...
...
  ...
  [188 150 137]
  [189 151 138]
  [189 151 138]]]
data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
#最大值法;
s=np.max(data_jpg,axis=-1)#axis取-1就是为了得到最里面的值;
plt.imshow(s,cmap='gray') #cmap参数表示最后的颜色,gray时灰色;

Show results;
insert image description here

Two.min() method

Similar to the max() method, so I won't go into too much detail here, just paste the code directly;

data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
sss=np.min(data_jpg,axis=-1)
plt.imshow(sss,cmap='gray')
3. Average method mean() function
data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
#最大值法;
s=np.max(data_jpg,axis=-1)
plt.imshow(s,cmap='gray')

The effect is shown;
insert image description here
the effect obtained by the above method is basically similar. So, just master one of them.

4. Weighted average method

Different from the average method, the weighted average method can be grayed according to the data given by itself;

data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
weight=[0.01,0.01,0.01]  #这里的值自己更改,值越大,灰度化颜色偏深,反之则偏浅.
ss=np.dot(data_jpg,weight)
plt.imshow(ss,cmap='gray')

insert image description here

image segmentation

Sometimes we only need a small part of the picture, then we can do it by dividing the function split();

one,data4,two=np.split(data_jpg,indices_or_sections=[100,400],axis=1) #中间的参数表示我们分割的区间;
plt.imshow(data4)
plt.imshow(one)
plt.imshow(two)

Show results;

insert image description here

Summarize

That's all I'm going to introduce in this section. There are many ways to process pictures. I won't introduce too much here, and I will continue to update them later. Using code to process pictures will have a lot of flexible use space, not limited to the limitations of mobile phone software, so it is also very beneficial to master these methods. The
source code is placed below, and interested students can take a look.

source code


import matplotlib.image as mpimg
import math
import numpy as np
import matplotlib.pyplot as plt
data=mpimg.imread("./onetemp.jpg")
data1=data.copy()
#打马赛克;
#第一步,找出要打马赛克的位置;
data2=data1[200:900,100:800]
#第二步,将图片比例的收缩;
data2=data2[::35,::35]
print(data2.shape)
datatemp=data1.copy()
#第三步,替换,将现有的替换到原来的,记住两个尺寸不一样;
for i in range(20):
    for j in range(20):
        datatemp[200+35*i:200+35*i+35,100+35*j:100+35*j+35]=data2[i,j]
plt.imshow(datatemp)

#图片的拼接;
data4=np.concatenate([data,data])#axis=1时,并排着;
plt.imshow(data4)
data5=np.concatenate([data,data],axis=1)
plt.imshow(data5)
#图片切割;
one,two,three=np.split(data,indices_or_sections=[400,600],axis=1)
plt.imshow(one)
plt.imshow(two) 
plt.imshow(three)

#图像灰度化;
#第一种方法,最小值法;
data=mpimg.imread('./onetemp.jpg')
#data=np.min(data,axis=-1)
#plt.imshow(data,cmap='gray')
#第二种方法,最小值法;
data1=np.max(data,axis=-1)
#plt.imshow(data1,cmap='gray')
#第三种,平均值法;
data3=np.mean(data,axis=-1)#等价于data.mean()函数;
#plt.imshow(data3,cmap='gray')

#第四种方法,加权平均值法,dot函数;
weight=[0.299,0.345,0.456]
data6=np.dot(data,weight)
plt.imshow(data6,cmap='gray')


data_jpg=mpimg.imread('D:\CSDN图片\work.jpg')
print(data_jpg)
#plt.imshow(data_jpg)
#马赛克;
data2=data_jpg.copy()
data1=data_jpg[50:320,150:350]
#plt.imshow(data1)
data1=data1[::27,::40]
#plt.imshow(data1)
for i in range(10):
    for j in range(5):
        data2[50+27*i:50+27+27*i,150+40*j:150+40+40*j]=data1[i,j]
plt.imshow(data2)


#照片的合集;
import matplotlib.image as mpimg
import math
import numpy as np
import matplotlib.pyplot as plt
data_jpg=mpimg.imread('D:\CSDN图片\work.jpg')
data3=np.concatenate([data_jpg,data_jpg],axis=1)
plt.imshow(data3)

#图片的切割;
one,data4,two=np.split(data_jpg,indices_or_sections=[100,400],axis=1)
plt.imshow(data4)
#plt.imshow(one)
#plt.imshow(two)

#图像的灰度化;
data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
#最大值法;
s=np.max(data_jpg,axis=-1)
plt.imshow(s,cmap='gray')
#加权平均值法;
data_jpg=mpimg.imread("D:\CSDN图片\work.jpg")
weight=[0.31,0.41,0.53]
ss=np.dot(data_jpg,weight)
plt.imshow(ss,cmap='gray')

#平均值法;
smp=np.mean(data_jpg,axis=-1)
plt.imshow(smp,cmap='gray')

#最小值法;
sss=np.min(data_jpg,axis=-1)
plt.imshow(sss,cmap='gray')


Guess you like

Origin blog.csdn.net/qq_59931372/article/details/127470557