def test_npz_2d(self): ftmp = NamedTemporaryFile(suffix='.npz', delete=False) try: ftmp.close() h = Histogram(3, [0, 3], 4, [0, 4]) h.data[:] = [[-3, 0, 5, 3], [-2, 0, 4, 2], [-1, 0, 3, 1024]] h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.axes[0].label = 'x (cm)' h.axes[1].label = 'y (cm)' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.label = 'counts' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.title = 'title' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) finally: os.remove(ftmp.name)
def test_root_2d(self): ftmp = NamedTemporaryFile(suffix='.root', delete=False) try: ftmp.close() h = Histogram(3, [0, 3], 4, [0, 4]) h.data[:] = [[-3, 0, 5, 3], [-2, 0, 4, 2], [-1, 0, 3, 1024]] h.uncert = np.sqrt(np.abs( h.data)) # ROOT will always return uncert h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.axes[0].label = 'x (cm)' h.axes[1].label = 'y (cm)' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.label = 'counts' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.title = 'title' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) finally: os.remove(ftmp.name)
def test_hist_1d(self): ftmp = NamedTemporaryFile(suffix='.h5', delete=False) try: ftmp.close() h = Histogram(3, [0, 3]) h.data[:] = [-3, 0, 5] h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.axes[0].label = 'x (cm)' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) #print(type(h.axes[0].label), h.axes[0].label) #print(type(htmp.axes[0].label), htmp.axes[0].label) self.assertTrue(h.isidentical(htmp)) h.label = 'counts' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.title = 'title' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) h.uncert = [2, 3, 4] h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) finally: os.remove(ftmp.name)
def load_hue_histograms(self): for h in self.known_histograms.keys(): hist = Histogram() hist.load(self.known_histograms[h][0]) self.known_histograms[h][1] = hist for h in self.known_histograms: print h print self.known_histograms[h][1]
def test_hdf5(self): try: import h5py h = self.h.clone() filename = 'h.hdf5' h.save(filename) hh = Histogram.load(filename) assert h.isidentical(hh) except ImportError: pass
class SkinDetector: def __init__(self, topic=DEF_TOPIC_NAME, hist_file=DEF_HISTOGRAM_FILE): rospy.init_node("skin_detection", anonymous=True) rospy.Subscriber(topic, Image, self.rgb_cb) cv2.namedWindow("wnd_orig") cv2.namedWindow("wnd_prob") cv2.namedWindow("wnd_skin") self.skin_threshold = 127 cv2.createTrackbar("track_skin", "wnd_skin", self.skin_threshold, 255, self.on_skin_track) rospy.on_shutdown(self.on_shutdown) self.hist = Histogram() self.hist.load(hist_file) def on_skin_track(self, pos, *argv): self.skin_threshold = pos def find_skin(self, img): img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) mask = hsv_filter_mask(img_hsv) back_proj = calc_back_proj(img_hsv, self.hist.hist, hsv=True) back_proj &= mask skin_bin = cv2.threshold(back_proj, self.skin_threshold, 255, cv2.THRESH_BINARY) return (back_proj, skin_bin[1]) def rgb_cb(self, msg): try: img = bridge.imgmsg_to_cv(msg, "bgr8") img = np.asarray(img) except CvBridgeError, e: print >> stderr, "Cannot convert from ROS msg to CV image:", e (img_prob, img_skin) = self.find_skin(img) cv2.imshow("wnd_orig", img) cv2.imshow("wnd_prob", img_prob) cv2.imshow("wnd_skin", img_skin) ch = cv2.waitKey(3) if ch == 27: rospy.signal_shutdown("Quit") elif ch == ord(" "): cv2.imwrite("img_prob.png", img_prob)
def test_unicode(self): ftmp = NamedTemporaryFile(suffix='.npz', delete=False) try: h = Histogram(3, [0, 3]) h.data[:] = [-3, 0, 5] h.title = 'χ-squared' h.label = 'αβγ' h.axes[0].label = 'θ' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) finally: os.remove(ftmp.name)
def test_unicode(self): ftmp = NamedTemporaryFile(suffix='.root', delete=False) try: ftmp.close() h = Histogram(3, [0, 3]) h.data[:] = [-3, 0, 5] h.uncert = np.sqrt(np.abs( h.data)) # ROOT will always return uncert h.title = 'χ-squared' h.label = 'αβγ' h.axes[0].label = 'θ' h.save(ftmp.name) htmp = Histogram.load(ftmp.name) self.assertTrue(h.isidentical(htmp)) finally: os.remove(ftmp.name)
def test_root(self): # For CERN/ROOT, we resorted to converting everything # into float64's so histograms are not typically # "identical" but they should be "close" try: import ROOT h = self.h.clone() filename = 'h.root' self.h.save(filename) hh = Histogram.load(filename) assert np.allclose(h.data,hh.data) assert np.allclose(h.uncert,hh.uncert) assert h.label == hh.label assert h.title == hh.title for a,aa in zip(h.axes,hh.axes): assert np.allclose(a.edges,aa.edges) assert a.label == aa.label except ImportError: pass
def _to_unicode(s): if not isinstance(s,unicode): return unicode(s,'utf-8') else: return s h.title = _to_unicode(h.title) h.label = _to_unicode(h.label) for ax in h.axes: ax.label = _to_unicode(ax.label) infiles = ['h2','h3','h2.hdf5','h3.hdf5'] for infile in infiles: print(infile) hh = Histogram.load(infile) assert h.isidentical(hh) # For CERN/ROOT, we resorted to converting everything # into float64's so histograms are not typically # "identical" but they should be "close" infiles = ['h2.root','h3.root'] for infile in infiles: print(infile) hh = Histogram.load(infile) assert np.allclose(h.data,hh.data) assert np.allclose(h.uncert,hh.uncert) assert h.label == hh.label assert h.title == hh.title
def test_npz(self): h = self.h.clone() filename = 'h' h.save(filename) hh = Histogram.load(filename) assert h.isidentical(hh)