程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Background pixel processing method in Python gdal+numpy remote sensing image processing

編輯:Python

In the process of processing remote sensing images , It often involves the processing of background pixels , Remote sensing images are usually stored in regular rectangular arrays , The effective image area is often irregular data , This involves the processing of background pixel values .

Most software processes the background value of remote sensing image as 0 value , There are also -9999、-19999、nodata、nan etc. .

Some calculations , The background value does not affect the calculation results , The calculation result here refers to the calculation result of the image area , Background values often change , But some changes are right and wrong 0 The value is calculated as 0 The value of , So when we open the image, the display doesn't change much . Some calculation background values will produce incorrect results for the calculation , I'll talk about it in detail later .

For example , Take the image background value as -19999 For example , such as landsat Data products , Some use this value as the background value , This value can be used as the background value to effectively distinguish the 0 value , With zero as the background value, it is easy to compare with the 0 Values mixed together .

We use numpy Generate an array , Manually change some values to -19999, As a background value . Not here gdal Read the image data as an array , If necessary, please refer to the previous column : Remote sensing image processing https://blog.csdn.net/soderayer/category_11011395.html.

Let's use numpy Randomly generate two arrays , Set it to 10 That's ok 10 Come on . Random range set at (-10000,10000).

img_array1=np.random.randint(-10000,10000,(10,10))
img_array2=np.random.randint(-10000,10000,(10,10))
print(img_array1)
print(img_array2)

Generate two arrays, namely :

[[ 8721 7329 -2775 -6596 5229 7148 1385 1711 700 -7822]
[ -816 -8182 30 -4158 5816 -5305 5669 5625 3552 -384]
[-3565 5693 4146 8107 -2774 -4736 3292 9236 241 -8017]
[ 2066 -9807 -241 -6861 7028 6369 6259 -5660 -1612 5498]
[ 6978 -1917 -39 -5350 5398 -9869 4032 3451 6517 -3606]
[-9431 -7302 8738 3456 -3984 -8229 -9269 2655 -3206 4163]
[-7831 2560 800 -6254 9050 3982 1317 8016 5144 1791]
[ 9465 773 8575 -6392 -9249 2405 9490 -341 -9153 7140]
[ 2088 4 5321 9371 -9785 -5229 3681 -6103 -6100 1233]
[-1987 3837 4125 3227 4257 -3267 7863 1640 -583 -7665]]
[[-7408 -1577 3365 5674 -7981 -9032 9326 6460 -4230 -859]
[ 9197 -7282 6126 -2568 6494 7313 -1999 -2293 7415 7533]
[ 7307 -6304 8747 -793 -9423 619 9281 -3042 4621 822]
[ 7328 5918 -5312 -2999 -8226 -6250 -5463 -9790 -200 -7922]
[ 7339 -974 -4859 -5033 -3066 -4912 -5041 8331 -8908 2473]
[-7276 4051 5302 -7259 -1645 5275 1527 1811 -9701 4537]
[-7924 719 3889 8950 15 -228 8703 -5319 -621 6546]
[-9236 5188 8391 9077 8269 -7798 817 -3914 8359 -5854]
[-9905 2167 1676 -3345 6357 2736 -3426 3897 8539 -552]
[ 162 766 8993 175 9677 1460 3250 -193 -5935 4897]]

Let's change the outer two circles of an array to -19999:

