-
Notifications
You must be signed in to change notification settings - Fork 0
/
Newdetectcloud13.10
166 lines (128 loc) · 4.77 KB
/
Newdetectcloud13.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def calMeanGray(img):
return np.average(img) / 255
def calFEdgeness(img, threshold):
img_edge = cv2.Sobel(src=img, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
img_edge = np.abs(img_edge)
r, c = img_edge.shape[:2]
FEdgeness = np.count_nonzero(img_edge > threshold) / (r*c)
return FEdgeness
def calTextureHis(img):
normalizedMean = calMeanGray(img)
FEdgeness = calFEdgeness(img, 138)
textureHis = np.append(normalizedMean, FEdgeness)
return textureHis
def calL1Dist(his1, his2):
index0 = his1[0]-his2[0]
index1 = his1[1]-his2[1]
l1 = np.abs(index0) + np.abs(index1)
return l1
def textureOverlay(img, texture, threshold):
imgOut = np.copy(img)
hisTexture = calTextureHis(texture)
windowSize = 21
r, c = img.shape[:2]
detected_area = 0
for i in range(r-windowSize+1):
for j in range(c-windowSize+1):
subImg = img[i:i+windowSize, j:j+windowSize]
hisSubImg = calTextureHis(subImg)
if calL1Dist(hisTexture, hisSubImg) < threshold:
imgOut[i+windowSize//2][j+windowSize//2] = 0
detected_area += 1
else:
imgOut[i + windowSize // 2][j + windowSize // 2] = 255
return [imgOut, detected_area]
def quantize_img_color(img):
if len(img.shape) == 2:
data = img.reshape((-1, 1))
else:
data = img.reshape((-1, 3))
data = np.float32(data)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
k = 4
_, labels, centers = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
centers = np.uint8(centers)
quantized_data = centers[labels.flatten()]
quantized_image = quantized_data.reshape(img.shape)
return quantized_image
def get_circularity(img):
edge = cv2.Canny(img, threshold1=100, threshold2=200)
perimeter = cv2.countNonZero(edge)
area = cv2.countNonZero(img)
return round(perimeter**2/area)
def resize_img(img):
h, w = img.shape
return cv2.resize(img, (w//2, h//2))
def run(img, texture):
q_img = quantize_img_color(img)
q_img_t = quantize_img_color(texture)
img_overlay = textureOverlay(q_img, q_img_t, 0.2)
resized_img = resize_img(img_overlay[0])
area = img_overlay[1]
cir = get_circularity(q_img)
return [resized_img, area, cir]
def get_edgeDensity(img):
r, c = img.shape
smooth_img = cv2.GaussianBlur(img, ksize=(5,5), sigmaX=0, sigmaY=0)
edges = cv2.Canny(smooth_img, threshold1=100, threshold2=200)
total_pixels = r*c
edge_pixels = np.count_nonzero(edges)
edge_density = edge_pixels / total_pixels
return edge_density
def get_aspect_ratio(img):
# Get the shape of the image (height, width)
h, w = img.shape[:2]
# Calculate aspect ratio
aspect_ratio = w / h
return aspect_ratio
def resize_to_fixed_size(img, width, height):
# Resize the image to the specified width and height
resized_img = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
return resized_img
def detect_cloud_types(img):
area = cv2.countNonZero(img)
cir = get_circularity(img)
aspect_ratio = get_aspect_ratio(img)
edge_density = get_edgeDensity(img)
print("Area:", area)
print("Circularity:", cir)
print("Aspect Ratio:", aspect_ratio)
print("Edge", edge_density)
if area > 7000 and cir >= 3 and 1.6 < aspect_ratio < 3.5 and edge_density > 0.0003:
print('Type:cumulus (0)')
elif area > 40000 and cir == 0 and 1.1 < aspect_ratio < 2.0 and edge_density < 0.002:
print('Type:nimbostratus (1)')
elif 5000 < area < 20000 and 0 <= cir <= 1 and 0.8 < aspect_ratio < 1.84 and 0.0 <= edge_density < 0.02:
print('Type:stratocumulus (0)')
def load_file():
pass
def write_excel():
pass
fixed_width = 256
fixed_height = 256
cumulus = cv2.imread('minicumulus6.png', 0)
nimbostratus = cv2.imread('mininimbro4.png', 0)
stratocumulus = cv2.imread('miniStrato10.png', 0)
resized_cumulus = resize_to_fixed_size(cumulus, fixed_width, fixed_height)
resized_nimbostratus = resize_to_fixed_size(nimbostratus, fixed_width, fixed_height)
resized_stratocumulus = resize_to_fixed_size(stratocumulus, fixed_width, fixed_height)
cumulus_t = cv2.imread('cumulus_t.png', 0)
nimbostratus_t = cv2.imread('nimbo_t.png', 0)
stratocumulus_t = cv2.imread('strato_t.png', 0)
run1 = run(resized_cumulus, cumulus_t)
run2 = run(resized_nimbostratus, nimbostratus_t)
run3 = run(resized_stratocumulus, stratocumulus_t)
# cv2.imshow('overlay', run1)
# cv2.waitKey(0)
# cv2.imshow('overlay', run2)
# cv2.waitKey(0)
# cv2.imshow('overlay', run3)
# cv2.waitKey(0)
detect_cloud_types(cumulus)
detect_cloud_types(nimbostratus)
detect_cloud_types(stratocumulus)