예제 #1
0
    def viewNDVI(self):
        fileNameDir = "Data/1999_2017_landsat_time_series/roi/cut/fileNames.txt"
        if 'fileNameDir' in locals():
            pass
        else:
            raise AssertionError("FILE_NAME_DIR is not given!")

        with open(fileNameDir, "r") as file:
            imageFileNames = file.readlines()  # imageFiles is a list
            file.close()

        NDVI_seq = []
        fig, ax = plt.subplots()

        for fileName in imageFileNames:
            fileNameNew = "Data/1999_2017_landsat_time_series/roi/cut/" + fileName[:
                                                                                   -1] + ".tif"

            raster = gdal.Open(fileNameNew)
            data = Toolbox.readGeotiff(raster)
            # close dataset
            raster = None

            # If data is more than one band!
            if len(data.shape) != 3:
                continue
            if data.shape[0] < 3:
                continue
            x_size = data.shape[2]
            y_size = data.shape[1]
            '''
            ###########
            img = QImage(fileName)
            ptr = img.bits()
            ptr.setsize(img.byteCount())

            ## copy the data out as a string
            strData = ptr.asstring()
            ## get a read-only buffer to access the data
            buf = memoryview(ptr)
            ## view the data as a read-only numpy array            
            arr = np.frombuffer(buf, dtype=np.ubyte).reshape(img.height(), img.width(), 4)
            ## view the data as a writable numpy array
            arr = np.asarray(ptr).reshape(img.height(), img.width(), 4)
            ############            
            '''

            # Choose the band:

            ### Bug solved - data type should be declared earlier!
            imageNew = np.zeros((y_size, x_size, 4), dtype=np.uint8)
            imageNew[:, :, 0] = data[2, :, :]
            imageNew[:, :, 1] = data[1, :, :]
            imageNew[:, :, 2] = data[0, :, :]
            #           if data.shape[0] == 6:
            #                imageNew[:, :, 0] = data[2, :, :]
            #                imageNew[:, :, 1] = data[1, :, :]
            #                imageNew[:, :, 2] = data[0, :, :]
            #            else:
            #                imageNew[:, :, 0] = data[3, :, :]
            #                imageNew[:, :, 1] = data[3, :, :]
            #                imageNew[:, :, 2] = data[3, :, :]
            imageNew[:, :, 3] = 255

            qimg = QImage(imageNew, x_size, y_size, QImage.Format_RGB32)

            self.imageLabel.setPixmap(QPixmap.fromImage(qimg))

            self.fitToWindow()
            self.scaleFactor = 2.0
            self.updateActions()
            app.processEvents()

            # Change the title
            QMainWindow.setWindowTitle(self, fileName)

            self.imageLabel.setObjectName("image")
            self.imageLabel.mousePressEvent = self.getPos

            actualX = self.x * self.scaleFactor
            actualY = self.y * self.scaleFactor
            #            qcolor = QColor(qimg.pixel(actualX, actualY))
            #            # BGR
            #            red = qcolor.red()
            #            green = qcolor.green()
            #            blue = qcolor.blue()
            #
            #            print("Scalefactor is: ", self.scaleFactor)
            print("Cursor locations x & y:", actualX, actualY)
            #            print("Red, green, blue values: ", red, green, blue)

            sentinel_No = fileName[9]
            print("sentinel number is:", int(sentinel_No))

            actualX = int(actualX)
            actualY = int(actualY)

            ######
            # Search for similar temporal patterns
            actualX = 2495
            actualY = 310
            ######

            if data.shape[0] >= 4:
                if sentinel_No == 8:
                    NDVI = Toolbox.NDVI_3by3_8(data[:, actualY, actualX])
                    NDVI_seq.append(NDVI)
                else:
                    NDVI = Toolbox.NDVI_3by3_7(data[:, actualY, actualX])
                    NDVI_seq.append(NDVI)

            ax.cla()
            plt.plot(NDVI_seq)
            plt.ylim(0, 250)
            plt_name = str(actualX) + str(actualY) + '.png'
            plt.savefig(plt_name)
            # ax.set_title(fileName)

            # plt.pause(0.1)
            # time.sleep(0.02)

            del data, imageNew, qimg