rows=[0,1,8,9]
cols=[0,1,8,9]
for i in rows:
for j in range(0,10):
img_array1[i][j]=-19999
img_array2[i][j] = -19999
for i in cols:
for j in range(0,10):
img_array1[j][i]=-19999
img_array2[j][i] = -19999
[[-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
[-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
[-19999 -19999 4146 8107 -2774 -4736 3292 9236 -19999 -19999]
[-19999 -19999 -241 -6861 7028 6369 6259 -5660 -19999 -19999]
[-19999 -19999 -39 -5350 5398 -9869 4032 3451 -19999 -19999]
[-19999 -19999 8738 3456 -3984 -8229 -9269 2655 -19999 -19999]
[-19999 -19999 800 -6254 9050 3982 1317 8016 -19999 -19999]
[-19999 -19999 8575 -6392 -9249 2405 9490 -341 -19999 -19999]
[-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
[-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]]
[[-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
 [-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
 [-19999 -19999   8747   -793  -9423    619   9281  -3042 -19999 -19999]
 [-19999 -19999  -5312  -2999  -8226  -6250  -5463  -9790 -19999 -19999]
 [-19999 -19999  -4859  -5033  -3066  -4912  -5041   8331 -19999 -19999]
 [-19999 -19999   5302  -7259  -1645   5275   1527   1811 -19999 -19999]
 [-19999 -19999   3889   8950     15   -228   8703  -5319 -19999 -19999]
 [-19999 -19999   8391   9077   8269  -7798    817  -3914 -19999 -19999]
 [-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]
 [-19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999 -19999]]

The essence of remote sensing image index operation is array addition, subtraction, multiplication, division and various logic operations . The following is the addition, subtraction, multiplication and division of the array , In fact, it is the addition, subtraction, multiplication and division of the corresponding position in the array .

### Array addition, subtraction, multiplication and division Addition, subtraction, multiplication and division of arrays and constants , Omit
plus_array=img_array1+img_array2
print("plus_array:")
print(plus_array)
minus_array=img_array1-img_array2
print("minus_array:")
print(minus_array)
multiply_array=img_array1*img_array2
print("multiply_array:")
print(multiply_array)
divide_array=img_array1/img_array2
print("divide_array:")
print(divide_array)

The result of the calculation is :

plus_array:
[[-39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998]
[-39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998]
[-39998 -39998 12893 7314 -12197 -4117 12573 6194 -39998 -39998]
[-39998 -39998 -5553 -9860 -1198 119 796 -15450 -39998 -39998]
[-39998 -39998 -4898 -10383 2332 -14781 -1009 11782 -39998 -39998]
[-39998 -39998 14040 -3803 -5629 -2954 -7742 4466 -39998 -39998]
[-39998 -39998 4689 2696 9065 3754 10020 2697 -39998 -39998]
[-39998 -39998 16966 2685 -980 -5393 10307 -4255 -39998 -39998]
[-39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998]
[-39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998 -39998]]
minus_array:
[[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 -4601 8900 6649 -5355 -5989 12278 0 0]
[ 0 0 5071 -3862 15254 12619 11722 4130 0 0]
[ 0 0 4820 -317 8464 -4957 9073 -4880 0 0]
[ 0 0 3436 10715 -2339 -13504 -10796 844 0 0]
[ 0 0 -3089 -15204 9035 4210 -7386 13335 0 0]
[ 0 0 184 -15469 -17518 10203 8673 3573 0 0]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0]]
multiply_array:
[[399960001 399960001 399960001 399960001 399960001 399960001 399960001
399960001 399960001 399960001]
[399960001 399960001 399960001 399960001 399960001 399960001 399960001
399960001 399960001 399960001]
[399960001 399960001 36265062 -6428851 26139402 -2931584 30553052
-28095912 399960001 399960001]
[399960001 399960001 1280192 20576139 -57812328 -39806250 -34192917
55411400 399960001 399960001]
[399960001 399960001 189501 26926550 -16550268 48476528 -20325312
28750281 399960001 399960001]
[399960001 399960001 46328876 -25087104 6553680 -43407975 -14153763
4808205 399960001 399960001]
[399960001 399960001 3111200 -55973300 135750 -907896 11461851
-42637104 399960001 399960001]
[399960001 399960001 71952825 -58020184 -76479981 -18754190 7753330
1334674 399960001 399960001]
[399960001 399960001 399960001 399960001 399960001 399960001 399960001
399960001 399960001 399960001]
[399960001 399960001 399960001 399960001 399960001 399960001 399960001
399960001 399960001 399960001]]
divide_array:
[[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 4.73991083e-01 -1.02232030e+01
2.94386077e-01 -7.65105008e+00 3.54703157e-01 -3.03616042e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 4.53689759e-02 2.28776259e+00
-8.54364211e-01 -1.01904000e+00 -1.14570749e+00 5.78140960e-01
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 8.02634287e-03 1.06298430e+00
-1.76060013e+00 2.00916124e+00 -7.99841301e-01 4.14235986e-01
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.64805734e+00 -4.76098636e-01
2.42188450e+00 -1.56000000e+00 -6.07007204e+00 1.46604086e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 2.05708408e-01 -6.98770950e-01
6.03333333e+02 -1.74649123e+01 1.51327129e-01 -1.50705020e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.02192826e+00 -7.04197422e-01
-1.11851494e+00 -3.08412413e-01 1.16156671e+01 8.71231477e-02
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00]]

From the above calculation results , We can see the background pixel value -19999 The change of , The background value will change with the calculation , So how to keep the background value unchanged ? Or keep the result of each operation of the background value as 0 value .

We can use the following two methods to achieve :

Method 1 : Use conditional operations to force a change in the background value

Conditional operation function :np.where( Ternary operator )

np.where(nparray == -19999, -19999, nparray+-*/n )

  The above code means , If the value of the array is equal to -19999, It is assigned as -19999, Otherwise, it will be calculated as a new value , That is, only non background values are calculated . More complex operations require more conditional operations .

stay arcgis It shows that nodata The background of , We use gdal When reading, it can be regarded as 0 Value to process .

Method 2 : Record the position of background value with data , Assign a new value to the background value after all calculations are completed

Still use the conditional operation function :np.where( Ternary operator )

location_array=np.where(nparray == -19999, 0, 1)

  The above code means , If the value of the array is equal to -19999, It is assigned as 0, Otherwise 1.

After all the calculations are completed , We can use the last calculated array , Assign background values to 0:

end_array=end_array*location_array

  Or keep -19999:

end_array=end_array*location_array
end_array=np.where(end_array== 0, -19999, end_array)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved