def renderIndividualHist(self, painting): height = 500 width = 500 (r, g, b) = cv.GetDims(painting.data.bins) (_, tallest, _, _) = cv.GetMinMaxHistValue(painting.data) scale = height / tallest if scale > 1: scale = 1 histImage = cv.CreateImage((width, height), 8, 3) prev = -1 cv.Rectangle(histImage, (0, 0), (width, height), cv.CV_RGB(0, 0, 0), cv.CV_FILLED) step = width / (r * g * b) cur = 0 for (i, j, k) in iter3D(range(r), range(g), range(b)): intensity = cv.QueryHistValue_3D(painting.data, i, j, k) color = cv.CV_RGB(int((255 / r) * i), int((255 / g) * j), int((255 / b) * k)) x1 = cur cur += step x2 = x1 + step cv.Rectangle(histImage, (int(x1), height), (int(x2), height - int(intensity * scale)), color, cv.CV_FILLED) windowTitle = '{0} ({1})'.format(painting.title, painting.year) cv.NamedWindow(windowTitle) cv.ShowImage(windowTitle, histImage) cv.WaitKey(0)
def hue_histogram_as_image(hist): histimg_hsv = cv.CreateImage((640, 480), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = int(cv.GetDims(mybins)[0]) for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) return histimg
def setup_camshift(roi_selection, img): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) # backproject_mode = False histimg_hsv = cv.CreateImage((320, 200), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w, h = cv.GetSize(histimg_hsv) hdims = cv.GetDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1) cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1) histimg = cv.CreateImage((320, 200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) # making_selection # print roi_selection[0] # print roi_selection[1] point1 = roi_selection[0] point2 = roi_selection[1] xmin = point1[0] ymin = point1[1] xmax = point2[0] ymax = point2[1] widthx = xmax - xmin heighty = ymax - ymin selection = (xmin, ymin, widthx, heighty) return selection, hist
def calc_1dhisto(inframe, nbin=256, scale=2, histh=200, hist=None, histimg=None): """ Calculate 1D intensity histogram of a iplimage, and return the histogram (as cv2.cv.cvhistogram) and an image representing this histogram (as 8bit unsigned iplimage) Use **hist** and **histimg** if they are provided, otherwise create them from scratch. To re-use the allocated memory, simply pass the output as input for input **hist** and **histimg**. Histogram bar height is calculated as follows: bin_height = int( bin_count*1.0 / (npix/nbin) * 0.2 * histh ) where bin_height is in pixels, bin_count is the number of pixels in this bin, npix the total number of pixels in the image, nbin the total bins, such that (npix/nbin) is the average bin count. 0.2 is a factor that sets the average bin height to 20% and histh scales the bin normalized bin height to pixels. @param [in] inframe Input frame, as iplimage @param [in] nbin Number of intensity bins @param [in] scale Histogram image bar width in pixels @param [in] histh Histogram image height in pixels @param [in] hist Previously allocated cv2.cv.cvhistogram to use @param [in] histimg Previously allocated iplimage to use @return Tuple of (histogram, histogram image) """ if (inframe.depth == cv.IPL_DEPTH_32F): hranges = [[0, 1]] elif (inframe.depth in [ cv.IPL_DEPTH_8U, cv.IPL_DEPTH_8S, cv.IPL_DEPTH_16U, cv.IPL_DEPTH_16S, cv.IPL_DEPTH_32S ]): hranges = None else: raise ValueError("Unsupported datatype for histogram ('%s')" % (str(inframe.depth))) if (not hist): hist = cv.CreateHist([nbin], cv.CV_HIST_ARRAY, ranges=[[0, 1]], uniform=1) if (not histimg): histimg = cv.CreateImage((nbin * scale, histh), cv.IPL_DEPTH_8U, 1) cv.CalcHist([cv.GetImage(inframe)], hist) #hmin, hmax, __, __ = cv.GetMinMaxHistValue(hist) # White noise histogram height should be be 0.2 npix = np.product(cv.GetDims(inframe)) histogram = [ cv.QueryHistValue_1D(hist, i) * 1.0 / (npix / nbin) * 0.2 * histh for i in range(nbin) ] histimg = plot_1dhisto(histogram, scale=scale, histh=histh, histimg=histimg) return hist, histimg
CAM_CFG = { 'camidx': 0, 'frameidx': 0, 'handler': None, 'buf': [None] * 10, 'roi': roi } # Initialize camera handler CAM_CFG['handler'] = cv.CaptureFromCAM(CAM_CFG['camidx']) rawframe = cv.QueryFrame(CAM_CFG['handler']) frame = cv.GetSubRect(rawframe, roi) # Make fake calibration frames from data darkarr = (np.random.random(cv.GetDims(frame)) * 10.0 / 256).astype(np.float32) darkframe = array2cv(darkarr) lastframe = cv.CloneImage(darkframe) camframe = cv.CloneImage(darkframe) diffframe = cv.CloneImage(darkframe) # Make real flat field print "Taking 100 flats..." frame = cv.GetSubRect(cv.QueryFrame(CAM_CFG['handler']), CAM_CFG['roi']) cv.ConvertScale(frame, camframe, scale=1.0 / 256) flatframe = cv.CloneImage(camframe) for i in xrange(9): print ".", frame = cv.GetSubRect(cv.QueryFrame(CAM_CFG['handler']), CAM_CFG['roi'])