对高光谱图像进行处理的一个代码示例-以SVM对KSC数据集进行分类为例

代码可直接跑通。

 

————————

个人技术公众号:解决方案工程师

欢迎同领域的朋友关注、相互交流。

————————

#coding: utf-8
import spectral
import matplotlib.pyplot  as plt
from sklearn.svm import SVC
import numpy as np
from scipy.io import loadmat
'''
get the KSC data

'''
import_image = loadmat(r'F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.mat')['KSC']
output_image = loadmat(r'F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC_gt.mat')['KSC_gt']

# print import_image.shape
# print output_image.shape

np.unique(output_image)
# print np.unique(output_image)

'''
get the number of each class

'''

dict_k = {}

for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        if output_image[i][j] in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]:
            if output_image[i][j] not in dict_k:
                dict_k[output_image[i][j]] = 0
            dict_k[output_image[i][j]] += 1
# print dict_k   #{1: 761, 2: 243, 3: 256, 4: 252, 5: 161, 6: 229, 7: 105, 8: 431, 9: 520, 10: 404, 11: 419, 12: 503, 13: 927}
# print reduce(lambda x,y:x+y, dict_k.values())
#add up all of the valuse in dictionary of dict_k

'''
show the picture of HSI
'''
# ground_truth = spectral.imshow(classes=output_image.astype(int), figsize=(5,5))
# ksc_color = np.array()
# ground_truth = spectral.imshow(classes= output_image.astype(int), figsize=(6,6))
# plt.show(ground_truth)    #  it shows the original picture


#if u want the picture shows differents colors , do next

# ksc_color = np.array([
#     [255,255,255],
#     [184,40,99],
#     [74,77,145],
#     [35,102,193],
#     [238,110,105],
#     [117,249,76],
#     [114,251,253],
#     [126,196,59],
#     [234,65,247],
#     [141,79,77],
#     [183,40,99],
#     [0,39,245],
#     [90,196,111],
# ])
# ground_truth = spectral.imshow(classes= output_image.astype(int), figsize=(9,9),colors = ksc_color)
# plt.show(ground_truth)

'''
change mat to csv
'''
#重构需要用到的类

need_label = np.zeros([output_image.shape[0],output_image.shape[1]])
new_datawithlabel_list = []
for i in range(output_image.shape[0]):
    for j in range (output_image.shape[1]):
        if output_image[i][j] != 0 :
            need_label[i][j]=output_image[i][j]

for i in range (output_image.shape[0]):
    for j in range (output_image.shape[1]):
            if need_label[i][j] != 0 :
                c2l = list (import_image[i][j])
                c2l.append (need_label[i][j])
                new_datawithlabel_list.append(c2l)

new_datawithlabel_array = np.array(new_datawithlabel_list)

#标准化数据并储存
from sklearn import preprocessing
data_D = preprocessing.StandardScaler().fit_transform(new_datawithlabel_array[:,:-1])
data_L = new_datawithlabel_array[:,-1]

import pandas as pd
new = np.column_stack((data_D,data_L))
new_ = pd.DataFrame(new)
new_.to_csv(r'F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.csv',header = False , index = False)
#the above get the csv data

'''
Train the model , save the model

'''
import joblib
from sklearn.model_selection import KFold , train_test_split
from sklearn import metrics

#split train and test data
data = pd.read_csv('F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.csv', header= None)
data = data.as_matrix()
data_D = data [:,:-1]
data_L = data[:,-1]
data_train, data_test, label_train, label_test = train_test_split(data_D,data_L,test_size= 0.5)

#train the model
clf = SVC(kernel= 'rbf', gamma = 0.125, C= 16)
clf.fit(data_train,label_train)
pred = clf.predict(data_test)
accuracy = metrics.accuracy_score(label_test,pred)*100
print accuracy

#储存学习模型
joblib.dump(clf,'KSC_MODEL.m')


'''

模型预测,在图中标记

'''
#
testdata = np.genfromtxt('F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.csv',delimiter= ',')
# mat文件的导入
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat
import spectral


# KSC
input_image = loadmat('F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.mat')['KSC']
output_image = loadmat('F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC_gt.mat')['KSC_gt']


testdata = np.genfromtxt('F:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\Python_codes\shixiongdechengxu\HSIdata\KSC.csv',delimiter=',')
data_test = testdata[:,:-1]
label_test = testdata[:,-1]

# /Users/mrlevo/Desktop/CBD_HC_MCLU_MODEL.m
clf = joblib.load("KSC_MODEL.m")

predict_label = clf.predict(data_test)
accuracy = metrics.accuracy_score(label_test, predict_label)*100

