2022-05-13 17:52:00 +08:00
|
|
|
|
import datetime, math, os, sys, logging
|
|
|
|
|
import cv2
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f1(image_path):
|
|
|
|
|
# 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)
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f2(image_path):
|
|
|
|
|
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)
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def f3(image_path):
|
|
|
|
|
img = cv2.imread(image_path)
|
|
|
|
|
b, g, r = cv2.split(img)
|
|
|
|
|
|
2022-05-17 17:43:27 +08:00
|
|
|
|
# 自己计算饱和度-----> 失败:计算的饱和度不对,是负值
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
μ_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)) # 薄云
|
|
|
|
|
|
|
|
|
|
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-17 17:43:27 +08:00
|
|
|
|
def jisuan(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)
|
|
|
|
|
|
|
|
|
|
print("均值为:%f\n" % mean)
|
|
|
|
|
print("方差为:%f\n" % var)
|
|
|
|
|
|
|
|
|
|
|
2022-05-13 17:52:00 +08:00
|
|
|
|
if __name__ == '__main__':
|
2022-05-17 17:43:27 +08:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
# 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)
|