我正在使用Tkinter创建一个图形界面,以便能够确定NDVI,我正在使用Opencv在植被中制作过滤器。到目前为止一切都很好。 计算NDVI时,生成的图像为黑色:(,您无法保存,但是当您打印图像时,您将获得每个像素的值。
代码
def ndvi(self,*args):
#self.res_guar esta en BGR
self.res_guar = self.res_guar.astype('float')
(self.r,self.g,self.b) = cv2.split(self.res_guar)
#Calculo NDVI
self.resta = cv2.subtract(self.r,self.b)
self.suma = cv2.add(self.r,self.b)
self.ndvi = cv2.divide(self.resta,self.suma)
self.rows,self.cols,_ = self.res_guar.shape
self.y = np.zeros((self.rows,self.cols,3),dtype=np.uint8) # for output
greenyellow = [4,255,173]
yellow = [0,255,255]
yellowred = [4,111,255]
green = [0,255,0]
blue = [255,0,0]
red = [0,0,255]
white = [255,255,255]
black = [0,0,0]
for i in range(self.rows):
for j in range(self.cols):
k = self.ndvi[i,j]
if 0.0 < k < 0.2 :
self.y[i,j] = yellowred
#print "Pintando AmarilloRojo"
break
elif 0.2 < k < 0.4 :
self.y[i,j] = yellow
#print "Pintando Amarillo"
break
elif 0.4 < k < 0.6 :
self.y[i,j] = greenyellow
#print "Pintando AmarilloVerde"
break
elif k > 0.6 :
self.y[i,j] = green
#print "Pintando Verde"
break
else:
self.y[i,j] = black
#print "Negro"
break
#Print Imagen resultante
print(self.y)
#Guardar Imagen
cv2.imwrite('NDVI.jpg',self.y)
#Mostrar Imagen
ndvi = Image.fromarray(self.y,'RGB').resize((570,650), Image.ANTIALIAS)
# convierte a formato ImageTk
ndvi = ImageTk.PhotoImage(ndvi)
# Ajuste de la imagen de hsv a tk etiqueta de imagen
self.original_img_lbl.configure(image=ndvi)
# adding a reference to the image to Prevent python's garbage collection from deleting it
#Anadiendo una referencia a la imagen para evitar que python garbage collection lo elimine
self.original_img_lbl.image = ndvi
图片原创 URL = http://es.zimagez.com/zimage/capturadepantallade2016-12-2310-08-43.php
检控 URL = http://es.zimagez.com/zimage/capturadepantallade2016-12-2310-09-33.php
NDVI URL = http://es.zimagez.com/zimage/capturadepantallade2016-12-2310-09-40.php
打印self.y
[[[ 4 111 255]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 4 111 255]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 4 111 255]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
...,
[[ 4 255 173]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 4 255 173]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 4 255 173]
[ 0 0 0]
[ 0 0 0]
...,
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]]
[[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]
...,
[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...,
[0 0 0]
[0 0 0]
[0 0 0]]]
已保存NDVI图像 URL = http://es.zimagez.com/zimage/ndvi.php
答案 0 :(得分:1)
从break
语句中取出所有if
语句。您的if
语句会更改一个像素的值,但随后您{for}循环并且没有其他像素被写入,会发生什么情况。删除所有break
语句,你应该没事。