通过2种比值法的差值识别云

This commit is contained in:
2022-05-19 08:59:09 +08:00
parent daeafd9659
commit 47117f5643

View File

@ -3,7 +3,8 @@ import cv2
import numpy as np import numpy as np
def f1(image_path): # r / b
def ratio_method1(image_path):
# Load an image # Load an image
img = cv2.imread(image_path) img = cv2.imread(image_path)
# img = cv2.imread(image_path, 0) # Load an image in grayscale # 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.imwrite(out, cloud_BINARY)
# cv2.imshow('image', cloud_BINARY) # cv2.imshow('image', cloud_BINARY)
cv2.waitKey(0) # cv2.waitKey(0)
cv2.destroyAllWindows() # cv2.destroyAllWindows()
return cloud_BINARY
def f2(image_path): # (b-r) / (b+r)
def ratio_method2(image_path):
img = cv2.imread(image_path) img = cv2.imread(image_path)
b, g, r = cv2.split(img) b, g, r = cv2.split(img)
@ -41,11 +45,34 @@ def f2(image_path):
cv2.imwrite(out, cloud_BINARY) cv2.imwrite(out, cloud_BINARY)
# cv2.imshow('image', cloud_BINARY) # cv2.imshow('image', cloud_BINARY)
cv2.waitKey(0) # cv2.waitKey(0)
cv2.destroyAllWindows() # 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) img = cv2.imread(image_path)
b, g, r = cv2.split(img) b, g, r = cv2.split(img)
@ -70,15 +97,28 @@ def f3(image_path):
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间 hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间
h, l, s = cv2.split(hls) 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 # # np.where用法https://www.zhihu.com/question/62844162
σ_sky = 4.4 # 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 # np.where用法https://www.zhihu.com/question/62844162
clouds_loc = np.where(s < μ_Clouds + 3 * σ_Clouds) # 厚云 clouds_loc = np.where(s > μ_Clouds - 3 * σ_Clouds) # 厚云
sky_loc = np.where(s > μ_sky - 3 * σ_sky) # 天空 sky_loc = np.where(s < μ_sky + 3 * σ_sky) # 天空
unknow_loc = np.where((μ_Clouds + 3 * σ_Clouds <= s) & (s <= μ_sky - 3 * σ_sky)) # 薄云 unknow_loc = np.where((μ_Clouds + 3 * σ_Clouds <= s) & (s <= μ_sky - 3 * σ_sky)) # 薄云
s[sky_loc] = 1 s[sky_loc] = 1
@ -94,7 +134,7 @@ def f3(image_path):
cv2.destroyAllWindows() cv2.destroyAllWindows()
def jisuan(image_path): def calculate_mean_var(image_path):
img = cv2.imread(image_path) img = cv2.imread(image_path)
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间 hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间
@ -103,6 +143,8 @@ def jisuan(image_path):
mean = np.mean(s) mean = np.mean(s)
var = np.var(s) var = np.var(s)
print(image_path)
print("\n")
print("均值为:%f\n" % mean) print("均值为:%f\n" % mean)
print("方差为:%f\n" % var) print("方差为:%f\n" % var)
@ -111,12 +153,21 @@ if __name__ == '__main__':
train_image_path1 = r'D:\PycharmProjects\cloudage\train\sky.jpg' train_image_path1 = r'D:\PycharmProjects\cloudage\train\sky.jpg'
train_image_path2 = r'D:\PycharmProjects\cloudage\train\cloude.jpg' train_image_path2 = r'D:\PycharmProjects\cloudage\train\cloude.jpg'
train_image_path3 = r'D:\PycharmProjects\cloudage\train\light cloud.jpg' train_image_path3 = r'D:\PycharmProjects\cloudage\train\light cloud.jpg'
jisuan(train_image_path1) train_image_path4 = r'D:\PycharmProjects\cloudage\train\s61.jpg'
jisuan(train_image_path2) train_image_path5 = r'D:\PycharmProjects\cloudage\train\s96.jpg'
jisuan(train_image_path3) # 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' # 证明python-opencv的饱和度值域范围未0-255
# # image_path = r'D:\PycharmProjects\cloudage\photo\cloud1_perfect.jpg' # f3全为天空 calculate_mean_var(train_image_path4)
# # f1(image_path) calculate_mean_var(train_image_path5)
# # f2(image_path)
# f3(image_path)
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)