Adaptive window algorithm design

 Since late October, early November to write a little about computing software SEVI, optimize, convenient, real-time results of the idea, has been swirling in my mind.
& emsp; I was so conceived, sub optimization below the psychological process stages are set forth:

The first stage, Preliminary (fourth week of October)

1 idea

 How f-window option? At first I was tentative with a maximum gradient grid lattice point as the center point to 20 grid 20 grid for the initial window, which is 600m 600m as the initial window, which should be accessible to both the yin and yang slope, if the initial window is too small, it may it is only just involve shady or sunny. Outward step is 5 grid, the grid 60 is the maximum radius of extension. Start outwardly into upper and lower, left and right while extendingHere Insert Picture Description

2 implementation

Specific codes are formed as follows:

# 注: 已完成了阴阳坡剖分图
[max_hang,max_lie] = np.where(slope == np.max(slope))
max_hang = xi
max_lie = yi
width_pre = 10
height_pre = 10
for add in range(0,61,5):
	width = width_pre+add
	height = height_pre+add
	sum_area = 2*width*2*height
	# -------窗口参数如下--------
       win_up = max_hang-height  
       win_bottom = max_hang+height
       win_left = max_lie-width
       win_right = max_lie+width
       windows = aspect_10[int(win_up):int(win_bottom),int(win_left ):int(win_right)]   
       sum_windows1 = np.sum(windows == 1)
       ind1 = sum_windows1/windows  # 当ind1等于50%时,确认为当前最优窗口

 Why set up indexes -ind1? Before have been made to verify, pure positive slope, pure shady SLOPES mixed, to calculate the third best. This is because, according to the calculation algorithm SEVI "correlation coefficient", which the Pearson correlation coefficient related to the accumulation and seeking, and multiplicative. And co-linear correlation SEVI demand of RVI, SVI is a sunny "quiet" amount, a shady is "quiet" amount. So, when yin and yang slope area is large enough, and quiet strength is equal, the value of the calculation result f makes the most sense.
By reacting 1-ind1 index equal to 50%, which is equal to the number of pixels SLOPES, when equal area, as is best window, is calculated.

3 disadvantages

 (1) is not related to the problem of irregular boundaries of non-rectangular, non-rectangular if the remote sensing image, the window will most likely be extended to the background value.
Here Insert Picture Description
 (2) expansion mode window is very monotonous, may involve expansion of less than optimal window of the problem. Suppose step size s, the maximum length M, the step size starts from 0 extension, this expansion will be a total operation mode (M / s) times.

The second phase, to improve the (second week and three weeks of November three days)

1 Problems and Solutions

 Knowledge is often a gradual process, when I started the second week of November, when the feeling of getting better, there are many practical problems, in fact, have given me a slap in the face.
 According to the first phase of the problems, and the recommendations given by the teacher. The second indicator came up to build -ind2, but also to determine the availability of this indicator window, it is related to the question whether the background value. Specific codes are as follows:

# 注: 表观反射率不会存在负值,以及很小可能出现0值。满足这些值就是背景值。
# 用Arcgis剪裁背景值可能会出现-38e+38这个数,是小于-5的
rs_windows = rs[int(win_up):int(win_bottom),int(win_left ):int(win_right)]   
index1 = np.where(rs_windows < 5)[0]   # 小坑,np.where返回的是一个tuple,等于2
index2 = np.where(rs_windows == 0)[0]
ind2 = index1 + index2

2 expansion mode

 A width of about expansion, expansion of the upper and lower height 12 times this cycle. Suppose step size s, the maximum length M, the step size starts from 0 extension, this expansion will be a total operation mode (M / s) ^ 2 times. With a maximum gradient point as the center point, substituting s = 5, M = 60, a total of 244 times expansion. Assumptions in steps of 1, then the center point of a total of 3600 times can be extended.
 Than the previous stage, up and down, left and right with the expansion mode, more often for a whole doubled.
Here Insert Picture Description

3 code implementation

