iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 28
0
Software Development

開源的GIS實作系列 第 28

[day-28] 串接2

  • 分享至 

  • xImage
  •  

前言

接下來將[day-20] K-means 分類法分類影像 分類法串接上去。

K-means分類法程式

我們將k-means程式改寫為

## KMEANFunc.py

import skimage
from sklearn import cluster
import numpy

def image_kmeans_classification(image_array):
    image_array = numpy.asarray(image_array)
    #print(image_array)
    row_number = image_array.shape[0]
    col_number = image_array.shape[1]
    sample = image_array.reshape((row_number*col_number,3))
    #print(sample)
    kmeans_classifer = cluster.KMeans(n_clusters = 4,random_state=0).fit(sample)
    kmeans_result = kmeans_classifer.predict(sample)
    kmeans_result = (kmeans_result-numpy.min(kmeans_result))/(numpy.max(kmeans_result)-numpy.min(kmeans_result))*255
    image_array_classification = kmeans_result.reshape((row_number,col_number))
    return image_array_classification

主程式

接下來我們將我們k-means串接上去

from ODMFunc import Run_Nodeodm, Catch_Filename
from IMAGEFunc import std_convoluted, add_exif
from KMEANFunc import image_kmeans_classification

def main():
    sys.path.append('..')
    upload_file = sys.argv[1] # 將外部參數sys.avgv[1]作為upload檔案輸入。
    filename_list = Catch_Filename(upload_file) # 遍歷upload_file的所有Jpg並且回傳一個list
    Run_Nodeodm(filename_list) # 執行影像建模
    
    img_path = "./results/odm_orthophoto/odm_orthoimage.tif"
    img_output_path = "./results/odm_orthoimage_variance.tif"
    kmeans_image_path = "./results/odm_orthoimage_kmeans.tif"
    
    img = skimage.io.imread(img_path)
    img_g = img[:,:,1]
    N = 3
    img_var = std_convoluted(img_g, N)
    img_var = numpy.asarray(img_var)
    skimage.io.imsave(img_output_path,img_var)
    add_exif(img_output_path,img_path)
    # kmeans classification
    image_array = skimage.io.imread(img_output_path)
    image_array_classification = image_kmeans_classification(image_array)
    skimage.io.imsave(img_output_path,kmeans_image_path)

結語

接下來就剩下將計算數量的串聯上去了。


上一篇
[day-27]串接1
下一篇
[day-29] 串接3
系列文
開源的GIS實作30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言