alejandrobll / py-sphviewer

Py-SPHViewer is a framework for rendering cosmological simulations in Python using the Smoothed Particle Hydrodynamics scheme.
https://alejandrobll.github.com/py-sphviewer
GNU General Public License v3.0
70 stars 29 forks source link

Request: Combine gas and stars #5

Closed christopherlovell closed 4 years ago

christopherlovell commented 8 years ago

It would be great to combine the gas and stars in the same render, is this possible? If not, any plans to include it?

alejandrobll commented 8 years ago

I don't think that it should be part of the "core" of sphviewer, but it could certainly be included as a tool. I have my own tools to do so, but I am not planning to release those soon, in part because the result depends strongly on the method used to blend the images, and I haven't had time to check different methods. Any suggestion in particular?

christopherlovell commented 8 years ago

Fair enough. I haven't put too much thought in to any particular method, bit naive with regards to what's possible. Would simply pasting the stars as point sources on the rendered gas work, with brightness as a function of distance from the camera? Or do you have to include the interaction of the stars and gas?

alejandrobll commented 8 years ago

Let me just think in a good way (with this I mean general and simple) to blend the images. The basic idea would consist in making two images, one of the gas and the other of the stars. The routine should ask for the two images and return a RGB (and eventually alpha) channels of the final image. I would be able to write a small routine to do so, but I need to find some time...

christopherlovell commented 8 years ago

Sounds like a good approach. I have time in the near future to take a look, will let you know how I get on

alejandrobll commented 8 years ago

Please check the new Blend.py tool that I've pushed to the repository. It is very primitive at this point, but it can probably illuminate your thoughts. Just do python Blend.py and see the example I've wrote. This is a disc inside a cube. The cube and the disc are rendered separately using the gist_heat and gist_stern colour maps respectively. Then, I blend the images using two different algorithms (Screen and Overlay). Note that there are plenty of different ways to do the same, and certain algorithms are better than others, depending on the input images, and the desired output of course. Perhaps one final warning. Before trying to blend the images, you should ensure that they are normalized in the range [0,1], without alpha channel at the moment, and also you could get better results if you use your own customized color map. I would like to keep developing it in my spare time, but of course that any suggestion or collaboration is always welcome.

figure_1

christopherlovell commented 8 years ago

I checked out the blend tool and it looks neat. I had an attempt myself and came out with something less sophisticated. It simply produces an RGBA array with the alpha value scaled to the RGB power, or cut above a certain limit. The results, on first impressions, don't look as good as the blend tool. However, the approach could still be of value for introducing RGBA arrays in to the blend pipeline. It basically allows you to skip the following step in the blend process,

image1 = cm.gist_heat(get_normalized_image(qv1.get_image()))[:,:,0:3]

and just call my rgba_from_array method with a given colourmap and normalisation. See here.

rgba_tools _test

alejandrobll commented 8 years ago

Thanks, nice approach! I liked the way you managed to merge the images, but I think that the final image should be a unique RGB instead of two with different alpha values. Two reasons for that. 1) What if you want to save the image without going though imshow? for example, using plt.imsave? In practice this is a good feature as it does speed up the process of making movies a lot. 2) It would be desirable to preserve the colour of each individual image to some extent, which can be done with the Screen algorithm that I use. That's the main reason why I prefer to say that the process consist in "blending" the images instead of stacking them. I would definitely add a "rgba_from_array" method to the current Blend tool. Would you like to add it and do a pull request? Otherwise, don't worry, I would be able to do so. In addition, I think that it would be nice to have a feature to define a custom colour map easily, probably setting up the minimum and maximum hue value for each image? I have already a tool to get RGB from HSV, and I could work on it later.

alejandrobll commented 8 years ago

Also, I think that you could also add your own methods as a possibility as well, among the current Screen and Overlay, and many others that can be added. I think that the idea of the Blend tool is to have as many methods as possible to get different results.

christopherlovell commented 8 years ago

I've got a bit of time at the end of the week, will add an rgba method then, keep an eye out for a pull request!

christopherlovell commented 8 years ago

Just made a pull request #6

Rather than creating a dedicated RGBA method I've just updated the Blend methods to accept RGBA arrays. Creating an RGBA is tied up with what colourmaps you wish to choose so it gets quite messy.

alejandrobll commented 8 years ago

Thanks, I'll work a little bit more on this to check whether the images are RGB or not, and in case they aren't, apply a default colour scheme to each one. I think this would be a nice feature for lazy users like me...