【发布时间】:2020-01-14 06:11:29
【问题描述】:
我是一名工科学生,正在从事一个基于 NDVI 计算的项目来监测作物健康状况。为了获得 NIR 和Red Region 的值,我在实验中使用了带有蓝色滤光片的 PiNoIR 相机。我使用以下代码提取所需的值并计算 NDVI。但是在输出图像中,空白区域(如下图所示没有叶子的区域)和地面具有更高的 NDVI 值。阴影区域显示在从0.5 到0.6 的范围内。我想知道输出是否正确以及可以在 -code 中进行哪些更正以更正错误。代码如下。
from PIL import Image
import numpy as np
import cv2
from cv2 import imread
from matplotlib import cm
rgb_matrix =cv2.imread('inputimg.jpg')
w=rgb_matrix.shape[1] #columns
h=rgb_matrix.shape[0] #rows
print(w)
print(h)
#Compute ndvi values for each pixel
#NDVI=(NIR-R)/(NIR+R)
res=[]
for i in range(h):
row=[]
for j in range(w):
val=rgb_matrix[i][j]
n=val[2]
r=val[1]
num=((int(n)-int(r)))
den=((int(n)+int(r)))
if(den == 0):
r=0.0
else:
r=np.divide(num,den)
row.append(r)
res.append(row)
print('Done')
#based on NDVI values, give different colors for easier identification
for i in range(h):
for j in range(w):
if(res[i][j] >=-1 and res[i][j] <0):
rgb_matrix[i][j]=[128,128,128] #grey
elif(res[i][j]>=0 and res[i][j]<0.2):
rgb_matrix[i][j]=[64,255,0] #parrot green
elif(res[i][j]>=0.2 and res[i][j]<0.3):
rgb_matrix[i][j]=[125,255,255] #yellow
elif(res[i][j]>=0.3 and res[i][j]<0.4):
rgb_matrix[i][j]=[0,128,128] #dark green
elif(res[i][j]>=0.4 and res[i][j]<0.5):
rgb_matrix[i][j]=[255,255,0] #sky blue
elif(res[i][j]>=0.5 and res[i][j]<0.6):
rgb_matrix[i][j]=[255,51,153] #purple
elif(res[i][j]>=0.6 and res[i][j]<0.7):
rgb_matrix[i][j]=[0,128,255] #orange
elif(res[i][j]>=0.7 and res[i][j]<0.8):
rgb_matrix[i][j]=[255,43,255] #pink
elif(res[i][j]>=0.8 and res[i][j]<0.9):
rgb_matrix[i][j]=[40,40,255] #red
else:
rgb_matrix[i][j]=[255,0,0] #dark blue
cv2.imwrite('outputimg.jpg',rgb_matrix)
print("Completed!!")
(Ignore the induntation errors)
【问题讨论】:
-
没有原始图像很难说。但是你 110% 确定你正确地抓住了 NIR 和红色波段吗?反转问题听起来像是使用了哪些频段的问题。
-
请附上您的图片或链接。谢谢。
-
@Nebulous29 我们从下面提到的链接中得到了转移的概念。据此,使用过滤器将生成(NIR,R,G)图像而不是(R,G,B)图像。链接:publiclab.org/wiki/ndvi