question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to rescale the image after rio-color operations?

See original GitHub issue

According to documentation, rio-color operations only take rasters of dimensions (3, x, y) for processing, in 0 to 1 range for pixel values.

I’m facing some issues getting the image into the same state as doing rio color --co photometric=rgb stack.tiff landsat8_color.tiff sigmoidal RGB 20 0.2 -j 1

The code I’ve used is as follows.

# initializing and reading the bands

import rasterio as rio
import numpy as np
from rio_color import operations, utils

R10 = '/Users/shivashis.ext/felicette-data/LC81390462020136'
b4 = rio.open(R10+'/LC81390462020136-b4.tiff')
b3 = rio.open(R10+'/LC81390462020136-b3.tiff')
b2 = rio.open(R10+'/LC81390462020136-b2.tiff')
# getting the bands in the range of [0..1]
r = b4.read(1)
g = b3.read(1)
b = b2.read(1)
norm_r = np.linalg.norm(r)
norm_g = np.linalg.norm(g)
norm_b = np.linalg.norm(b)
r = r / norm_r
g = g / norm_g
b = b / norm_b

# making and processing image
img = np.array([r,g,b])

img = operations.sigmoidal(img, 20, 0.2)

# from matplotlib import pyplot as plt
norm_r = img[0]
norm_r = utils.scale_dtype(img[0], np.uint16)# np.interp(norm_r, (norm_r.min(), norm_r.max()), (0, 65535))
norm_g = img[1]
norm_g = utils.scale_dtype(img[1], np.uint16)#np.interp(norm_g, (norm_g.min(), norm_g.max()), (0, 65535))
norm_b = img[2]
norm_b = utils.scale_dtype(img[2], np.uint16)#np.interp(norm_b, (norm_b.min(), norm_b.max()), (0, 65535))

# writing back to file

out_tiff = R10 + '/stack_prog_color_2.tiff'
with rio.open(out_tiff,'w',driver='Gtiff', width=b4.width, height=b4.height, 
              count=3,crs=b4.crs,transform=b4.transform, dtype=np.uint16, photometric="RGB") as rgb:
    rgb.write(norm_r.astype(np.uint16),1) 
    rgb.write(norm_g.astype(np.uint16),2) 
    rgb.write(norm_b.astype(np.uint16),3) 
    rgb.close()

As one can see, I’ve used both in-house scale_dtype and Numpy’s linear interpolation to scale the array back, without success.

Also, I planned to save the numpy response of sigmoid function as a pickle file, and debug keeping it as reference, but since the job is parallel by rio-mucho, it got too complex in very short time.

I am almost sure that I’m scaling the image back wrong, because size of the the output tiffs with a) command line and b) Python API are same. (Both use np.uint16 to store data)

Please let me also know, if any other detail is required to understand/debug/help this issue.

Thank you for your time in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sgilliescommented, Jul 20, 2020

@plant99 I appreciate the offer of documentation help! If there is anything incorrect in the readme, we’d love to fix it. However, the team maintaining this package is occupied with other work and doesn’t have the time to review major changes.

1reaction
plant99commented, Jul 18, 2020

Thank you @sgillies , what you described solved this issue.

Do you also feel that Python implementation’s documentation has to be improved a bit in README.md? Please let me know if I can help with a PR.

Have a nice day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

rio-color/operations.py at master - GitHub
Contribute to mapbox/rio-color development by creating an account on GitHub. ... Setting gamma (:math:`\gamma`) to be less than 1.0 darkens the image and....
Read more >
Rescale, resize, and downscale — skimage v0.19.2 docs
Rescale operation resizes an image by a given scaling factor. The scaling factor can either be a single floating point value, or multiple...
Read more >
Optimized raster color corrections with rio-color | by Mapbox
Last week we launched rio-color — an open source Rasterio plugin for color adjustment of geospatial rasters. We're using it as part of...
Read more >
recolorize: Color-Based Image Segmentation
Accomplished by shrinking and then grow- ing a pixset. Value. The resulting pixset after applying the specified morphological operation.
Read more >
Image Resizing Techniques - NSHipster
UIKit, Core Graphics, and Image I/O all perform well for scaling operations on most images. If you had to choose one (on iOS,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found