High resolution image to remove attributes in low resolution raster using array - python

I have a dataset which has a high resolution of 30m (let's say classification Land-Use data) and another dataset with a lower resolution of 36km (let's say Evaporation data) for the same region. I want to remove some points from the lower resolution array-based on the high-resolution array. For example, I want to exclude the pixels in Evaporation data, which have a Land-use class '10' above a certain threshold/percentage.
Description (if needed):
Let's consider the high-resolution (first image below) to an array of 10x10, and a lower resolution image to be an array of 2x2 (second image below).
I want to remove points on lower resolution image based on values of higher resolution image. Consider them overlapping perfectly, let's say if a said threshold of zero's (let's say more than 50%) from the first image appear in the quadrant (based on second image quadrant) a NaN value would be assigned to the second image pixel.
I have done this kind of masking using ArcMaps, but I have no idea if this is possible using python.

To use NumPy, you need to transform the low-res data onto the same grid as the high-res data, requiring them to be perfectly aligned. You could use scipy.ndimage.zoom for this (see example on the docs page).
Or, if they aren't perfectly aligned (or rotated, or whatever), then geopandas is perfect for this.

Related

Image Operations with Python

I hope you're all doing well!
I'm new to Image Manipulation, and so I want to apologize right here for my simple question. I'm currently working on a problem that involves classifying an object called jet into two known categories. This object is made of sub-objects. My idea is to use this sub-objects to transform each jet in a pixel image, and then applying convolutional neural networks to find the patterns.
Here is an example of the pixel images:
jet's constituents pixel distribution
To standardize all the images, I want to find the two most intense pixels and make sure the axis connecting them is in the vertical direction, as well as make sure that the most intense pixel is at the top. It also would be good to impose that one of the sides (left or right) of the image contains the majority of the intensity and to normalize the intensity of the whole image to 1.
My question is: as I'm new to this kind of processing, I don't know if there is a library in Python that can handle these operations. Are you aware of any?
PS: the picture was taken from here:https://arxiv.org/abs/1407.5675
You can look into OpenCV library for Python:
https://docs.opencv.org/master/d6/d00/tutorial_py_root.html.
It supports a lot of image processing functions.
In your case, it probably would be easier to convert the image into a more suitable color space in which one axis stands for color intensity (e.g HSI, HSL, HSV) and trying to find indices of the maximum values along this axis (this should return the pixels with the highest intensity in the image).
Generally, in Python, we use PIL library for basic manipulations with images and OpenCV for advances ones.
But, if understand your task correctly, you can just think of an image as a multidimensional array and use numpy to manipulate it.
For example, if your image is stored in a variable of type numpy.array called img, you can find maximum value along the desired axis just by writing:
img.max(axis=0)
To normalize image you can use:
img /= img.max()
To find which image part is brighter, you can split an img array into desired parts and calculate their mean:
left = img[:, :int(img.shape[1]/2), :]
right = img[:, int(img.shape[1]/2):, :]
left_mean = left.mean()
right_mean = right.mean()

libvips Nearest Neighbor / Bicubic Deep Zoom Pyramid Creation

I'm in the process of moving some of my code off of openzoom.py and onto Libvips but am not sure how dictate the interpolation method, which is important. I at the very least need to be able to use bicubic/bilinear in one case and nearest neighbors in the other case.
My old code is as follows:
creator = deepzoom.ImageCreator(tile_size=128, tile_overlap=2, tile_format="png",
image_quality=0.8, resize_filter="nearest")
creator.create(sourceFile, destFile)
Currently, using pyvips I have the following
image = pyvips.Image.new_from_file(sourceFile)
image.dzsave(destFile, tile_size=128, overlap=2,
suffix='.png[Q=80]')
Any help would be greatly appreciated :)
By default, dzsave will average each 2x2 block of pixels, which is equivalent to bilinear.
Sometimes, for example with images where pixel values represent labels rather than intensity, you need a non-interpolatory downsize. For these cases, you can use the region_shrink parameter to select median or mode, which will both preserve label values.
I would use:
image = pyvips.Image.new_from_file(sourceFile, access='sequential')
image.dzsave(destFile,
overlap=1,
tile_size=126,
region_shrink='mode',
suffix='.png')
Don't forget to set the access hint. It'll give you a huge improvement in speed and memory behaviour for large images that don't support random access.
The PNG Q number sets quantization quality when outputting palettized images. Perhaps you mean compression? libvips defaults to 6, the PNG standard.
Are you sure you want overlap=2? The deepzoom standard is overlap 1. Overlap 1 means there is one pixel extra around the edge of every tile, so tiles in the centre of the image will share two pixels on every edge with their neighbours. Setting overlap=2 means you'll have four pixel overlaps, confusingly.
Likewise, tile_size=128 means most of your tiles will be 132x132 pixels. It doesn't matter for PNG, but JPG works best with multiples of 8 on an axis. I would set tile_size to (128 - 2 * overlap), as deepzoom does by default.
git master libvips adds max, min and nearest (always pick the top-left pixel) as well. A branch has lanczos3, but it was never merged for various reasons.

