# 數據建模-數據集特征獲取
*****
**以下內容均按照作者的個人思路去編排,如果有更好的想法等,則以自己的優先**
*****
## 一、過濾圖像數據集
待更新......
## 二、按類別求圖像(集)的均值、方差
1. 求圖像的均值、方差
```
import cv2
img1 = cv2.imread("D:/Source Code/Python/Conda/AI/data/seg_data/seg_train/seg_train/forest/8.jpg",1)
print (img1[0][0])#輸出原始圖像的通道值
mean_image = np.mean(img1, axis=0)#對圖像求均值
var_image = np.var(img1, axis=0)#對圖像求方差
print ("-----------------------------------------------")
print ("mean_image的形狀以及數值")
print (mean_image.shape)
print (mean_image[0])
print ("var_image的形狀以及數值")
print (var_image.shape)
print (var_image[0])
print ("-----------------------------------------------")
```
結果:(與之類似即可)

2. 求圖像集的均值、方差
```
import os
from PIL import Image #加載圖像
import matplotlib.pyplot as plt
import numpy as np
from imageio import imread #讀取圖像
filepath = r'D:/Source Code/Python/Conda/AI/data/seg_data/seg_train/seg_train/forest'#載入數據集目錄
pathDir = os.listdir(filepath)#返回指定的文件夾包含的文件或文件夾的名字的列表給pathDir
R_channel = 0#R通道=0
G_channel = 0#G通道=0
B_channel = 0#B通道=0
for idx in range(len(pathDir)):#將索引指定為循環體為0~pathDir的長度-1
filename = pathDir[idx]#將包含索引的pathDir賦給filename
img = imread(os.path.join(filepath, filename)) / 255.0#將'filepath'與'filename'拼接在一起并歸一化,之后賦給img
R_channel = R_channel + np.sum(img[:, :, 0])#獲取讀取到的img中,單通道0的像素值并求和,之后與R通道的值合并在一起
G_channel = G_channel + np.sum(img[:, :, 1])#獲取讀取到的img中,單通道1的像素值并求和,之后與G通道的值合并在一起
B_channel = B_channel + np.sum(img[:, :, 2])#獲取讀取到的img中,單通道2的像素值并求和,之后與B通道的值合并在一起
#一張RGB圖像可以看成一個三維的矩陣,矩陣中的每一個數表示了圖像上不同位置,不同顏色的亮度。
num = len(pathDir) * 155 * 155 #這里(155*155)是每幅圖片的大小,所有圖片尺寸必須都一樣,將pathDir的個數*155*155賦給num
R_mean = R_channel / num#R的均值為'R通道的值÷num'
G_mean = G_channel / num#G的均值為'G通道的值÷num'
B_mean = B_channel / num#B的均值為'B通道的值÷num'
R_channel = 0
G_channel = 0
B_channel = 0
for idx in range(len(pathDir)):
filename = pathDir[idx]
img = imread(os.path.join(filepath, filename)) / 255.0
#獲取讀取到的img中,單通道0的像素值并求和,之后減去R的均值,再將得到的值平方,然后與R通道的值合并在一起賦給R_channel
R_channel = R_channel + np.sum((img[:, :, 0] - R_mean) ** 2)
#獲取讀取到的img中,單通道1的像素值并求和,之后減去G的均值,再將得到的值平方,然后與G通道的值合并在一起賦給G_channel
G_channel = G_channel + np.sum((img[:, :, 1] - G_mean) ** 2)
#獲取讀取到的img中,單通道2的像素值并求和,之后減去B的均值,再將得到的值平方,然后與B通道的值合并在一起賦給B_channel
B_channel = B_channel + np.sum((img[:, :, 2] - B_mean) ** 2)
R_var = np.sqrt(R_channel / num)#R的方差為'R通道的值÷num再開方'
G_var = np.sqrt(G_channel / num)#G的方差為'G通道的值÷num再開方'
B_var = np.sqrt(B_channel / num)#B的方差為'B通道的值÷num再開方'
print("R_mean is %f, G_mean is %f, B_mean is %f" % (R_mean, G_mean, B_mean))
print("R_var is %f, G_var is %f, B_var is %f" % (R_var, G_var, B_var))
```
結果:(與之類似即可)

* 拓展學習(統一圖片尺寸):
```
from PIL import Image
import os
def image_resize(image_path, new_path): # 統一圖片尺寸
print('============>>修改圖片尺寸')
for img_name in os.listdir(image_path):
img_path = image_path + "/" + img_name # 獲取該圖片全稱
image = Image.open(img_path) # 打開特定一張圖片
image = image.resize((512, 512)) # 設置需要轉換的圖片大小
image.save(new_path + '/'+ img_name) # 按照原圖像名稱保存圖像至新路徑
print("end the processing!")
if __name__ == '__main__':
print("ready for :::::::: ")
ori_path = r"Z:\pycharm_projects\ssd\VOC2007\JPEGImages" # 輸入圖片的文件夾路徑
new_path = 'Z:/pycharm_projects/ssd/VOC2007/reshape' # 轉換之后的文件夾路徑,注意反斜杠
image_resize(ori_path, new_path)
```