I have tiff files extracted from google earth engine from the same boundary location. I open these files using rasterio in python and then convert them into numpy array. But what happens is that despite the numpy arrays showing the same area they're misaligned. How can I fix this. I'm using the code below to read the tiff files and save them
import rasterio as rs
from rasterio.plot import reshape_as_image
from skimage import exposure
import numpy as np
import cv2
raster_file = reshape_as_image(rs.open(file_path).read())
mask = np.ones_like(raster_file)
mask[np.isnan(raster_file)] = 0
img_fixed = exposure.equalize_hist(raster_file,mask=mask)
img_fixed *= 255
img_fixed = img_fixed.astype('uint8')
png_file_path = file_path[:-4] + ".png"
cv2.imwrite(png_file_path, img_fixed)
One of the images is sentinel-1 ASCENDING VH 2022
and the other one from the same satellite and DESCENDING VV 2018
when using the data from the same year I don't have such a problem they're exactly aligned but when they're from different years they're not.
I appreciate any help :)
It seems that the problems is the different crs of the tif files. The solutions are either to download tif files using the same crs files (Google earth engine has an argument for this here is how to do this) or simply convert the existing tif file crs. Rasterio can reproject files in a different crs formats. Here is an example
Related
I want to select grid cells from ERA5 gridded data (surface level only) that are inside geographical masks for North- and South-Switzerland (plus the radar buffer), to calculate regional means.
The 4 masks (masks) are given as polygons/multipolygons (polygons) in a shapefile and so far for 2 of the masks I was able to use salem roi to get what I want:
radar_north = salem.read_shapefile('radar_north140.shp')
file_radar_north = file.salem.roi(shape=radar_north)
file_radar_north.cape.mean(dim='time').salem.quick_map()
However, for the radar_south and alpensuedseite shapefiles the code didn´t work at the beginning (wrong selection or shows no data), and now the nothing works anymore (?). I don´t know why, as I have not changed anything from the first time to the second.
If someone sees the issue or knows a different way to mask the ERA data (which is maybe quicker) I would be grateful! (I was unsuccessfull with the answers from similar questions here).
Best
Lena
This could work if you are working on netcdf files
import geopandas as gpd
import xarray as xr
import rioxarray
from shapely.geometry import mapping
# load shapefile with geopandas
radar_north = gpd.read_file('radar_north140.shp')
# load ERA5 netcdf with xarray
era = xr.open_dataset('ERA5.nc')
# add projection system to nc
era = era.rio.write_crs("EPSG:4326", inplace=True)
# mask ERA5 data with shapefile
era_radar_north = era.rio.clip(radar_north.geometry.apply(mapping), radar_north.crs)
I have a satellite image of 7-channels (Basically I have seven .tif files, one for each band). And I have a .csv file with coordinates of points-of-interest that are in the region shot by the satellite. I want to cut small portions of the image in the surroundings of each coordinate point. How could I do that?
As I don't have a full working code right now, it really doesn't matter the size of those small portions of image. For the explanation of this question let's say that I want them to be 15x15 pixels. So for the moment, my final objective is to obtain a lot of 15x15x7 vectors, one for every coordinate point that I have in the .csv file. And that is what I am stucked with. (the "7" in the "15x15x7" is because the image has 7 channels)
Just to give some background in case it's relevant: I will use those vectors later to train a CNN model in keras.
This is what I did so far: (I am using jupyter notebook, anaconda environment)
imported gdal, numpy, matplotlib, geopandas, among other libraries.
Opened the .gif files using gdal, converted them into arrays
Opened the .csv file using pandas.
Created a numpy array called "imagen" of shape (7931, 7901, 3) that will host the 7 bands of the satellite image (in form of numbers). At this point I just need to know which rows and colums of the array "imagen" correspond to each coordinate point. In other words I need to convert every coordinate point into a pair of numbers (row,colum). And that is what I am stucked with.
After that, I think that the "cutting part" will be easy.
#I import libraries
from osgeo import gdal_array
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import geopandas
from geopandas import GeoDataFrame
from shapely.geometry import Point
#I access the satellite images (I just show one here to make it short)
b1 = r"E:\Imágenes Satelitales\2017\226_86\1\LC08_L1TP_226086_20170116_20170311_01_T1_sr_band1.tif"
band1 = gdal.Open(b1, gdal.GA_ReadOnly)
#I open the .csv file
file_svc = "C:\\Users\\Administrador\Desktop\DeepLearningInternship\Crop Yield Prediction\Crop Type Classification model - CNN\First\T28_Pringles4.csv"
df = pd.read_csv(file_svc)
print(df.head())
That prints something like this:
Lat1 Long1 CropingState
-37.75737 -61.14537 Barbecho
-37.78152 -61.15872 Verdeo invierno
-37.78248 -61.17755 Barbecho
-37.78018 -61.17357 Campo natural
-37.78850 -61.18501 Campo natural
#I create the array "imagen" (I only show one channel here to make it short)
imagen = (np.zeros(7931*7901*7, dtype = np.float32)).reshape(7931,7901,7)
imagen[:,:,0] = band1.ReadAsArray().astype(np.float32)
#And then I can plot it:
plt.imshow(imagen[:,:,0], cmap = 'hot')
plt.plot()
Which plots something like this:
(https://github.com/jamesluc007/DeepLearningInternship/blob/master/Crop%20Yield%20Prediction/Crop%20Type%20Classification%20model%20-%20CNN/First/red_band.png)
I want to transform those (-37,-61) into something like (2230,1750). But I haven't figured it how yet. Any clues?
I've seen that vtkplotter supports a wide range of volumetric image formats, but does it support plotting numpy arrays? I have an image in a .mhd format that I converted to .npz file and would like to plot it using the mentioned library. Is there a way to do so?
import numpy as np
data = np.zeros((3,3,3))
# how to plot this?
If you know any other tools for visualizing volumetric data, suited for medical analysis, please tell so. I've tried mayavi but it consumes so much memory that it crashes my computer.
You can use numpy_support to convert numpy arrays to vtkImageData (i.e. a VTK 3D image).
An example follows, there's a numpy warning about type conversion: I don't think it's relevant but I'm not a python guru...
from vtk.util import numpy_support
import vtk
import numpy as np
data = np.zeros((3,3,3))
# vtkImageData is the vtk image volume type
imdata = vtk.vtkImageData()
# this is where the conversion happens
depthArray = numpy_support.numpy_to_vtk(data.ravel(), deep=True, array_type=vtk.VTK_DOUBLE)
# fill the vtk image data object
imdata.SetDimensions(data.shape)
imdata.SetSpacing([1,1,1])
imdata.SetOrigin([0,0,0])
imdata.GetPointData().SetScalars(depthArray)
# f.ex. save it as mhd file
writer = vtk.vtkMetaImageWriter()
writer.SetFileName("test.mhd")
writer.SetInputData(imdata)
writer.Write()
... and to visualize vtk volume data you can use Paraview.
I have a set of dicom images(contains 160 .dcm files). I can visualize a single file by the following python code:
import pydicom as dicom
import os
import numpy
import matplotlib.pyplot as plt
filename = "./myfiles/MR000130.dcm";
dataset = dicom.dcmread(filename)
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()
My question is:
How do I visualize these whole bunch of volumetric data as a single picture?
Is there any way to obtain the z-buffer information?
It's in the file. There is stuff in the DICOM apart from pixel data. See it with
print(dataset._pretty_str).
It might be dataset.SliceLocation.
We can't tell you how to change 3D to 2D. You could read all the slices, and then reslice in other planes. You could do some fancy segmentation and render surfaces in 3D. You need to decide what is most appropriate for your case.
I am trying to use SVMs to classify a set if images I have on my computer into 3 categories :
I am just facing a problem of how to load the data as in the following example , he uses a data set that is already saved.
http://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html
Me I have all the images in png format saved in a folder on my pc
You can load data as numpy arrays using Pillow, in this way:
from PIL import Image
import numpy as np
data = np.array(Image.open('yourimg.png')) # .astype(float) if necessary
couple it with os.listdir to read multiple files, e.g.
import os
for file in os.listdir('your_dir/'):
img = Image.open(os.path.join('your_dir/', file))
data = np.array(img)
your_model.train(data)