1

I have a problem with raster aligning.

Given:

  1. Two rasters that need to be aligned. Rasters a little bit shifted, scaled and rotated.
  2. Two arrays of matched keypoints.
  3. Transformation is wrong, so usual methods don't work.

My code:

 with rasterio.open('non_aligned_imagery/121817_Sq_GH3_index_ndvi.tif') as src:
    # checkout array
    data = src.read()
    reshaped = data[0]
    rows, cols = reshaped.shape

    # clean-up
    reshaped[reshaped < 0] = 0

    # Select 3 first points from a list
    pts1 = np.float32(end_almost_aligned[0:3])
    pts2 = np.float32(end_test[0:3])

    # Calculating affine matrix
    M = cv2.getAffineTransform(pts1,pts2)

    # Use matrix on array
    arr_c = cv2.warpAffine(reshaped, M, (cols,rows))

    # Open base image
    with rasterio.open('alignment_data/10_31_17_gh3_ndvi.tif') as base:
        kwargs = base.meta
        # write image with base
        with rasterio.open('tmp/3_points_old_transform.tif', 'w', **kwargs) as dst:
            dst.write(arr_c.reshape(data.shape))

result: https://prnt.sc/iiov5o as you see image pixels are shifted. Now i'm trying to play with affine matrix coefficients to find a problem.

rasterio meta src.meta = {'width': 3657, 'count': 1, 'affine': Affine(0.11285, 0.0, 318832.68138, 0.0, -0.11285, 4001963.8552400004), 'driver': 'GTiff', 'crs': CRS({'init': 'epsg:32611'}), 'dtype': 'float32', 'nodata': -10000.0, 'transform': (318832.68138, 0.11285, 0.0, 4001963.8552400004, 0.0, -0.11285), 'height': 3618}

base.meta = {'width': 3823, 'count': 1, 'affine': Affine(0.10329, 0.0, 318843.80832, 0.0, -0.10329, 4001960.77144), 'driver': 'GTiff', 'crs': CRS({'init': 'epsg:32611'}), 'dtype': 'float32', 'nodata': None, 'transform': (318843.80832, 0.10329, 0.0, 4001960.77144, 0.0, -0.10329), 'height': 3894}

  • Affine matrix looks like this [[ 1.00072057e+00, -9.56156632e-05, 1.53423162e+02], [ 6.59586069e-04, 1.00414499e+00, -1.67980755e+04]] And if I make 0 two last elements it will works better. I assume that problem in points coordinates that have a difference in 2, 3 digit after separator. I tried to normalize coordinates before matrix creation, It helpes a little, but results still don't poor. – Davion Feb 26 '18 at 9:40

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.