Files
cloudage/cloudage.py

173 lines
4.9 KiB
Python
Raw Normal View History

2022-05-13 17:52:00 +08:00
import datetime, math, os, sys, logging
import cv2
import numpy as np
2022-05-19 08:59:09 +08:00
# r / b
def ratio_method1(image_path):
2022-05-13 17:52:00 +08:00
# Load an image
img = cv2.imread(image_path)
# img = cv2.imread(image_path, 0) # Load an image in grayscale
b, g, r = cv2.split(img)
cloud = r / b
ret, cloud_BINARY = cv2.threshold(cloud, 0.6, 255, cv2.THRESH_BINARY) # 文献中推荐阈值为0.6
cloudage = cloud_BINARY.sum() / (cloud_BINARY.size * 255)
print("r/b算法云量为%f\n" % cloudage)
tmp = image_path.split('.')
out = tmp[0] + "_r_b." + tmp[1]
cv2.imwrite(out, cloud_BINARY)
# cv2.imshow('image', cloud_BINARY)
2022-05-19 08:59:09 +08:00
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return cloud_BINARY
2022-05-13 17:52:00 +08:00
2022-05-19 08:59:09 +08:00
# (b-r) / (b+r)
def ratio_method2(image_path):
2022-05-13 17:52:00 +08:00
img = cv2.imread(image_path)
b, g, r = cv2.split(img)
cloud = (b-r) / (b+r)
ret, cloud_BINARY = cv2.threshold(cloud, 0.2, 255, cv2.THRESH_BINARY_INV)
cloudage = cloud_BINARY.sum() / (cloud_BINARY.size * 255)
print("(b-r) / (b+r)算法,云量为%f\n" % cloudage)
tmp = image_path.split('.')
out = tmp[0] + "_(b-r)_(b+r)." + tmp[1]
cv2.imwrite(out, cloud_BINARY)
# cv2.imshow('image', cloud_BINARY)
2022-05-19 08:59:09 +08:00
# cv2.waitKey(0)
# cv2.destroyAllWindows()
2022-05-13 17:52:00 +08:00
2022-05-19 08:59:09 +08:00
return cloud_BINARY
2022-05-13 17:52:00 +08:00
2022-05-19 08:59:09 +08:00
# 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):
2022-05-13 17:52:00 +08:00
img = cv2.imread(image_path)
b, g, r = cv2.split(img)
# 自己计算饱和度-----> 失败:计算的饱和度不对,是负值
ones = np.ones_like(b)
sum = b + g + r
min_b_g_r = np.zeros_like(b)
for x in range(b.shape[0]):
for y in range(b.shape[1]):
minValue = min(b[x, y], g[x, y], r[x, y])
min_b_g_r[x, y] = minValue
tmp = (3/sum)*min_b_g_r
s_tc = ones - tmp
# 转到HSV空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h1, s1, v1 = cv2.split(hsv)
2022-05-13 17:52:00 +08:00
# 转到HLS空间
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间
h, l, s = cv2.split(hls)
2022-05-19 08:59:09 +08:00
# # 文献中的阈值
# μ_Clouds = 12.7
# σ_Clouds = 3.7
#
# μ_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
2022-05-13 17:52:00 +08:00
2022-05-19 08:59:09 +08:00
μ_sky = 111.25
σ_sky = 3.13
2022-05-13 17:52:00 +08:00
# np.where用法https://www.zhihu.com/question/62844162
2022-05-19 08:59:09 +08:00
clouds_loc = np.where(s > μ_Clouds - 3 * σ_Clouds) # 厚云
sky_loc = np.where(s < μ_sky + 3 * σ_sky) # 天空
2022-05-13 17:52:00 +08:00
unknow_loc = np.where((μ_Clouds + 3 * σ_Clouds <= s) & (s <= μ_sky - 3 * σ_sky)) # 薄云
s[sky_loc] = 1
s[clouds_loc] = 2
s[unknow_loc] = 3
tmp = image_path.split('.')
out = tmp[0] + "_saturation." + tmp[1]
cv2.imwrite(out, s)
# cv2.imshow('image', cloud_BINARY)
cv2.waitKey(0)
cv2.destroyAllWindows()
2022-05-19 08:59:09 +08:00
def calculate_mean_var(image_path):
img = cv2.imread(image_path)
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) # 这就是ihs颜色空间
h, l, s = cv2.split(hls)
mean = np.mean(s)
var = np.var(s)
2022-05-19 08:59:09 +08:00
print(image_path)
print("\n")
print("均值为:%f\n" % mean)
print("方差为:%f\n" % var)
2022-05-13 17:52:00 +08:00
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'
2022-05-19 08:59:09 +08:00
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)
# 证明python-opencv的饱和度值域范围未0-255
2023-03-12 17:29:26 +08:00
# calculate_mean_var(train_image_path4)
# calculate_mean_var(train_image_path5)
2022-05-19 08:59:09 +08:00
image_path = r'D:\PycharmProjects\cloudage\photo\photo12.jpg'
# image_path = r'D:\PycharmProjects\cloudage\photo\cloud1_perfect.jpg'
# ratio_method1(image_path)
2023-03-12 17:29:26 +08:00
ratio_method2(image_path)
2022-05-19 08:59:09 +08:00
# ratio_method1_subtract_ratio_method2(image_path)
# saturability_method(image_path)