Files
cloudage/cloudage.py

90 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
# # 转到HSV空间
# hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# h, s, v = cv2.split(hsv)
# 转到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()
if __name__ == '__main__':
image_path = r'D:\PycharmProjects\cloudage\photo\photo7.jpg'
# image_path = r'D:\PycharmProjects\cloudage\photo\cloud1_perfect.jpg' # f3全为天空
f1(image_path)
f2(image_path)
f3(image_path)