使用vispy以灰度显示图像

2024-06-02 21:41:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个空间光调制器(SLM),它被连接成第二个蒙比特器。SLM具有tzo接收8位灰度图像。 我目前正在与vispy合作,在SLM上显示图像,但如果显示正确的话,我不支持shore。 有没有可能用vispy在灰度上显示图像?在

我用这个代码显示图像

import sys
from vispy import scene
from vispy import app
import numpy as np

canvas = scene.SceneCanvas(keys='interactive')
canvas.size = 800, 600
canvas.show()

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Create the image
img_data = *my image*
image = scene.visuals.Image(img_data, parent=view.scene)

# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)

if __name__ == '__main__' and sys.flags.interactive == 0:
    app.run()

来自http://vispy.readthedocs.io/en/stable/examples/basics/scene/image.html

谢谢你的帮助,很抱歉英语不好


Tags: thefrom图像imageimportviewappsys
1条回答
网友
1楼 · 发布于 2024-06-02 21:41:38

您可以将图片从RGB转换为灰色(请参见this post),然后使用“grays”颜色映射。在

import sys
from vispy import scene
from vispy import app
import numpy as np
from vispy.io import load_data_file, read_png

canvas = scene.SceneCanvas(keys='interactive')
canvas.size = 800, 600
canvas.show()

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Define a function to tranform a picture to gray
def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

# Load the image
img_data = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))

# Apply transformation
img_data = rgb2gray(img_data)

# Image visual
image = scene.visuals.Image(img_data, cmap='grays', parent=view.scene)

# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
view.camera.flip = (0, 1, 0)

if __name__ == '__main__' and sys.flags.interactive == 0:
    app.run()

相关问题 更多 >