Combined with the window extended mode of question:

# 上下同扩、左右同扩的方案
[max_hang,max_lie] = np.where(slope == np.max(slope))
max_hang = xi
max_lie = yi
width_pre = 10
height_pre = 10
for add1 in range(0,M+1,s):  # 左右扩一次
	width = width_pre+add
	for add2 in range(0,M+1,s):   # 上下扩一次
		height = height_pre+add
		sum_area = 2*width*2*height
	       win_up = max_hang-height  
	       win_bottom = max_hang+height
	       win_left = max_lie-width
	       win_right = max_lie+width
       		# ----------指标1、2---------
	       windows = aspect_10[int(win_up):int(win_bottom),int(win_left ):int(win_right)]   
	       sum_windows1 = np.sum(windows == 1)
	       ind1 = sum_windows1/windows  # 当ind1等于50%时,确认为当前最优窗口
	       ind1 = int(ind1*100)  # float没法判断,转换为int
	       rs_windows = rs[int(win_up):int(win_bottom),int(win_left ):int(win_right)]   
		index1 = np.where(rs_windows < 5)[0]   # 小坑,np.where返回的是一个tuple,等于2
		index2 = np.where(rs_windows == 0)[0]
		ind2 = index1 + index2
		if ind1 == 50:
			if ind2 == 0 :
				#--------  f值算法----------
				pass

The third stage, tossing and turning (the third week of November, Thursday)

1 sunny cloudy

  After bedding the first two phases, he did not say, something more and more clear, but found more and more problems, summed up in one sentence, how to choose the best automated window?
(1) f the value of algorithmic problems, what is the value of f satisfy the conditions of it? F to select the optimal value of f values to meet the situation?
(2) Window incomplete traversal issue
 just the first question, there is enough for us to take into account. Our easy to difficult, first discuss the second question, in fact, the second question, is not particularly difficult, but specific optimization.

2 window scaling problems

 How to expand the window for the problem. We adopt helical extension method, as shown on the right side, provided the maximum extension length M, the maximum expansion inside this square 2M, there is ((M-10) ^ 4) -1 rectangular, may be made of our the center point for expansion.

 Analyzing the above two indicators -ind1, ind2 for each rectangle. In fact all of the windows can be obtained to satisfy the condition f-value is calculated.

# 注: 螺线式扩展法-逆时针
for add1 in range(0, M+1, s):  # 上扩一次
    win_up = now_hang-height_pre-add1  # now_hang为待扩展中心点的行坐标
    for add2 in range(0,  M + 1,  s):  # 左扩一次
        win_left = now_lie - width_pre - add2  # now_lie为待扩展中心点的列坐标
    for add3 in range(0,  M + 1,  s):  # 下扩一次
        win_bottom = now_hang + height_pre + add3
        for add4 in range(0,  M + 1,  s):  # 下扩一次
            win_right = now_lie + width_pre + add4
            # ----------指标1、2---------
            windows = aspect_10[int(win_up):int(win_bottom), int(win_left):int(win_right)]
            sum_windows1 = np.sum(windows == 1)
            sum_area = (win_bottom-win_up)*(win_right-win_left)
            ind1 = int((sum_windows1 / sum_area) * 100)  # 当ind1等于50%时,确认为当前最优窗口
            rs_wds = band4[int(win_up):int(win_bottom), int(win_left):int(win_right)]
            index1 = len(np.where(rs_wds < 5)[0])   # 小坑,np.where返回的是一个tuple,等于2
            index2 = len(np.where(rs_wds == 0)[0])
            ind2 = index1 + index2
            if ind1 == 50:
                if ind2 == 0:
                    # --------  f值算法----------
               	 pass

Here Insert Picture Description

 Helical extension, co-extended (M / s) ^ 4 times. In the image Wuyishan LC8 example, M may be set to 1/2 the image side of the maximum, i.e. 700, s can be set to 1. That each central point, need to test the extended 240.1 billion times. Then each grid point as a central point (1400 * 1200) were extended to test it? Obviously, this calculation can be obtained to meet the circumstances of each window can be obtained the optimal value of f within each window. It should be used so many windows, the optimal f value at which window it?

