Matplotlib (그레이 스케일)로 표시하려는 비디오 스트림에서 오는 일련의 이미지가 있습니다. 왠지 나는 그들을 색으로 완벽하게 표시 할 수는 있지만 회색 음영으로 변환 할 때는 그렇지 않습니다.matplotlib을 사용하여 그레이 스케일 이미지 업데이트
나는 많은 이미지를 가지고 있기 때문에 훨씬 더 빠르기 때문에 imshow(image)
대신 set_data(image)
을 사용하고 있습니다. 컬러 이미지는이 두 명령 모두에서 제대로 작동하지만 그레이 스케일은 imshow()
에서만 작동합니다. set_data()
을 사용하면 어떤 데이터를 전송했는지에 관계없이 흑백 이미지 만 표시됩니다.
여기에 무슨 일이 일어날 지 모릅니다.
내 코드는 참조 용입니다. 중요한 점을 강조하여 중요한 부분을 강조했지만 다른 문제가 발생했을 때를 대비하여 다른 모든 코드를 포함했습니다. 나머지 코드는 기본적으로 카메라의 이미지로 채워지는 대기열을 설정하고이를 가져올 때 표시하려고합니다.
import matplotlib.pyplot as plt
from matplotlib import cm
from cv_bridge import CvBridge, CvBridgeError
import cv
from collections import deque
import rospy
from sensor_msgs.msg import Image
import numpy as np
class CameraViewer():
def __init__(self, root='navbot'):
self.root = root
self.im_data = deque()
self.bridge = CvBridge() # For converting ROS images into a readable format
self.im_fig = plt.figure(1)
self.im_ax = self.im_fig.add_subplot(111)
self.im_ax.set_title("DVS Image")
self.im_im = self.im_ax.imshow(np.zeros((256, 256),dtype='uint8'), cmap=plt.cm.gray) # Blank starting image
#self.im_im = self.im_ax.imshow(np.zeros((256, 256),dtype='float32'), cmap=plt.cm.gray) # Tried a different format, also didn't work
self.im_fig.show()
self.im_im.axes.figure.canvas.draw()
def im_callback(self, data):
cv_im = self.bridge.imgmsg_to_cv(data, "mono8") # Convert Image from ROS Message to greyscale CV Image
im = np.asarray(cv_im) # Convert from CV image to numpy array
#im = np.asarray(cv_im, dtype='float32')/256 # Tried a different format, also didn't work
self.im_data.append(im)
def run(self):
rospy.init_node('camera_viewer', anonymous=True)
sub_im = rospy.Subscriber(self.root + '/camera/image', Image, self.im_callback)
while not rospy.is_shutdown():
if self.im_data:
im = self.im_data.popleft()
#######################################################
# The following code is supposed to display the image:
#######################################################
self.im_im.set_cmap('gray') # This doesn't seem to do anything
self.im_im.set_data(im) # This won't show greyscale images
#self.im_ax.imshow(im, cmap=plt.cm.gray) # If I use this, the code runs unbearably slow
self.im_im.axes.figure.canvas.draw()
def main():
viewer = CameraViewer(root='navbot')
viewer.run()
if __name__ == '__main__':
main()
감사합니다! 그 완벽하게 일했다 :) – Brent