def hs_histogram(src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S plane h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 to 180 h_ranges = [0, 180] # saturation varies from 0 to 255 s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / max_value) cv.Rectangle(hist_img, (h * scale, s * scale), ((h + 1) * scale - 1, (s + 1) * scale - 1), cv.RGB(intensity, intensity, intensity), cv.CV_FILLED) return hist_img
def use_image(self, im): self.kind = 'opencv' self.hist = cv.CreateHist([self.bins], cv.CV_HIST_ARRAY, [self.value_range], 1) cv.CalcHist([im], self.hist) self.min_value, self.max_value, _, _ = cv.GetMinMaxHistValue(self.hist) return self
def get_2d_hist(planes, x_bins, y_bins, x_range_max=255, y_range_max=255): x_ranges = [0, x_range_max] y_ranges = [0, y_range_max] ranges = [x_ranges, y_ranges] hist = cv.CreateHist([x_bins, y_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist(planes, hist) return hist
def calcHistogram(self, image): cv.CvtColor(image, self.tmp, cv.CV_BGR2HSV) cv.Split(self.tmp, self.B, self.G, self.R, None) channels = self.B, self.G, self.R hist_props = [{} for channel in channels] #Normalisation constant for histogram size size_norm = 255.0 / self.hist_size for chnum, ch in enumerate(channels): #, colour, Ypos in zip(channels, values, positions): cv.CalcHist([ch], self.hist, 0, None) hist_arr = self.smoothHistogram() diffs = np.diff(hist_arr, 1) hist_props[chnum]['plateaus'] = [] mins = [(0, 0)] for i, v in enumerate(diffs): if v < mins[-1][1]: mins.append((i, v)) if hist_arr[i] > 1 and diffs[i - 1] * diffs[i] <= 0: hist_props[chnum]['plateaus'].append(i * size_norm) hist_props[chnum]['mins'] = [(i * size_norm, v) for i, v in mins] #print "MINS:",mins # Calculate beyond the 100th percentile due to numerical instability ptile = range(0, 103, 1) total = sum(hist_arr) running_sum = 0 for i, v in enumerate(hist_arr): running_sum += v while running_sum >= total * ptile[0] / 100.0: hist_props[chnum][ptile[0]] = i * size_norm del ptile[0] # Make sure all percentiles are actually in the data structure: prev = 10 for p in ptile: if p in hist_props[chnum]: prev = p else: hist_props[chnum][p] = hist_props[chnum][prev] assert 90 in hist_props[chnum], "percentile calculation incomplete" post_peaks = mins[-1][0] for i in range(mins[-1][0], len(diffs)): if diffs[i - 1] * diffs[i] <= 0: post_peaks = i #print hist_arr[i:] break #print "POST:",post_peaks*size_norm hist_props[chnum]['post_peaks'] = post_peaks * size_norm #self.drawHistogram(tmp, chnum, hist_arr, plateaus) return hist_props
def otsu(image, min_value, max_value): hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 255]], 1) # Create a histogram variable cv.CalcHist([image], hist, 0, None) # Calculate the histogram threshold, variance = calc_threshold( hist, min_value, max_value) # Find the optimal threshold print threshold, variance return image, threshold, hist
def LoadHistogram(self, samples, channels, bins, ranges, factor): ch_numpy = [ samples[:, ch:(ch + 1)] for ch in channels ] ch_cvmat = map(cv.fromarray, ch_numpy) ch_image = map(cv.GetImage, ch_cvmat) histogram = cv.CreateHist(bins, cv.CV_HIST_ARRAY, ranges, True) cv.CalcHist(ch_image, histogram, False) cv.NormalizeHist(histogram, factor) return histogram
def make_histogram(imagefile): col = cv.LoadImageM(imagefile) gray = cv.CreateImage(cv.GetSize(col), cv.IPL_DEPTH_8U, 1) cv.CvtColor(col, gray, cv.CV_RGB2GRAY) hist = cv.CreateHist([NUM_BINS], cv.CV_HIST_ARRAY, [[0, 255]], 1) cv.CalcHist([gray], hist) cv.NormalizeHist(hist, 1.0) return hist
def otsu_get_threshold(src): ''' Find the optimal threshold value for a grey level image using Otsu's method. This is baised on Otsu's original paper published in IEEE Xplore: "A Threshold Selection Method from Gray-Level Histograms" ''' if src.nChannels != 1: raise ValueError("Image must have one channel.") # Compute Histogram hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 255]], 1) cv.CalcHist([src], hist) # Convert to Probability Histogram cv.NormalizeHist(hist, 1) overall_moment = 0 for t in xrange(256): overall_moment += t * cv.QueryHistValue_1D(hist, t) # Find the threshold t that gives the highest variance # Suffixes _b and _f mean background and foreground num_pixels = src.width * src.height weight_b = 0 moment_b = 0 highest_variance = 0 best_threshold = 0 for t in xrange(256): hist_value = cv.QueryHistValue_1D(hist, t) weight_b += hist_value weight_f = 1 - weight_b if weight_b == 0: continue if weight_f == 0: break moment_b += t * hist_value moment_f = overall_moment - moment_b mean_b = moment_b / weight_b mean_f = moment_f / weight_f variance_between = weight_b * weight_f * \ (mean_b - mean_f) ** 2 if variance_between >= highest_variance: highest_variance = variance_between best_threshold = t return best_threshold
def calc_hist(self): self.hist = cv.CreateHist([self.h_bins, self.s_bins], cv.CV_HIST_ARRAY, self.ranges, 1) hsv = cv.CreateImage(cv.GetSize(self.background_noise[0]), 8, 3) h_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) s_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) for i in xrange(len(self.background_noise)): cv.CvtColor(self.background_noise[i], hsv, cv.CV_BGR2HSV) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane]#, s_plane, v_plane] cv.CalcHist([cv.GetImage(i) for i in planes], self.hist, True, self.mask)
def grey_histogram(img, nBins=64): """ Returns a one dimension histogram for the given image The image is expected to have one channel, 8 bits depth nBins can be defined between 1 and 255 """ hist_size = [nBins] h_ranges = [0, 255] hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, [[0, 255]], 1) cv.CalcHist([img], hist) return hist
def hs_histogram(src, patch): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) cv.Split(hsv, h_plane_img, s_plane_img, None, None) cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None) #cv.Split(src, h_plane, s_plane, v_plane, None) planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane] h_bins = 30 s_bins = 32 v_bins = 30 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] v_ranges = [0, 255] ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3) back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1) cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist) # for h in range(h_bins): # for s in range(s_bins): # bin_val = cv.QueryHistValue_2D(hist, h, s) # intensity = cv.Round(bin_val * 255 / max_value) # cv.Rectangle(hist_img, # (h*scale, s*scale), # ((h+1)*scale - 1, (s+1)*scale - 1), # cv.RGB(intensity, intensity, intensity), # cv.CV_FILLED) return back_proj_img, hist
def drawHistogram(self, image): w = 0.5 n = 9 gauss1d = np.exp(-0.5 * w / n * np.array(range(-(n - 1), n, 2))**2) gauss1d /= sum(gauss1d) hist_size = 180 hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, [[0, 256]], 1) cv.CvtColor(image, self.HSV, cv.CV_BGR2HSV) cv.Split(self.HSV, self.B, self.G, self.R, None) channels = self.B, self.G, self.R _red = cv.Scalar(255, 0, 0, 0) _green = cv.Scalar(0, 255, 0, 0) _blue = cv.Scalar(0, 0, 255, 0) _white = cv.Scalar(255, 255, 255, 0) _trans = cv.Scalar(0, 0, 0, 255) values = _blue, _green, _red positions = (0, (self.Ihist.height + 10), 2 * self.Ihist.height + 20) for ch, colour, Y in zip(channels, values, positions): cv.CalcHist([ch], hist, 0, None) cv.Set(self.Ihist, _trans) bin_w = cv.Round(float(self.Ihist.width) / hist_size) # min_value, max_value, pmin, pmax = cv.GetMinMaxHistValue(hist) X = self.HSV.width - self.Ihist.width rect = (X, Y, self.Ihist.width, self.Ihist.height) hist_arr = [cv.GetReal1D(hist.bins, i) for i in range(hist_size)] hist_arr = np.convolve(gauss1d, hist_arr, 'same') cv.SetImageROI(image, rect) hist_arr *= self.Ihist.height / max(hist_arr) for i, v in enumerate(hist_arr): cv.Rectangle(self.Ihist, (i * bin_w, self.Ihist.height), ((i + 1) * bin_w, self.Ihist.height - round(v)), colour, -1, 8, 0) diffs = np.diff(hist_arr, 1) for i, v in enumerate(diffs): if v > 1 and diffs[i - 1] * diffs[i] <= 0: cv.Rectangle(self.Ihist, (i * bin_w, self.Ihist.height), ((i + 1) * bin_w, self.Ihist.height - round(hist_arr[i])), _white, -1, 8, 0) cv.AddWeighted(image, 1 - self.hist_visibility, self.Ihist, self.hist_visibility, 0.0, image) cv.ResetImageROI(image)
def cdf(channel): # -- find cum dist func for individual channel # -- find cumulative histogram and calculate hist to find # which bin (between 0,255) corresponds to what value # and store in refHist hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 256]], 1) cv.CalcHist([cv.GetImage(channel)], hist) refHist = [cv.QueryHistValue_1D(hist, x) for x in range(0, 256)] # -- calculate cdf cdf = [v / np.sum(refHist[:]) for v in refHist[:]] for x in range(1, 256): cdf[x] += cdf[x - 1] return cdf
def find_color_bounds(self, hue): """ retrieve hue boundaries for most prevailing colors of the image """ hSize = self.max_histogram_size hist = cv.CreateHist([hSize], cv.CV_HIST_SPARSE, [[0, 180]]) cv.CalcHist([hue], hist) bins = np.array([hist.bins[i] for i in range(hSize)]) start_pt = list(self.get_hist_edges(bins)) end_pt = start_pt[1:] end_pt.append(self.max_histogram_size) return zip(start_pt, end_pt)
def _compute_1ch_histogram(self, img_list): """ DESIGNED FOR INTERNAL USE ONLY CAREFUL, NO VERIFICATIONS PERFORMED """ dims = [self.nb_bins] all_ranges = [self.ranges] hist_list = [] for img in img_list: hist = cv.CreateHist(dims, cv.CV_HIST_ARRAY, all_ranges, uniform=1) cv.CalcHist([img], hist) hist_list.append(hist) return hist_list
def pitch_detect(intrinsics, dist_coeffs, dst0): capture = cv.CaptureFromCAM(0) src = cv.QueryFrame(capture) cv.SetImageROI(dst0, (0, 0, 640, 480)) cv.Undistort2(src, dst0, intrinsics, dist_coeffs) cv.SetImageROI(dst0, image_ROI) dst = GetImage(dst0) hsv = cv.CreateImage(size, 8, 3) CvtColor(dst, hsv, CV_RGB2HSV) cv.Split(hsv, hue, sat, val, None) hist = cv.CreateHist([32, 64], CV_HIST_ARRAY, [[0, 180], [0, 256]], 1) cv.CalcHist([hue, sat], hist, 0, None) values = cv.GetMinMaxHistValue(hist) tweak = values[3][0] return tweak
def mapper(key, value): # Read in the data and decode... imgbytes = np.fromstring(value,dtype='uint8') imarr = cv2.imdecode(imgbytes,cv2.CV_LOAD_IMAGE_COLOR) im = cv.fromarray(imarr) # Convert and split data to get hue... hsv = cv.CreateImage(cv.GetSize(im),8,3) hue = cv.CreateImage(cv.GetSize(im),8,1) cv.CvtColor(im,hsv,cv.CV_BGR2HSV) cv.Split(hsv, hue, None, None, None) # Calculate colour (hue) histogram... hue_bins = 180 hue_range = [0,180] # n.b. opencv hue range hist = cv.CreateHist([hue_bins], cv.CV_HIST_ARRAY, [hue_range], 1) cv.CalcHist([hue],hist,0,None) # Yield count of colour... for h in range(hue_bins): yield int(h),cv.QueryHistValue_1D(hist,h)
def getHist(self, hBins, sBins): # Extract the H and S planes h_plane = cv.CreateMat(self.src.height, self.src.width, cv.CV_8UC1) s_plane = cv.CreateMat(self.src.height, self.src.width, cv.CV_8UC1) cv.Split(self.src, h_plane, s_plane, None, None) planes = [h_plane, s_plane] hist_size = [hBins, sBins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] self.hist = cv.CreateHist([hBins, sBins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], self.hist) return self.hist
def RGBHist(im,r_bins=8,g_bins=8,b_bins=8,mask=None,normalize=True): ''' Compute the rgb saturation histogram of an image. (Based on OpenCV example code). @param im: the image @type im: pv.Image @param r_bins: the number of bins for hue. @type r_bins: int @param g_bins: the number of bins for hue. @type g_bins: int @param b_bins: the number of bins for hue. @type b_bins: int @param mask: an image containing a mask @type mask: cv.Image or np.array(dtype=np.bool) @return: an OpenCV histogram @rtype: pv.Histogram ''' w,h = im.size bgr = im.asOpenCV() # Extract the H and S planes b_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) g_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) r_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) cv.Split(bgr, b_plane, g_plane, r_plane, None) planes = [b_plane, g_plane, r_plane] # set the histogram size hist_size = [b_bins, g_bins, r_bins] # pixel value ranges b_ranges = [0, 255] g_ranges = [0, 255] r_ranges = [0, 255] ranges = [b_ranges, g_ranges, r_ranges] # Calculate the histogram hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1) if mask != None: mask = mask.asOpenCVBW() cv.CalcHist(planes, hist, mask=mask) return pv.Histogram(hist,HIST_RGB,b_bins,g_bins,r_bins)
def HSHist(im,h_bins=32,s_bins=32,mask=None,normalize=True): ''' Compute the hue saturation histogram of an image. (Based on OpenCV example code). @param im: the image @type im: pv.Image @param h_bins: the number of bins for hue. @type h_bins: int @param s_bins: the number of bins for saturation. @type s_bins: int @param mask: an image containing a mask @type mask: cv.Image or np.array(dtype=np.bool) @return: an OpenCV histogram @rtype: pv.Histogram ''' w,h = im.size hsv = im.asHSV() # Extract the H and S planes h_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) s_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] # set the histogram size hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] # Calculate the histogram hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1) if mask != None: mask = mask.asOpenCVBW() cv.CalcHist(planes, hist,mask=mask) return pv.Histogram(hist,HIST_HS,h_bins,s_bins,None)
def compute_histogram(src, h_bins=30, s_bins=32): #create images hsv = cv.CreateImage(cv.GetSize(src), 8, 3) hplane = cv.CreateImage(cv.GetSize(src), 8, 1) splane = cv.CreateImage(cv.GetSize(src), 8, 1) vplane = cv.CreateImage(cv.GetSize(src), 8, 1) planes = [hplane, splane] cv.CvtColor(src, hsv, cv.CV_BGR2HSV) cv.Split(hsv, hplane, splane, vplane, None) #compute histogram (why not use v_plane?) hist = cv.CreateHist((h_bins, s_bins), cv.CV_HIST_ARRAY, ranges=((0, 180), (0, 255)), uniform=True) cv.CalcHist(planes, hist) #compute histogram cv.NormalizeHist(hist, 1.0) #normalize hist return hist
def analyse(self, painting): data = super(SimpleRHOG, self).analyse(painting) segmented = numpy.ones((10, 10), numpy.float32) for i in range(1, 10): for j in range(1, 10): avg = 0 for x in range(1, int(data.rows / 10)): for y in range(1, int(data.cols / 10)): (val, _, _, _) = cv.Get2D(data, x * i, y * j) avg += val avg /= int(data.rows / 10) * int(data.cols / 10) segmented[i - 1][j - 1] = avg segs = cv.fromarray(segmented) grad_img = cv.CreateImage((segs.rows, segs.cols), cv.IPL_DEPTH_32F, segs.channels) cv.Convert(segs, grad_img) hist = cv.CreateHist([15], cv.CV_HIST_ARRAY, [[0, 2 * math.pi]]) cv.CalcHist([grad_img], hist) return hist
def calcHistogram(src, quantization=16): # Convert to HSV #src = cv.fromarray(_src) luv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, luv, cv.CV_RGB2Luv) # Extract the H and S planes size = cv.GetSize(src) L_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) U_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) V_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) cv.Split(luv, L_plane, U_plane, V_plane, None) planes = [L_plane, U_plane, V_plane] #print np.asarray(L_plane),np.asarray(U_plane),np.asarray(V_plane) #Define number of bins L_bins = quantization U_bins = quantization V_bins = quantization #Define histogram size hist_size = [L_bins, U_bins, V_bins] # L_ranges = [0, 255] U_ranges = [0, 255] V_ranges = [0, 255] ranges = [L_ranges, U_ranges, V_ranges] #Create histogram hist = cv.CreateHist([L_bins, U_bins, V_bins], cv.CV_HIST_ARRAY, ranges, 1) #Calc histogram cv.CalcHist([cv.GetImage(i) for i in planes], hist) #Normalize histogram cv.NormalizeHist(hist, 1.0) #Return histogram return hist
def histogram(self, src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) v_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) max_val = cv.GetMinMaxHistValue(hist)[3] max_hue_bin = max_val[0] max_sat_bin = max_val[1] # display this color in RGB h_interval = 6 s_interval = 8 hue = h_interval * max_hue_bin BGR_color = HSV_to_RGB(hue) cv.NamedWindow("About this color?", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("About this color?", 620, 530) color_swatch = cv.CreateImage((200, 140), 8, 3) cv.Set(color_swatch, BGR_color) cv.ShowImage("About this color?", color_swatch) return BGR_color, hue
def locate_object(self, frame): ''' Finds the object in the given frame based on information from previous frames. The object location as a tuple (x,y) within the given image is returned. If the object is not found, False is returned. Tracker.object_center will always contain the last known object location. ''' if not self._template: raise RuntimeError("The Tracker class can not be used after it is " "unpickled.") search_rect = clip_rectangle( ( self.object_center[0] - self.search_size[0] / 2, # x self.object_center[1] - self.search_size[1] / 2, # y self.search_size[0], # width self.search_size[1], # height ), frame.width, frame.height) search_image = self._preprocess(crop(frame, search_rect)) result = cv.CreateImage( (search_image.width - self._template.width + 1, search_image.height - self._template.height + 1), cv.IPL_DEPTH_32F, 1) cv.MatchTemplate(search_image, self._template, result, self.match_method) min_or_max = MATCH_METHOD_MIN_OR_MAX[self.match_method] minmaxloc = cv.MinMaxLoc(result) if abs(minmaxloc[1] - minmaxloc[0]) < 0.001: return False match_in_result = minmaxloc[min_or_max] # Change from result image coordinates to search region coordinates to # image coordinates match_in_search_region = ( match_in_result[0] + self._template.width / 2, match_in_result[1] + self._template.height / 2, ) object_center = ( match_in_search_region[0] + search_rect[0], match_in_search_region[1] + search_rect[1], ) object_center = ( int(in_range(0, object_center[0], frame.width - 1)), int(in_range(0, object_center[1], frame.height - 1)), ) # Determine if the max/min is significant. hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 255]], 1) cv.CalcHist([scale_32f_image(result)], hist) # XXX stddevs from mean should be calculated from either 0 or 255 # depending on min or max distance = abs(libvision.hist.num_stddev_from_mean(hist, 255)) if distance < self.min_z_score: object_found = False else: object_found = True self._update_template(search_image, match_in_search_region) self.object_center = object_center if self.debug: result_8bit = scale_32f_image(result) if object_found: cv.Circle(result_8bit, match_in_result, 5, (0, 255, 0)) cv.Circle(search_image, match_in_search_region, 5, (0, 255, 0)) hist_image = libvision.hist.histogram_image(hist) cv.ShowImage("match", result_8bit) cv.ShowImage("template", scale_32f_image(self._template)) cv.ShowImage("search region", scale_32f_image(search_image)) cv.ShowImage("Histogram", hist_image) # Update Template if object_found: return self.object_center else: return False
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
def back_project_hs(src, patch): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3) cv.CvtColor(patch, hsv_patch, cv.CV_BGR2HSV) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) cv.Split(hsv, h_plane_img, s_plane_img, None, None) cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None) #cv.Split(src, h_plane, s_plane, v_plane, None) planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane] # planes = [s_plane_patch]#, s_plane, v_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] s_ranges = [0, 255] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges] #ranges = [s_ranges]#, s_ranges, v_ranges] scale = 1 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) #hist = cv.CreateHist([s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist) #cv.NormalizeHist(hist, 20*250.0) print "min hist value is :", min_value print "max hist value is :", max_value back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1) #cv.NormalizeHist(hist, 2000) cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist) back_modified = cv.CreateImage(cv.GetSize(src), 8, 1) back_modified2 = cv.CreateImage(cv.GetSize(src), 8, 1) # cv.Dilate(back_proj_img, back_proj_img) # cv.Erode(back_proj_img, back_proj_img) #cv.Smooth(back_proj_img, back_modified) #cv.AdaptiveThreshold(back_proj_img, back_modified, 255, adaptive_method=cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C) #cv.Threshold(back_proj_img, back_modified, 250, 255, cv.CV_THRESH_BINARY) #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 3) #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 1) # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1) #cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2) cv.MorphologyEx(back_proj_img,back_modified, None, None, cv.CV_MOP_OPEN, 1) cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 2) cv.Threshold(back_modified, back_modified, 250, 255, cv.CV_THRESH_BINARY) # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1) # cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2) #cv.FloodFill(back_modified, (320, 240), cv.Scalar(255), cv.Scalar(30), cv.Scalar(30), flags=8) # for i in xrange (10): # cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_OPEN, 3) # cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 1) #cv.SubRS(back_modified, 255, back_modified) # cv.CalcBackProject([s_plane_img], back_proj_img, hist) # cv.Scale(back_proj_img, back_proj_img, 30000) cv.ShowImage("back_projection", back_proj_img) cv.ShowImage("back_modified", back_modified) cv.ShowImage("back_modified2", back_modified2) cv.WaitKey(0) #return back_proj_img, hist return back_modified, hist
h_plane = cv.CreateImage(cv.GetSize(src), 8, 1) s_plane = cv.CreateImage(cv.GetSize(src), 8, 1) v_plane = cv.CreateImage(cv.GetSize(src), 8, 1) planes = [h_plane, s_plane] cv.CvtPixToPlane(hsv, h_plane, s_plane, v_plane, None) h_bins = 30 s_bins = 32 # Build the histogram and compute its contents. hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, [(0, 180), (0, 255)], 1) cv.CalcHist(planes, hist, 0, None) cv.NormalizeHist(hist, 1.0) # Create an image to use to visualize our histogram. scale = 10 hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3) cv.Zero(hist_img) max_value = 0 (minvalue, maxvalue, minidx, maxidx) = cv.GetMinMaxHistValue(hist) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / maxvalue)
def detectGripperWithCollectedData( self, detectedGripperAngleBuffer, detectedOpticalFlowBufferX, detectedOpticalFlowBufferY, imageRGB ): t1 = time.time() maxLag = int( self.MAX_CORRELATION_LAG * self.SAMPLES_PER_SECOND ) correlationsX = np.apply_along_axis( crossCorrelateComplete, 2, detectedOpticalFlowBufferX, detectedGripperAngleBuffer, maxLag ) correlationsY = np.apply_along_axis( crossCorrelateComplete, 2, detectedOpticalFlowBufferY, detectedGripperAngleBuffer, maxLag ) t2 = time.time() print 'Correlation took %0.3f ms' % ((t2-t1)*1000.0) # Detect the input signal based on the correlation in the x and y axis maxCorrelationArrayX = np.maximum.reduce( np.absolute( correlationsX ), axis=2 ) maxCorrelationArrayY = np.maximum.reduce( np.absolute( correlationsY ), axis=2 ) self.inputSignalDetectedArray = np.frompyfunc( isInputSignalPresent, 2, 1 )( maxCorrelationArrayX, maxCorrelationArrayY ) # Build a histogram for the gripper self.gripperHistogram = cv.CreateHist( [ 256/8, 256/8, 256/8 ], cv.CV_HIST_ARRAY, [ (0,255), (0,255), (0,255) ], 1 ) r_plane = cv.CreateMat( imageRGB.height, imageRGB.width, cv.CV_8UC1 ) g_plane = cv.CreateMat( imageRGB.height, imageRGB.width, cv.CV_8UC1 ) b_plane = cv.CreateMat( imageRGB.height, imageRGB.width, cv.CV_8UC1 ) cv.Split( imageRGB, r_plane, g_plane, b_plane, None ) planes = [ r_plane, g_plane, b_plane ] maskArray = np.zeros(shape=( imageRGB.height, imageRGB.width ), dtype=np.uint8 ) for rowIdx in range( self.inputSignalDetectedArray.shape[ 0 ] ): for colIdx in range( self.inputSignalDetectedArray.shape[ 1 ] ): if self.inputSignalDetectedArray[ rowIdx, colIdx ]: rowStartIdx = rowIdx*self.OPTICAL_FLOW_BLOCK_HEIGHT rowEndIdx = rowStartIdx + self.OPTICAL_FLOW_BLOCK_HEIGHT colStartIdx = colIdx*self.OPTICAL_FLOW_BLOCK_WIDTH colEndIdx = colStartIdx + self.OPTICAL_FLOW_BLOCK_WIDTH maskArray[ rowStartIdx:rowEndIdx, colStartIdx:colEndIdx ] = 255 cv.CalcHist( [ cv.GetImage( i ) for i in planes ], self.gripperHistogram, 0, mask=maskArray ) minX = 1000 maxX = 0 minY = 1000 maxY = 0 numMotionBlocks = 0 # Create the track window from the blocks where motion was detected for rowIdx in range( self.inputSignalDetectedArray.shape[ 0 ] ): for colIdx in range( self.inputSignalDetectedArray.shape[ 1 ] ): if self.inputSignalDetectedArray[ rowIdx, colIdx ]: if rowIdx < minY: minY = rowIdx if rowIdx > maxY: maxY = rowIdx if colIdx < minX: minX = colIdx if colIdx > maxX: maxX = colIdx numMotionBlocks += 1 if numMotionBlocks > 0: windowX = minX*self.OPTICAL_FLOW_BLOCK_WIDTH windowY = minY*self.OPTICAL_FLOW_BLOCK_HEIGHT windowWidth = (maxX+1-minX)*self.OPTICAL_FLOW_BLOCK_WIDTH windowHeight = (maxY+1-minY)*self.OPTICAL_FLOW_BLOCK_HEIGHT self.gripperTrackWindow = ( windowX, windowY, windowWidth, windowHeight )
def main(): BLACK_AND_WHITE = False THRESHOLD = 0.48 BW_THRESHOLD = 0.4 os.chdir(sys.argv[1]) try: os.mkdir(OUTPUT_DIR_NAME) except: pass if len(sys.argv) > 2: if sys.argv[2] == "bw": BLACK_AND_WHITE = True THRESHOLD = BW_THRESHOLD print "##########" print " B/W MODE" print "##########" tree = et.parse("project.xml") movie = tree.getroot() file_path = movie.attrib["path"] cap = cv.CreateFileCapture(file_path) if DEBUG: cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("win", 200, 200) hist = None prev_hist = None prev_img = None pixel_count = None frame_counter = 0 last_frame_black = False black_frame_start = -1 t = time.time() while 1: img_orig = cv.QueryFrame(cap) if not img_orig: # eof cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter - 1), prev_img) """movie.set("frames", str(frame_counter)) tree.write("project.xml")""" break img = cv.CreateImage( (int(img_orig.width / 4), int(img_orig.height / 4)), cv.IPL_DEPTH_8U, 3) cv.Resize(img_orig, img, cv.CV_INTER_AREA) if frame_counter == 0: # erster frame cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (0), img) pixel_count = img.width * img.height prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(prev_img) if DEBUG and frame_counter % 2 == 1: cv.ShowImage("win", img) img_hsv = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.CvtColor(img, img_hsv, cv.CV_BGR2HSV) # ##################### # METHOD #1: find the number of pixels that have (significantly) changed since the last frame diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.AbsDiff(img_hsv, prev_img, diff) cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY) d_color = 0 for i in range(1, 4): cv.SetImageCOI(diff, i) d_color += float(cv.CountNonZero(diff)) / float(pixel_count) if not BLACK_AND_WHITE: d_color = float(d_color / 3.0) # 0..1 # ##################### # METHOD #2: calculate the amount of change in the histograms h_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) s_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) v_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1) cv.Split(img_hsv, h_plane, s_plane, v_plane, None) planes = [h_plane, s_plane, v_plane] hist_size = [50, 50, 50] hist_range = [[0, 360], [0, 255], [0, 255]] if not hist: hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) cv.NormalizeHist(hist, 1.0) if not prev_hist: prev_hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1) # wieso gibt es kein cv.CopyHist()?! cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist) cv.NormalizeHist(prev_hist, 1.0) continue d_hist = cv.CompareHist(prev_hist, hist, cv.CV_COMP_INTERSECT) # combine both methods to make a decision if ((0.4 * d_color + 0.6 * (1 - d_hist))) >= THRESHOLD: if DEBUG: if frame_counter % 2 == 0: cv.ShowImage("win", img) winsound.PlaySound(soundfile, winsound.SND_FILENAME | winsound.SND_ASYNC) print "%.3f" % ((0.4 * d_color + 0.6 * (1 - d_hist))), "%.3f" % ( d_color), "%.3f" % (1 - d_hist), frame_counter if DEBUG and DEBUG_INTERACTIVE: if win32api.MessageBox(0, "cut?", "", win32con.MB_YESNO) == 6: #yes cv.SaveImage( OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img) else: cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img) cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist) cv.NormalizeHist(prev_hist, 1.0) # ##################### # METHOD #3: detect series of (almost) black frames as an indicator for "fade to black" average = cv.Avg(v_plane)[0] if average <= 0.6: if not last_frame_black: # possible the start print "start", frame_counter black_frame_start = frame_counter last_frame_black = True else: if last_frame_black: # end of a series of black frames cut_at = black_frame_start + int( (frame_counter - black_frame_start) / 2) print "end", frame_counter, "cut at", cut_at img_black = cv.CreateImage( (img_orig.width / 4, img_orig.height / 4), cv.IPL_DEPTH_8U, 3) cv.Set(img_black, cv.RGB(0, 255, 0)) cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (cut_at), img_black) last_frame_black = False cv.Copy(img_hsv, prev_img) frame_counter += 1 if DEBUG: if cv.WaitKey(1) == 27: break if DEBUG: cv.DestroyWindow("win") print "%.2f min" % ((time.time() - t) / 60) #raw_input("- done -") return