3 test optimal f value Optimal

 Conventional computing f value of a window, so that f is from 0 to 0.01 in steps of, traversing to 1. At this time, when the value of f and make SEVI_win RVI_win, SVI_win close linear relationship, i.e., with the Pearson correlation coefficient SEVI_now RVI is r1, SEVI_win SVI_win with Pearson correlation coefficient r2, e r1 r2-≈0 = time, but the code is not understood how is approximately equal to 0, it finds the smallest residuals E, f is the optimal value at that time. code show as below:

# 常规f值算法
     for f in range(0, 1001, 1):
            if f % 500 == 0:  # 进度条 
                print('当前运行了:{}%'.format(f/10))
            f = f / 1000
            SEVI_win = RVI_win + f * SVI_win
            n = len[SEVI]*len[SEVI[0]]
            r1 = Cal_PersonCC(SEVI, RVI, n)
            r2 = Cal_PersonCC(SEVI, SVI, n)
            e = abs(r1) - abs(r2)
            list_e.append(abs(e))
            list_f.append(f)
        e_min = min(list_e)
        f_opt = list_f[list_e.index(e_min)]  # e最小情况下的,当前窗口最优f值

* Think *

 1,2 index is used to select the appropriate window, and now we need new indicators: (1) determine the optimal f value is consistent with the design conditions (2) and the optimal value of f with which the window. The following flowchart is to explore the availability of optimal value f current window:
Here Insert Picture Description
  Note: SC_win correlation coefficient with COSI_win SEVI_win under current window, used to determine the optimal value of f.
Therefore, the new design of the two indicators are as follows:
IND3 = 0.001: observe e, how to say it is close to 0? Pearson correlation coefficients mainly by the extent of two discrete variables, to determine whether significant or insignificant linear relationship. So, in fact, if e <= 0.001 time, it can be identified, a linear correlation between them close. Moreover, there is no comparison really make f cycle from 0 to 1, because of changes in the value of f, in fact, does not actually affect the SC much floating.
ind4 = 0.05: When SC_win less than 0.05, f prove that the optimal values of the current window available, calculated SEVI better.

4 four indicators of principle, optimal f selected the best

 In general, is ind1, ind2 index control window, IND3 index control value f, f IND4 control value and availability goodness
Here Insert Picture Description

Specific implementation code:

import numpy as np
import datetime
import time
from ReadTif import *
from CalSEVI_maxSlope_gai_cal_f import *
s = datetime.datetime.now()
# --------------data-----------------------
slope, im_proj, im_geotrans, im_height, im_width = GRID().Tif_ReadBands(
    'F://2014018-wys-4-wys-yq4tif.tif', 1)
