2

I would like to find the statistical correlation between all the pixels of an image in relation to the bands. In my case 7.

from osgeo import gdal
from osgeo import gdal_array
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import scatter_matrix
%matplotlib inline

dataset = gdal.Open( "C:\Mosaico.tif") 
image_datatype = dataset.GetRasterBand(1).DataType
image = np.zeros((dataset.RasterYSize, dataset.RasterXSize, dataset.RasterCount),
                  dtype=gdal_array.GDALTypeCodeToNumericTypeCode(image_datatype))   

for b in range(dataset.RasterCount):
    band = dataset.GetRasterBand(b + 1)
    image[:, :, b] = band.ReadAsArray()

df=pd.DataFrame(np.concatenate(image))

scatter_matrix(df,figsize = (10,10), diagonal = 'hist')
  • 1
    Can you expand a bit more “doesn’t work”part? – fatih_dur Dec 8 '19 at 0:32
  • I changed my code because I don't think it was right, I think I'll let him read the pandas in the image array, but I noticed that with seaborn.scatterplot it also causes me problems – vins_26 Dec 8 '19 at 10:04
  • Can you please review your code before posting here? It looks like half of it is coming from somewhere else. Where did you define img, where do you use srcband and img_array, etc.? – fatih_dur Dec 8 '19 at 23:44
  • I'm sorry, and that in a fury of doing tests I copied the code without looking at it properly, is driving me crazy – vins_26 Dec 10 '19 at 18:53
  • I use that loop code to make sure because I noticed that otherwise the image array was represented by null values, with this I think I have solved that problem. – vins_26 Dec 10 '19 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.