print accuracy # 97.1022836308


# 将预测的结果匹配到图像中
new_show = np.zeros((output_image.shape[0],output_image.shape[1]))
k = 0
for i in range(output_image.shape[0]):
    for j in range(output_image.shape[1]):
        if output_image[i][j] != 0 :
            new_show[i][j] = predict_label[k]
            k +=1

# print new_show.shape

# 展示地物
ground_truth = spectral.imshow(classes = output_image.astype(int),figsize =(9,9))
ground_predict = spectral.imshow(classes = new_show.astype(int), figsize =(9,9))
plt.show(ground_truth)
plt.show(ground_predict)

print 'Done'
### 回答1: matlab高光谱svm正确分类后图像是指在使用高光谱图像进行支持向量机(SVM分类后,得到的分类结果图像。 高光谱图像是一种具有很高光谱分辨率的图像,它能够捕捉到物体不同波段的光谱信息。而SVM是一种常用的机器学习方法,可以用于分类和回归分析。在matlab中,通过调用相应的函数和工具箱,我们可以使用SVM进行高光谱图像分类。 当我们使用SVM高光谱图像进行分类后,将得到一个新的图像,该图像将原始图像中的不同类别进行分类标识。通常情况下,分类结果图像使用不同的颜色或灰度级别来表示不同的类别。 例如,假设我们有一幅高光谱图像,其中包含了植被和非植被两个类别。我们可以使用SVM对这幅图像进行分类,然后根据不同的类别,将植被部分用绿色表示,非植被部分用红色表示,生成一个分类结果图像。 在这个分类结果图像中,我们可以清晰地看到原始图像中的植被区域和非植被区域被正确地分离开来。这个分类结果图像可以帮助我们进一步分析和理解原始高光谱图像中不同部分的特征和属性。 总之,matlab高光谱svm正确分类后的图像能够直观地展示出高光谱图像中不同类别的分布情况,帮助我们对图像数据进行进一步的分析和研究。 ### 回答2: MATLAB高光谱SVM(支持向量机)是一种用于高光谱图像分类机器学习算法。它的目标是将高光谱图像中的每个像素正确分类为特定的类别。 在正确分类后,高光谱SVM将生成一个分类完成的图像。这个图像显示了原始高光谱图像中每个像素所属的类别。 在分类过程中,高光谱SVM使用训练集的样本数据来构建一个数学模型,该模型可以根据输入的像素特征将其分类为不同的类别。在模型训练之后,高光谱SVM使用这个模型来分类整个图像。 分类后的图像将使用不同的颜色或灰度级别来表示不同的类别。每个像素会被归类为其中一个类别,并相应地赋予一个独特的颜色或灰度值。这样,在生成的分类图像中,我们可以清楚地看到原始高光谱图像中不同区域的类别。 高光谱SVM的正确分类后图像可以帮助我们理解高光谱图像中不同物体或区域的特征和类别分布。这对于目标检测、土地利用分析、农作物监测等应用非常有价值。 总而言之,高光谱SVM正确分类后的图像可以直观地展示整个高光谱图像分类结果,帮助我们分析和理解图像中不同类别的分布情况。 ### 回答3: MATLAB高光谱SVM是一种用于图像分类的算法。在进行高光谱图像分类之后,我们可以得到一张分类正确的图像。 高光谱图像是一种具有多个连续波段的图像,每个波段都包含了不同的光谱信息。SVM(支持向量机)是一种机器学习算法,它通过找到最佳的超平面来对不同类别的数据进行分类。 在MATLAB中,我们可以使用高光谱图像和对应的标签数据进行训练和测试。首先,我们需要将图像数据和标签数据分成训练集和测试集。然后,使用训练集数据进行模型训练,得到一个高光谱SVM模型。 接下来,我们可以使用测试集数据来对模型进行验证。将测试集数据输入到高光谱SVM模型中,模型会根据学习到的超平面对每个像素点进行分类。对于分类正确的像素点,可以在输出图像中保留原始像素的颜色;对于分类错误的像素点,可以在输出图像中将其标记为不同的颜色或者用其他方式进行标记。 最终,我们可以得到一张高光谱SVM正确分类的图像。在这张图像中,不同类别的像素点被正确分类并用不同的颜色表示。这张图像可以被用于进一步的分析和应用,比如地质勘探、环境监测等领域。 总的来说,MATLAB高光谱SVM正确分类后的图像是一种通过机器学习算法对高光谱图像进行分类的结果,它能够快速、准确地将不同类别的像素点分开并用不同的颜色表示。这种图像可以在多个领域中发挥作用,并为进一步的分析和应用提供支持。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值