def process(self, videofile, progress): progress(0, _("Extracting histogram")) video = hg.cvCreateFileCapture(str(videofile).encode(sys.getfilesystemencoding())) if not video: raise Exception("Could not open video file") histo = cv.cvCreateHist([256],cv.CV_HIST_ARRAY,[[0,256]], 1) frame = hg.cvQueryFrame(video) frame_gray = cv.cvCreateImage(cv.cvGetSize(frame), frame.depth, 1); hists = [] nbframes = 0 fps = hg.cvGetCaptureProperty(video, hg.CV_CAP_PROP_FPS) while frame : if not progress(hg.cvGetCaptureProperty(video, hg.CV_CAP_PROP_POS_AVI_RATIO)): break hg.cvConvertImage(frame,frame_gray) cv.cvCalcHist(frame_gray,histo,0,None) h = [cv.cvGetReal1D(histo.bins,i) for i in range(255) ] h = numpy.array(h,dtype='int32') hists.append(h) frame = hg.cvQueryFrame(video) nbframes += 1 hists = numpy.array(hists) return hists.reshape(nbframes, -1), fps
# compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val = cv.cvGetMinMaxHistValue (hist, None, None) # compute the scale factor if max_val > 0: scale = 255. / max_val else: scale = 0. # scale the histograms cv.cvConvertScale (hist.bins, hist.bins, scale, 0) # clear the histogram image cv.cvSetZero (histimg)
# compute the hsv version of the image cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS(hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit(hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect(hue, selection) mask_roi = cv.cvGetSubRect(mask, selection) # it's time to compute the histogram cv.cvCalcHist(hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue(hist) # compute the scale factor if max_val > 0: scale = 255. / max_val else: scale = 0. # scale the histograms cv.cvConvertScale(hist.bins, hist.bins, scale, 0) # clear the histogram image cv.cvSetZero(histimg)
# compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram #min_val, max_val = cv.cvGetMinMaxHistValue (hist, None, None) min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist) # compute the scale factor if max_val > 0: scale = 255. / max_val else: scale = 0. # scale the histograms cv.cvConvertScale (hist.bins, hist.bins, scale, 0) # clear the histogram image
if track_object: _vmin = vmin _vmax = vmax cv.cvInRangeS( hsv, cv.cvScalar( 0, smin,min(_vmin,_vmax),0), cv.cvScalar(180, 256, max(_vmin,_vmax),0), mask ); cv.cvSplit( hsv, hue, None, None, None) if track_object < 0: max_val = 0.0 subhue = cv.cvGetSubRect(hue, selection) submask = cv.cvGetSubRect(mask, selection) cv.cvCalcHist( subhue, hist, 0, submask ) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist) if (max_val): cv.cvConvertScale( hist.bins, hist.bins, 255.0 / max_val, 0) else: cv.cvConvertScale( hist.bins, hist.bins, 0.0, 0 ) track_window = selection track_object = 1 cv.cvCalcArrBackProject( hue, backproject, hist )
# compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist) # compute the scale factor if max_val > 0: scale = 255. / max_val else: scale = 0. # scale the histograms cv.cvConvertScale (hist.bins, hist.bins, scale, 0) # clear the histogram image cv.cvSetZero (histimg)
cv.CV_RGB(0, 255, 0), 2, 8, 0) if track_object: _vmin = vmin _vmax = vmax cv.cvInRangeS(hsv, cv.cvScalar(0, smin, min(_vmin, _vmax), 0), cv.cvScalar(180, 256, max(_vmin, _vmax), 0), mask) cv.cvSplit(hsv, hue, None, None, None) if track_object < 0: max_val = 0.0 subhue = cv.cvGetSubRect(hue, selection) submask = cv.cvGetSubRect(mask, selection) cv.cvCalcHist(subhue, hist, 0, submask) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue( hist) if (max_val): cv.cvConvertScale(hist.bins, hist.bins, 255.0 / max_val, 0) else: cv.cvConvertScale(hist.bins, hist.bins, 0.0, 0) track_window = selection track_object = 1 cv.cvCalcArrBackProject(hue, backproject, hist)
cv.cvZero(thresh_mask) highgui.cvShowImage("Input", img) # 5x5 Gaussian Blur cv.cvSmooth(img, img, cv.CV_GAUSSIAN, 5, 5) # convert to HSV cv.cvCvtColor(img, img, cv.CV_BGR2HSV) # threshold bad values cv.cvInRangeS(img, hsv_min, hsv_max, thresh_mask) cv.cvAnd(thresh_mask, mask_bw, thresh_mask) # Hue(0,180), Saturation(0,255), Value(0,255) cv.cvSplit(img, img_h, img_s, img_v, 0) # calculate histogram cv.cvCalcHist(img_h, h_hue, 0, thresh_mask) cv.cvCalcHist(img_s, h_sat, 0, thresh_mask) cv.cvCalcHist(img_v, h_val, 0, thresh_mask) # Don't normalize, use total mask pixels to calculate relative importance # cv.cvNormalizeHist(h_hue, 180) # cv.cvNormalizeHist(h_sat, 255) # cv.cvNormalizeHist(h_val, 255) # minv,maxv,minp,maxp = cv.cvMinMaxLoc(img_h) # print minv,maxv cv.cvZero(hist_hue_img) # hue_min,hue_max,min_loc,max_loc = cv.cvGetMinMaxHistValue(h_hue) for h in xrange(h_bins): hue = cv.cvGetReal1D(h_hue.bins, h) color = hsv2rgb(h * h_limit / h_bins)