aspect = GRID().Tif_ReadBands('F:/2014018-wys-4-wys-yq4tif.tif', 2)[0]
band4 = GRID().Tif_ReadBands('F://2014018-wys-4-wys-yq4tif.tif', 3)[0]
band5 = GRID().Tif_ReadBands('F://2014018-wys-4-wys-yq4tif.tif', 4)[0]
sun_azimuth = 145.80911759  
sun_zenith = 40.65020465
aspect_10 = cal_sss(sun_azimuth, aspect)
COSI = cal_cosi(sun_zenith, sun_azimuth, slope, aspect)
SVI_over = 1 / band4  # 计算SVI
RVI_over = band5 / band4  # 计算RVI
s = runTime(s, '准备工作')
# --------------windows-----------------------
M = 200
s = 5
print('共需扩展{}次'.format((M/s)**4))
now_hang = 50
now_lie = 50
height_pre = 10
width_pre = 10
width_pre = 10  # 假定初始窗口大小为20*20,然后逐步扩大
count = 0 # 记录窗口的扩展次数
for add1 in range(0, M+1, s):  # 上扩一次
    win_up = now_hang-height_pre-add1  # now_hang为待扩展中心点的行坐标
    for add2 in range(0,  M + 1,  s):  # 左扩一次
        win_left = now_lie - width_pre - add2  # now_lie为待扩展中心点的列坐标
        for add3 in range(0,  M + 1,  s):  # 下扩一次
            win_bottom = now_hang + height_pre + add3
            for add4 in range(0,  M + 1,  s):  # 下扩一次
                win_right = now_lie + width_pre + add4
                count = count + 1
                # ----------指标1、2---------
                try:
                    windows = aspect_10[int(win_up):int(win_bottom), int(win_left):int(win_right)]
                    sum_windows1 = np.sum(windows == 1)
                    sum_area = (win_bottom-win_up)*(win_right-win_left)
                    ind1 = int((sum_windows1 / sum_area) * 100)  # 当ind1等于50%时,确认为当前最优窗口
                    rs_wds = band4[int(win_up):int(win_bottom), int(win_left):int(win_right)]
                    index1 = len(np.where(rs_wds < -5)[0])   # 小坑,np.where返回的是一个tuple,等于2
                    index2 = len(np.where(rs_wds == 0)[0])
                    ind2 = index1 + index2
                    if ind1 == 50:
                        if ind2 == 0:
                            # --------  f值算法----------
                            SVI_wds = SVI_over[int(win_up):int(win_bottom), int(win_left):int(win_right)]
                            RVI_wds = RVI_over[int(win_up):int(win_bottom), int(win_left):int(win_right)]
                            cosi = COSI[int(win_up):int(win_bottom), int(win_left):int(win_right)]
                            n = len(cosi) * len(cosi[0])
                            ind3 = 0.001  # 用来判定r1与r2的小的程度
                            ind4 = 0.05  # 用来判定与cosi的近似小
                            flag1 = 0  # 跳出f
                            for f in range(0, 1001, 1):
                                if flag1 == 1:
                                    print('out')
                                    break
                                f = f / 1000
                                SEVI_wds = RVI_wds + f * SVI_wds  # 求取当前窗口SEVI
                                r1 = Cal_PersonCC(SEVI_wds, RVI_wds, len(SEVI_wds)*len(SEVI_wds[0]))  # 相关系数法
                                r2 = Cal_PersonCC(SEVI_wds, SVI_wds, len(SEVI_wds)*len(SEVI_wds[0]))
                                e = abs(abs(r1) - abs(r2))  # e要满足接近于0
                                if e <= ind3:  # 满足指标3,就是相关系数法求得的f值,若一个也没有,证明当前窗口不可用
                                    SEVI_over = RVI_over + f * SVI_over  # 求取全局SEVI
                                    SC_wds = abs(Cal_PersonCC(SEVI_wds, cosi, len(SEVI_wds)*len(SEVI_wds[0])))
                                    SC_over = abs(Cal_PersonCC(SEVI_over, COSI, len(SEVI_over)*len(SEVI_over[0])))
                                    if SC_wds < ind4:  # 满足指标4,就是优质f值
                                        print('\n\n\n**发现满足条件的:f、SC_wds、SC_over、e:\n')
                                        print(f, '\t', SC_wds, '\t', SC_over, '\t', e)
                                        print(win_up, win_bottom,win_left, win_right)
                                        print('窗口已扩展了,', count, '次\n\n\n')
                                        flag1 = 1  # 当前f值,满足了ind3,也满足了ind4,表示可用,确定为窗口内最佳
                                        break
                                    else:
                                        print('\t***满足ind1、ind2、ind3,但是不满足ind4','窗口已扩展了,', count, '次')
                                        flag1 = 1  # f值的变动其实对这个窗口内的,SEVI与COSI的关系影响不大了,不满足就立马换窗口
                                        break
                except Exception as e:
                    print(e)
                    
s = runTime(s, '一个点扩展总用时')

Guess you like

Origin blog.csdn.net/qq_40260867/article/details/84374342