Skip to content

Instantly share code, notes, and snippets.

@godber
Last active November 15, 2018 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godber/97d1a75bb52bd8942a4db1bd4efa3c3a to your computer and use it in GitHub Desktop.
Save godber/97d1a75bb52bd8942a4db1bd4efa3c3a to your computer and use it in GitHub Desktop.
3D Histogram of an image in Python using VisPy
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''Generates 3D Histogram of Wallaby image and renders to screen using vispy
Requires:
vispy
scipy
numpy
Related URLs:
http://vispy.org/installation.html
https://github.com/vispy/vispy/blob/master/examples/basics/scene/volume.py
http://api.vispy.org/en/latest/scene.html#vispy.scene.visuals.Volume
'''
from urllib.request import urlopen
import numpy as np
from scipy.misc import imread
from vispy import scene
from vispy import app
from vispy.io import load_data_file, read_png
# Create the all zero 3D Histogram we will use to store the color information
tristogram = np.zeros((256,256,256), dtype=np.uint8)
url = "https://raw.githubusercontent.com/desertpy/presentations/master/exploring-numpy-godber/wallaby_746_600x450.jpg"
with urlopen(url) as file:
img = imread(file, mode='RGB')
for h in range(600):
for w in range(450):
(r, g, b) = img[h, w, :]
tristogram[r, g, b] += 1
canvas = scene.SceneCanvas(show=True)
view = canvas.central_widget.add_view()
volume = scene.visuals.Volume(tristogram, parent=view.scene, emulate_texture=False)
view.camera = scene.cameras.TurntableCamera(parent=view.scene)
if __name__ == '__main__':
app.run()

Installation Instructions

Using a Python Virtual Environment

Install virtualenvwrapper as instructed here if you haven't done so already. Then follow the instructions below:

# First, create a python virtualenv with python3
mkvirtualenv -p `which python3` vispy
# Install dependencies
pip install -r requirements.txt
# Run program
python ./3dhist.py
numpy==1.14.4
Pillow==5.1.0
PyOpenGL==3.1.0
PyQt5==5.10.1
scipy==1.1.0
sip==4.19.8
vispy==0.5.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment