7 from cv_bridge
import CvBridge
8 from distutils.version
import LooseVersion
9 from dynamic_reconfigure.server
import Server
13 from matplotlib
import pyplot
as plt
14 from matplotlib
import gridspec
16 from jsk_topic_tools
import ConnectionBasedTransport
18 from sensor_msgs.msg
import Image
19 from jsk_pcl_ros.cfg
import ColorHistogramVisualizerConfig
as Config
29 super(ColorHistogramVisualizer, self).
__init__()
37 self.
pub_image = self.advertise(
"~output", Image, queue_size=1)
52 self.sub_histogram.unregister()
53 self.sub_histogram_array.unregister()
56 hsv_map = np.zeros((IMG_HEIGHT, IMG_WIDTH, 3), np.uint8)
57 h, s = np.indices(hsv_map.shape[:2])
58 hsv_map[:, :, 0] = h / float(IMG_HEIGHT) * 180.0
59 hsv_map[:, :, 1] = 125.0
60 hsv_map[:, :, 2] = s / float(IMG_WIDTH) * 256.0
61 hsv_map = cv2.cvtColor(hsv_map, cv2.COLOR_HLS2BGR)
67 elif self.
histogram_policy == Config.ColorHistogramVisualizer_HUE_AND_SATURATION:
70 rospy.logerr(
"Invalid histogram policy")
73 pub_msg = self.cv_bridge.cv2_to_imgmsg(img,
"bgr8")
74 self.pub_image.publish(pub_msg)
75 except Exception
as e:
76 rospy.logerr(
"Failed to convert image: %s" % str(e))
82 rospy.logerr(
"histogram index Out-of-index")
87 w, h = fig.canvas.get_width_height()
88 img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
91 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
95 gs = gridspec.GridSpec(1, 2, width_ratios=[4, 1])
96 if LooseVersion(matplotlib.__version__).version[0] > 1:
97 plt.subplot(gs[0], facecolor=
'silver')
99 plt.subplot(gs[0], axisbg=
'silver')
100 bin_size = len(hist) - 2
101 bin_step = 360.0 / bin_size
102 x = np.arange(360.0, step=bin_step)
103 bars = plt.bar(x, hist[:-2], width=bin_step)
104 cs = np.arange(0.0, 1.0, 1.0 / bin_size)
105 for c, b
in zip(cs, bars):
108 plt.ylim(ymin=0.0, ymax=1.0)
109 ymin, ymax = plt.ylim()
110 if LooseVersion(matplotlib.__version__).version[0] > 1:
111 plt.subplot(gs[1], facecolor=
'silver')
113 plt.subplot(gs[1], axisbg=
'silver')
114 bars = plt.bar(range(2), hist[-2:], label=[
"white",
"black"],
115 width=1.0, linewidth=2.0)
116 bars[0].set_facecolor((1.0, 1.0, 1.0, 1.0))
117 bars[1].set_facecolor((0.0, 0.0, 0.0, 1.0))
123 bin_step = 256.0 / bin_size
124 x = np.arange(256.0, step=bin_step)
125 plt.bar(x, hist, width=bin_step)
130 bin_size = int(math.sqrt(len(hist) - 2))
131 white, black = hist[-2], hist[-1]
132 hist = np.array(hist[:-2]).reshape(bin_size, bin_size).T
133 rospy.loginfo(
"white: %f, black: %f" % (white, black))
135 hist = hist[:, :, np.newaxis]
136 hist = cv2.resize(hist, (IMG_WIDTH, IMG_HEIGHT))
137 hist = hist[:, :, np.newaxis]
139 img = img.astype(np.uint8)
142 if __name__ ==
'__main__':
143 rospy.init_node(
"color_histogram_visualizer")
def image_from_plot(self)
def plot_hist_saturation(self, hist)
def plot_hist_hs(self, hist)
def get_hsv_color_map_2d(self)
def callback_array(self, msg)
def plot_hist_hue(self, hist)
def config_callback(self, config, level)