From 47117f5643548d8148e15db9fbccb10d1cdfd916 Mon Sep 17 00:00:00 2001 From: tangchao Date: Thu, 19 May 2022 08:59:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=872=E7=A7=8D=E6=AF=94=E5=80=BC?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E5=B7=AE=E5=80=BC=E8=AF=86=E5=88=AB=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloudage.py | 95 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 22 deletions(-) diff --git a/cloudage.py b/cloudage.py index 4147734..17dde78 100644 --- a/cloudage.py +++ b/cloudage.py @@ -3,7 +3,8 @@ import cv2 import numpy as np -def f1(image_path): +# r / b +def ratio_method1(image_path): # Load an image img = cv2.imread(image_path) # img = cv2.imread(image_path, 0) # Load an image in grayscale @@ -21,11 +22,14 @@ def f1(image_path): cv2.imwrite(out, cloud_BINARY) # cv2.imshow('image', cloud_BINARY) - cv2.waitKey(0) - cv2.destroyAllWindows() + # cv2.waitKey(0) + # cv2.destroyAllWindows() + + return cloud_BINARY -def f2(image_path): +# (b-r) / (b+r) +def ratio_method2(image_path): img = cv2.imread(image_path) b, g, r = cv2.split(img) @@ -41,11 +45,34 @@ def f2(image_path): cv2.imwrite(out, cloud_BINARY) # cv2.imshow('image', cloud_BINARY) - cv2.waitKey(0) - cv2.destroyAllWindows() + # cv2.waitKey(0) + # cv2.destroyAllWindows() + + return cloud_BINARY -def f3(image_path): +# r / b - (b-r) / (b+r) +def ratio_method1_subtract_ratio_method2(image_path): + result1 = ratio_method1(image_path) + result2 = ratio_method2(image_path) + + light_cloud_BINARY = np.zeros_like(result1) + + for x in range(result1.shape[0]): + for y in range(result1.shape[1]): + if (result1[x, y] != 0 and result2[x, y] == 0): + light_cloud_BINARY[x, y] = 255 + + tmp = image_path.split('.') + out = tmp[0] + "_subtract." + tmp[1] + cv2.imwrite(out, light_cloud_BINARY) + + # cv2.imshow('image', light_cloud_BINARY) + # cv2.waitKey(0) + # cv2.destroyAllWindows() + + +def saturability_method(image_path): img = cv2.imread(image_path) b, g, r = cv2.split(img) @@ -70,15 +97,28 @@ def f3(image_path): hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间: h, l, s = cv2.split(hls) - μ_Clouds = 12.7 - σ_Clouds = 3.7 + # # 文献中的阈值 + # μ_Clouds = 12.7 + # σ_Clouds = 3.7 + # + # μ_sky = 45.3 + # σ_sky = 4.4 - μ_sky = 45.3 - σ_sky = 4.4 + # # np.where用法:https://www.zhihu.com/question/62844162 + # clouds_loc = np.where(s < μ_Clouds + 3 * σ_Clouds) # 厚云 + # sky_loc = np.where(s > μ_sky - 3 * σ_sky) # 天空 + # unknow_loc = np.where((μ_Clouds + 3 * σ_Clouds <= s) & (s <= μ_sky - 3 * σ_sky)) # 薄云 + + # 自己通过样本测得的阈值 + μ_Clouds = 170.9 + σ_Clouds = 2.7 + + μ_sky = 111.25 + σ_sky = 3.13 # np.where用法:https://www.zhihu.com/question/62844162 - clouds_loc = np.where(s < μ_Clouds + 3 * σ_Clouds) # 厚云 - sky_loc = np.where(s > μ_sky - 3 * σ_sky) # 天空 + clouds_loc = np.where(s > μ_Clouds - 3 * σ_Clouds) # 厚云 + sky_loc = np.where(s < μ_sky + 3 * σ_sky) # 天空 unknow_loc = np.where((μ_Clouds + 3 * σ_Clouds <= s) & (s <= μ_sky - 3 * σ_sky)) # 薄云 s[sky_loc] = 1 @@ -94,7 +134,7 @@ def f3(image_path): cv2.destroyAllWindows() -def jisuan(image_path): +def calculate_mean_var(image_path): img = cv2.imread(image_path) hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间: @@ -103,6 +143,8 @@ def jisuan(image_path): mean = np.mean(s) var = np.var(s) + print(image_path) + print("\n") print("均值为:%f\n" % mean) print("方差为:%f\n" % var) @@ -111,12 +153,21 @@ if __name__ == '__main__': train_image_path1 = r'D:\PycharmProjects\cloudage\train\sky.jpg' train_image_path2 = r'D:\PycharmProjects\cloudage\train\cloude.jpg' train_image_path3 = r'D:\PycharmProjects\cloudage\train\light cloud.jpg' - jisuan(train_image_path1) - jisuan(train_image_path2) - jisuan(train_image_path3) + train_image_path4 = r'D:\PycharmProjects\cloudage\train\s61.jpg' + train_image_path5 = r'D:\PycharmProjects\cloudage\train\s96.jpg' + # calculate_mean_var(train_image_path1) + # calculate_mean_var(train_image_path2) + # calculate_mean_var(train_image_path3) - # image_path = r'D:\PycharmProjects\cloudage\photo\photo12.jpg' - # # image_path = r'D:\PycharmProjects\cloudage\photo\cloud1_perfect.jpg' # f3全为天空 - # # f1(image_path) - # # f2(image_path) - # f3(image_path) + # 证明python-opencv的饱和度值域范围未0-255 + calculate_mean_var(train_image_path4) + calculate_mean_var(train_image_path5) + + + image_path = r'D:\PycharmProjects\cloudage\photo\photo12.jpg' + # image_path = r'D:\PycharmProjects\cloudage\photo\cloud1_perfect.jpg' + # ratio_method1(image_path) + # ratio_method2(image_path) + # ratio_method1_subtract_ratio_method2(image_path) + + # saturability_method(image_path)