接下來我們有了k-means的分成果,接下來當然是開始計算數量啦!!!計算數量請參考[day-23] opencv - 計算封閉圖形數量(2)。
# CountFunc.py
import cv2、
def Count_Plant(count_path):
img = cv2.imread(count_path)
dst= img[:,:,0]
contours, hierarchy = cv2.findContours(dst,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #輪廓檢測函數
count=0
for cont in contours:
count+=1 #總體計數加1
return count
from ODMFunc import Run_Nodeodm, Catch_Filename
from IMAGEFunc import std_convoluted, add_exif
from KMEANFunc import image_kmeans_classification
from CountFunc import Count_Plant
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)
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)
count = Count_Plant(kmeans_image_path) # 計算秧苗數量
print(count)
終於串接完成,明天做個30的總結。