Remove outliers in an image after applying treshold

Here`s the deal. I want to create a mask that visualizes all the changes between two images (GeoTiffs which are converted to 2D numpy arrays).
For that I simply subtract the pixel values and normalize the absolute value of the subtraction:
Since the result will be covered in noise, I use a treshold and remove all pixels with a value below a certain limit.
def treshold(array, thresholdLimit):
print("Treshold...")
result = (array > thresholdLimit) * array
return result
This works without a problem. Now comes the issue. When applying the treshold, outliers remain, which is not intended:
What is a good way to remove those outliers?
Sometimes the outliers are small chunks of pixels, like 5-6 pixels together, how could those be removed?
Additionally, the images I use are about 10000x10000 pixels.
I would appreciate all advice!
EDIT:
Both images are landsat satelite images, covering the exact same area.
The difference here is that one image shows cloud coverage and the other one is free of clouds.
The bright snakey line in the top right is part of a river that has been covered by a cloud. Since water bodies like the ocean or rivers are depicted black in those images, the difference between the bright cloud and the dark river results in the river showing a high degree of change.
I hope the following images make this clear:
Source tiffs :
Subtraction result:
I also tried to smooth the result of the tresholding by using a median filter but the result was still covered in outliers:
from scipy.ndimage import median_filter
def filter(array, limit):
print("Median-Filter...")
filteredImg = np.array(median_filter(array, size=limit)).astype(np.float32)
return filteredImg
I would suggest the following:
Before proceeding please double check if the two images are 100% registered. To check that you should overlay them using e.g. different color channels. Even minimal registration errors can render your task impossible
Smooth both input images slightly (before the subtraction). For that I would suggest you use standard implementations. Play around with the filter parameters to find an acceptable compromise between smoothness (or reduction of graininess of source image 1) and resolution
Then try to match the image statistics by applying histogram normalization, using the histogram of image 2 as a target for the histogram of image 1. For this you can also use e.g. the OpenCV implementation
Subtract the images
If you then still observe obvious noise, look at the histogram of the subtraction result and see if you can relate the noise to intensity outliers. If you can clearly separate signal and noise based on intensity, apply again a thresholding (informed by your histogram). Alternatively (or additionally), if the noise is structurally different from your signal (e.g. clustered), you could look into morphological operations to remove it.

Rawpy: How to postprocess raw images WITHOUT adulterating pixel data?

I need to convert raw NEF images into numpy arrays for quantitative analysis. I'm currently using rawpy to do this, but I've failed to find a combination of postprocess parameters that leave the pixel data untouched. (I'll be the first to admit I don't entirely understand how raw files work also...)
Here is what I have right now:
rawArray = raw.postprocess(demosaic_algorithm=rawpy.DemosaicAlgorithm.AHD,
half_size=False,
four_color_rgb=False,use_camera_wb=False,
use_auto_wb=False,user_wb=(1,1,1,1),
output_color=rawpy.ColorSpace.raw,
output_bps=16,user_flip=None,
user_black=None,user_sat=None,
no_auto_bright=False,auto_bright_thr=0.01,
adjust_maximum_thr=0,bright=100.0,
highlight_mode=rawpy.HighlightMode.Ignore,
exp_shift=None,exp_preserve_highlights=0.0,
no_auto_scale=True,gamma=(2.222, 4.5),
chromatic_aberration=None,
bad_pixels_path=None)
Postprocessing, which means demosaicing here, will always change the original pixel values. Typically what you want is to get a linearly postprocessed image so that roughly the number of photons are in linear relation to the pixel values. You can do that with:
rgb = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
In most cases you will not be able to get a calibrated image out where you can directly infer the number of photons that hit the sensor at each pixel. See also https://www.dpreview.com/forums/post/56232710.
Note that you can also access the raw image data via raw.raw_image (see also Bayer matrix) which is as close to the sensor data as you can get -- no interpolation or camera curves etc are applied here, but I would say scientifically you don't get much more than you would get with a linearly postprocessed image as described above.

How to find neighbors in binary image with given horizontal and vertical distance (Python)

I have an Image (or several hundreds of them) that need to be analyzed. The goal is to find all black spots close to each other.
For example all black spots with a Horizontal distance of 160 pixel and vertical 40 pixel.
For now I just look at each Pixel and if there is a black pixel I call a recursive Method to find its neighbours (i can post the code too if you want to)
It works, but its very slow. At the moment the script runs about 3-4 minutes depending on image size.
Is there some easy/fast way to accomplish this (best would be a scikit-image method to help out here) I'm using Python.
edit: I tried to use scikit.measure.find_contours, now i have an array with arrays containing the contours of the black spots. Now I only need to find the contours in the neighbourhood of these contours.
When you get the coordinates of the different black spots, rather than computing all distances between all pairs of black pixels, you can use a cKDTree (in scipy.spatial, http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html#scipy.spatial.cKDTree). The exact method of cKDTree to use depends on your exact criterion (you can for example use cKDTree.query_ball_tree to know whether there exists a pair of points belonging to two different labels, with a maximal distance that you give).
KDTrees are a great method to reduce the complexity of problems based on neighboring points. If you want to use KDTrees, you'll need to rescale the coordinates so that you can use one of the classical norms to compute the distance between points.
Disclaimer: I'm not proficient with the scikit image library at all, but I've tackled similar problems using MATLAB so I've searched for the equivalent methods in scikit, and I hope my findings below help you.
First you can use skimage.measure.label which returns label_image, i.e. an image where all connected regions are labelled with the same number. I believe you should call this function with background=255 because from your description it seems that the background in your images is the while region (hence the value 255).
This is essentially an image where the background pixels are assigned the value 0 and the pixels that make up each (connected) spot are assigned the value of an integer label, so all the pixels of one spot will be labelled with the value 1, the pixels of another spot will be labelled with the value 2, and so on. Below I'll refer to "spots" and "labelled regions" interchangeably.
You can then call skimage.measure.regionprops, that takes as input the label_image obtained in the previous step. This function returns a list of RegionProperties (one for each labelled region), which is a summary of properties of a labelled region.
Depending on your definition of
The goal is to find all black spots close to each other.
there are different fields of the RegionProperties that you can use to help solve your problem:
bbox gives you the set of coordinates of the bounding box that contains that labelled region,
centroid gives you the coordinates of the centroid pixel of that labelled region,
local_centroid gives you the centroid relative to the bounding box bbox
(Note there are also area and bbox_area properties which you can use to decide whether to throw away very small spots that you might not be interested in, thus reducing computation time when it comes to comparing proximity of each pair of spots)
If you're looking for a coarse comparison, then comparing the centroid or local_centroid between each pair of labelled regions might be enough.
Otherwise you can use the bbox coordinates to measure the exact distance between the outer bounds of any two regions.
If you want to base the decision on the precise distance between the pixel(s) of each pair of regions that are closest to each other, then you'll likely have to use the coords property.
If your input image is binary, you could separate your regions of interest as follows:
"grow" all the regions by the expected distance (actually half of it, as you grow from "both sides of the gap") with binary_dilation, where the structure is a kernel (e.g. rectangular: http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.rectangle) of, let's say, 20x80pixels;
use the resulting mask as an input to skimage.measure.label to assign different values for different regions' pixels;
multiply your input image by the mask created above to zero dilated pixels.
Here are the results of proposed method on your image and kernel = rectange(5,5):
Dilated binary image (output of step 1):
Labeled version of the above (output of step 2):
Multiplication results (output of step 3):

Categories

Resources