Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to rescale the image after rio-color operations? #61

Closed
plant99 opened this issue Jul 17, 2020 · 6 comments
Closed

How to rescale the image after rio-color operations? #61

plant99 opened this issue Jul 17, 2020 · 6 comments

Comments

@plant99
Copy link

plant99 commented Jul 17, 2020

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!

@plant99
Copy link
Author

plant99 commented Jul 17, 2020

Screen Shot 2020-07-17 at 1 01 50 AM

Screen Shot 2020-07-16 at 10 25 24 AM

Above are images generated by Python API's usage, and command line, in the same order.

@sgillies
Copy link
Contributor

@plant99 It looks like you've implemented your own scaling to the range of 0-1, yes? the rio-color command does this https://github.com/mapbox/rio-color/blob/master/rio_color/utils.py#L15-L20 and it might be worth a try.

@plant99
Copy link
Author

plant99 commented 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!

@sgillies
Copy link
Contributor

@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.

@plant99
Copy link
Author

plant99 commented Jul 20, 2020

@sgillies README.md is sufficient, and I understand that major changes are hard for the team to review at the moment.

Since I'm anyway working with this tool, if you don't mind an open PR, may I still make it?

@sgillies
Copy link
Contributor

@plant99 yes, go ahead, and thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants