第九章 基于水色图像的水质评价
9.1 背景与挖掘目标
有经验的渔业从业者可以通过观察水色的变化来调控水质,以维持水产养殖水生态系统中浮游植物、微生物和浮游动物的合理动态平衡。在水质在线监测方面,数字图像处理技术基于计算机视觉和专家经验对池塘水质进行分类,实现对池塘水色的准确快速识别。
[En]
Experienced fishery practitioners can regulate and control water quality by observing the changes of water color in order to maintain a reasonable dynamic balance of phytoplankton, microorganisms and zooplankton in the aquaculture water ecosystem. In the aspect of on-line water quality monitoring, the digital image processing technology is based on computer vision and expert experience to classify the water quality of the pond and realize the accurate and rapid discrimination of the water color of the pond.
9.2 分析方法与过程
水样图像是通过拍摄水样获得的,但图像数据的维度太大,无法进行分析,因此有必要对水样图像进行特征提取,提取一些能反映图像本质的关键指标。从而达到图像的自动识别或分类的目的。因此,图像特征提取是图像识别或分类的关键步骤,图像特征提取的效果直接影响到图像识别和分类的质量。
[En]
The water sample image is obtained by shooting the water sample, but the dimension of the image data is too large to be analyzed, so it is necessary to extract the features of the water sample image and extract some key indexes that reflect the essence of the image. in order to achieve the purpose of automatic image recognition or classification. Therefore, * image feature extraction * is a key step in image recognition or classification, and the effect of image feature extraction directly affects the quality of image recognition and classification.
图像特征主要包括颜色特征、纹理特征、形状特征和空间关系特征。与几何特征相比,颜色特征更稳健,对物体的大小和方向不敏感,表现出较强的稳健性。一般的颜色特征是基于像素点的,属于图像或图像区域的所有像素都有自己的贡献。对于颜色特征,我们主要采用了颜色处理中常用的直方图方法和颜色矩方法。
[En]
Image features mainly include color features, texture features, shape features and spatial relationship features. Compared with geometric features, * color features * are more robust, insensitive to the size and direction of objects, and show strong robustness. General color features are based on pixel points, and all pixels belonging to the image or image region have their own contributions. For color features, we mainly use the histogram method and color moment method which are commonly used in color processing.
(1)颜色直方图
颜色直方图反映的是图像中颜色的组成分布,即 出现了哪些颜色以及各种颜色出现的概率。其优点在于它能简单描述一幅图像中颜色的全局分布,即不同色彩在整幅图像中所占的比例,适用于描述那些难以自动分割的图像和不需要考虑物体空间位置的图像。缺点在于它无法描述图像中颜色的局部分布及每种色彩所处的空间位置,即无法描述图像中某一具体的对象或物体。
(2)颜色矩方法
基于颜色矩提取图像特征的数学基础为 图像中任何的颜色分布均可以用它的矩来表示。据随机变量的概率分布可以由其各阶矩唯一的表示和描述。一幅图像的色彩分布也可认为是一种概率分布,则图像也可以由其各阶矩来描述。颜色矩包含各个颜色通道的一阶矩、二阶矩和三阶矩,对于一幅RGB颜色空间的图像, 具有R、G和B三个颜色通道,则有9个分量。
本案例选择采用产生特征维数较小的颜色矩来提取水样图像特征。
分析步骤:
1)从采集到的原始水样图像中进行选择性抽取与实时抽取,形成建模数据和增量数据。
2)对1)形成的两个数据集进行数据预处理,包括图像切割和颜色矩特征提取。
3)利用2)形成的已完成数据预处理的建模数据,由有经验的专家对水样图像根据经验进行分类,构建专家样本。
4)利用3)的专家样本构建分类模型。
5)利用4)的构建好的分类模型进行水质评价。
9.2.1 数据预处理
1.图像切割
避免容器颜色的干扰并且水体位于图像中央,为了提取水色的特征,需要提取水样图像中央具有代表意义的图像,即 提取水样图像中央101x101像素的图像。设原始图像I的大小是MxN,则截取宽从第f i x ( M 2 ) − 50 fix(\frac{M}{2})-50 f i x (2 M )−5 0个像素点到第f i x ( M 2 ) + 50 fix(\frac{M}{2})+50 f i x (2 M )+5 0个像素点,长从第f i x ( N 2 ) − 50 fix(\frac{N}{2})-50 f i x (2 N )−5 0到第f i x ( N 2 ) + 50 fix(\frac{N}{2})+50 f i x (2 N )+5 0个像素点的子图像。
2.特征提取
(1)一阶颜色矩
一阶颜色矩采用一阶原点矩,反映图像的整体明暗程度。
E i = 1 N ∑ j = 1 N p i j E_{i}=\frac{1}{N}\sum_{j=1}^{N}p_{ij}E i =N 1 j =1 ∑N p i j
其中,Ei是在第i个颜色通道的一阶颜色矩,对于RGB颜色空间的图像,i=1,2,3,pij是第j个像素的第i个颜色通道的颜色值。
(2)二阶颜色矩
二阶颜色矩采用二阶中心距的平方根,反映了图像的颜色分布范围。
[En]
The second-order color moment uses the square root of the second-order center distance, which reflects the color distribution range of the image.
σ i = 1 N ∑ j = 1 N ( p i j − E i ) 2 σ_{i}=\sqrt{\frac{1}{N}\sum_{j=1}^{N}(p_{ij}-E_{i})^{2}}σi =N 1 j =1 ∑N (p i j −E i )2
其中,σi是在第i个颜色通道的二阶颜色矩,Ei是在第i个颜色通道的一阶颜色矩。
(3)三阶颜色矩
三阶颜色矩采用三阶中心距的三次根,反映了图像颜色分布的对称性。
[En]
The cubic root of the third-order center distance is used in the third-order color moment, which reflects the symmetry of the color distribution of the image.
s i = 1 N ∑ j = 1 N ( p i j − E i ) 3 3 s_{i}=\sqrt[3]{\frac{1}{N}\sum_{j=1}^{N}(p_{ij}-E_{i})^{3}}s i =3 N 1 j =1 ∑N (p i j −E i )3
其中,si是在第i个颜色通道的三阶颜色矩,Ei是在第i个颜色通道的一阶颜色矩。
提取切割后的图像颜色矩,作为图像的颜色特征。
import numpy as np
import os,re
import pandas as pd
from PIL import Image
path = './test/data/images/'
def getImgNames(path=path):
'''
获取指定路径中所有图片的名称
:param path: 指定的路径
:return: 名称列表
'''
filenames = os.listdir(path)
imgNames = []
for i in filenames:
if re.findall('^\d_\d+\.jpg$', i) != []:
imgNames.append(i)
return imgNames
def Var(data=None):
'''
获取给定像素值矩阵的三阶颜色矩
:param data: 给定的像素值矩阵
:return: 对应的三阶颜色矩
'''
x = np.mean((data-data.mean())**3)
return np.sign(x)*abs(x)**(1/3)
imgNames = getImgNames(path=path)
n = len(imgNames)
data = np.zeros([n, 9])
labels = np.zeros([n])
for i in range(n):
img = Image.open(path+imgNames[i])
M,N = img.size
img = img.crop((M/2-50,N/2-50,M/2+50,N/2+50))
r,g,b = img.split()
rd = np.asarray(r)/255
gd = np.asarray(g)/255
bd = np.asarray(b)/255
data[i,0] = rd.mean()
data[i,1] = gd.mean()
data[i,2] = bd.mean()
data[i,3] = rd.std()
data[i,4] = gd.std()
data[i,5] = bd.std()
data[i,6] = Var(rd)
data[i,7] = Var(gd)
data[i,8] = Var(bd)
labels[i] = imgNames[i][0]
data = pd.DataFrame(data)
labels = pd.DataFrame(labels)
data = pd.concat([labels, data], axis=1)
data.to_excel('./demo/data/model_data.xlsx',index=False)
9.2.2 模型构建
对特征提取后的样本进行抽样,抽取80%作为训练样本,剩下的20%作为测试样本,用于水质评价检验。
import pandas as pd
data = pd.read_csv('./demo/data/moment.csv', encoding='gbk')
data = data.values
from random import shuffle
shuffle(data)
data_train = data[:int(0.8*len(data)), :]
data_test = data[int(0.8*len(data)):, :]
print(len(data))
print(len(data_train))
print(len(data_test))
在这种情况下,采用支持向量机作为水质评价的分类模型。模型的输入包括训练样本的输入和建模参数的输入。
[En]
In this case, support vector machine is used as the classification model of water quality evaluation. The input of the model includes the input of training samples and modeling parameters.
其中1~9作为输入的特征,其取值都在0 ~ 1之间,如果直接输入SVM模型,彼此之间区分度会比较小,因此将所有特征都统一乘以一个适当的常数k,从而提高区分度和准确率。但k的选取不能过大或过小,可根据测试准确率来选择k的最优值。通过测试,本案例k的最优值为30。
from sklearn import svm
model = svm.SVC()
model.fit(x_train, y_train)
import pickle
pickle.dump(model, open('./demo/tmp/svm.model', 'wb'))
from sklearn import metrics
cm_train = metrics.confusion_matrix(y_train, model.predict(x_train))
cm_test = metrics.confusion_matrix(y_test, model.predict(x_test))
print(cm_train)
print(cm_test)
pd.DataFrame(cm_train, index=range(1, 4), columns=range(1, 4)).to_excel('./demo/tmp/cm_train.xlsx')
pd.DataFrame(cm_test, index=range(1, 5), columns=range(1, 5)).to_excel('./demo/tmp/cm_test.xlsx')
import warnings
warnings.filterwarnings("ignore")
print(metrics.classification_report(y_train, model.predict(x_train)))
print(metrics.classification_report(y_test, model.predict(x_test)))
from sklearn.metrics import accuracy_score
print(accuracy_score(y_train, model.predict(x_train)))
print(accuracy_score(y_test, model.predict(x_test)))
Original: https://blog.csdn.net/qq_54716845/article/details/124208068
Author: bib_i
Title: python数据与挖掘实战学习:实战篇 第九章基于水色图像的水质评价笔记

2021/11/2

win10安装 Anaconda + Pytorch + TensorFlow + CUDA + cuDNN + 在PyCharm使用pytorch或者tensorflow

tf 随机种子固定

TensorFlow保姆级别教入门,不会就把我头打爆

超详细!带你轻松掌握 MMSegmentation 整体构建流程

tensorflow 与protobuf版本对应问题

机器学习-卷积神经网络之深度残差网络CIFAR10实战(四)

tf.ConfigProto()详解

对话式AI——多轮对话拼接

芯片产业“冰火两重天”:谁在降价?谁在缺货?

L2正则化是否适用于所有类型的优化算法

项目实训12

计算机视觉学习第六章——图像聚类

大年初十七,安装了一天的tensorflow gpu
