def __init__(self, **traits): super(HistDemo, self).__init__(**traits) img = cv.imread("lena.jpg") gray_img = cv.Mat() cv.cvtColor(img, gray_img, cv.CV_BGR2GRAY) self.img = gray_img self.img2 = self.img.clone() result = cv.MatND() r = cv.vector_float32([0, 256]) ranges = cv.vector_vector_float32([r, r]) cv.calcHist(cv.vector_Mat([self.img]), channels=cv.vector_int([0, 1]), mask=cv.Mat(), hist=result, histSize=cv.vector_int([256]), ranges=ranges) data = ArrayPlotData(x=np.arange(0, len(result[:])), y=result[:]) self.plot = Plot(data, padding=10) line = self.plot.plot(("x", "y"))[0] self.select_tool = RangeSelection(line, left_button_selects=True) line.tools.append(self.select_tool) self.select_tool.on_trait_change(self._selection_changed, "selection") line.overlays.append(RangeSelectionOverlay(component=line)) cv.imshow("Hist Demo", self.img) self.timer = Timer(50, self.on_timer)
# -*- coding: utf-8 -*- import pyopencv as cv import numpy as np img = cv.imread("fruits_section.jpg") img_hsv = cv.Mat() cv.cvtColor(img, img_hsv, cv.CV_BGR2HSV) channels = cv.vector_int([0, 1]) result = cv.MatND() r = cv.vector_float32([0, 256]) ranges = cv.vector_vector_float32([r, r]) cv.calcHist(cv.vector_Mat([img_hsv]), channels, cv.Mat(), result, cv.vector_int([40, 40]), ranges) result[:] /= np.max(result[:]) / 255 2 ### img2 = cv.imread("fruits.jpg") img_hsv2 = cv.Mat() cv.cvtColor(img2, img_hsv2, cv.CV_BGR2HSV) img_bp = cv.Mat() cv.calcBackProject(cv.vector_Mat([img_hsv2]), channels=channels, hist=result, backProject=img_bp, ranges=ranges) 3 ###