def handle_depth_frame(self, dev, data, timestamp): return depth = self.pretty_depth(data) # Calculate depth layers depth_layers = numpy.zeros_like(depth) for (low, high, value) in self.layers: depth_copy = numpy.copy(depth) segment = self.threshold(depth_copy, low, high, value=value) depth_layers = numpy.add(depth_layers, segment) frame = self.img_from_depth_frame(depth_layers) frame_array = numpy.asarray(frame[:, :]) if not self.gray_image: self.gray_image = cv.CreateImage(cv.GetSize(frame), frame.depth, 1) self.temp_image = cv.CreateImage(cv.GetSize(frame), frame.depth, 1) if self.overlay_video: if self.gray_image and self.last_video_frame: cv.CvtColor(self.last_video_frame, self.gray_image, cv.CV_BGR2GRAY) gray_frame_array = self.array(self.gray_image) empty_frame_array = numpy.zeros_like(gray_frame_array) cv.AddWeighted(self.gray_image, 1, frame, 1, 1, self.temp_image) frame_array = self.array(self.temp_image)
def drawHistogram(self, image, chnum, hist_arr, plateaus): positions = (0, (self.Ihist.height + 10), 2 * self.Ihist.height + 20) colour_values = _blue, _green, _red colour = colour_values[chnum] Y = positions[chnum] cv.Set(self.Ihist, _trans) bin_w = cv.Round(float(self.Ihist.width) / self.hist_size) # min_value, max_value, pmin, pmax = cv.GetMinMaxHistValue(hist) X = image.width - self.Ihist.width rect = (X, Y, self.Ihist.width, self.Ihist.height) cv.SetImageROI(image, rect) scaling = self.Ihist.height / max(hist_arr) hist_arr *= scaling 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) for i in plateaus[chnum]: 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 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 threshold_image(pil_image, file_name): cv_i = piltocv(pil_image) #calculate_histogram(pil_image) #split channels: r = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) g = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) b = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) #cv.Histogram(r) #cv.Split(cv_i, r, g, b, None) #return #calculate image brightness: cv_brightness = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) cv.AddWeighted(r, 1./3., g, 1./3., 0.0, cv_brightness) cv.AddWeighted(cv_brightness, 2./3., b, 1./3., 0.0, cv_brightness) cv_union = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) cv.Set(cv_union, cv.CV_RGB(0, 0, 0)); band_names = ['r','g','b'] idx = 0 cutoff = 15 print cv.GetSize(cv_i), cv_i.depth for band in [r, g, b]: dst = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) cv.AdaptiveThreshold(band, dst, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY, 75, 10) cv.SaveImage("%sthresh_%s_%s.png" % \ (path, 'adaptive', band_names[idx]), dst) '''for threshold in range(0, 21, 1): dst = cv.CreateImage(cv.GetSize(cv_i), cv_i.depth, 1) #cv.Threshold(band, dst, threshold, 255, cv.CV_THRESH_BINARY ) cv.AdaptiveThreshold(band, dst, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY, 25, 1) if threshold == cutoff: cv.Or(cv_union, dst, cv_union) cv.SaveImage("/usr/local/django/localground/jobs/stats/canny/thresh_%s_%s.png" % \ (band_names[idx], threshold), dst)''' idx += 1 cv.SaveImage("/usr/local/django/localground/jobs/stats/canny/%s_mask.png" % file_name, cv_union)
def circle_highest(filename): '''circle the highest value pixel in an image''' pgm = util.PGM(filename) maxpoint = pgm.array.argmax() maxpos = (maxpoint % 1280, maxpoint / 1280) color_img = cv.CreateImage((1280, 960), 16, 3) cv.CvtColor(pgm.img, color_img, cv.CV_GRAY2RGB) overlay = cv.CreateImage((1280, 960), 16, 3) cv.SetZero(overlay) cv.Circle(overlay, maxpos, 10, cv.CV_RGB(65535, 0, 0)) cv.AddWeighted(color_img, 1.0, overlay, 1.0, 0.5, color_img)
def show_edges(filename): '''show edges in an image''' pgm = util.PGM(filename) # convert to 8 bit img8 = cv.CreateImage((1280, 960), 8, 1) cv.ConvertScale(pgm.img, img8, scale=1.0 / 256) edge1 = cv.CreateImage((1280, 960), 8, 1) cv.Canny(img8, edge1, 250, 255, 5) edgecolor = cv.CreateImage((1280, 960), 8, 3) edgecolor16 = cv.CreateImage((1280, 960), 16, 3) cv.CvtColor(edge1, edgecolor, cv.CV_GRAY2RGB) cv.ConvertScale(edgecolor, edgecolor16, scale=256) color_img = cv.CreateImage((1280, 960), 16, 3) cv.CvtColor(pgm.img, color_img, cv.CV_GRAY2RGB) cv.AddWeighted(color_img, 1.0, edgecolor16, 1.0, 0.5, color_img)
def overlay(im, overlay, pos=(0, 0), blend=1, nowarning=False): """ Overlays an image with a specified blend ratio. If the overlay image is too big to fit at the specified position, it is cropped down to size and a warning is raised. This can be turned off by setting ``nowarning = True``. **Parameters:** * im (cvArr) - The source image, * overlay (cvArr) - The image to be overlayed. * pos (tuple) - The position in the image to place the top-left corner of the overlay image. * blend (float)/(string) - The blend ratio. If blend is 1, the overlay image is fully opaque, if blend is 0, the overlay image is invisible. Between 1 and 0 is transparent. If blend = 'both', both images are added as they are, this turns any black in the overlay region to become fully transparent. * nowarning (bool) - Suppress warning message. **Returns:** * The source image with overlay image added. """ size = (overlay.width, overlay.height) # Should I move the coordinate inside as with zoom() and sample() or crop the overlay image? I think cropping might be the best way to go here. warn = False if pos[0] + size[0] > im.width: size = (im.width - 1 - pos[0], size[1]) warn = True if pos[1] + size[1] > im.height: size = (size[1], im.height - 1 - pos[1]) warn = True if warn: overlay = crop(overlay, size) if nowarning == False: warnings.warn( "The overlay image was too big to fit at the position specified. It has been cropped to %dx%d. Use 'nowarning = True' to suppress this warning." % (size[0], size[1]), stacklevel=2) cv.SetImageROI(im, pos + size) if blend == 'both': alpha = 1 blend = 1 else: alpha = 1 - blend cv.AddWeighted(im, alpha, overlay, blend, 0, im) cv.ResetImageROI(im) return im
filename = "../data/output/weighted.avi" # Codec is OS dependant. # FIXME : Find an unified version if "win" in sys.platform: fourcc = cv.CV_FOURCC('C', 'V', 'I', 'D') else: # some kind of Linux/Unix platform fourcc = cv.CV_FOURCC('F', 'M', 'P', '4') fps = 5 frameSize = cv.GetSize(im1) my_video = cv.CreateVideoWriter(filename, fourcc, fps, frameSize, 1) num_inter_im = 10 step = float(1) / 10 for i in range(num_inter_im + 1): print i im3 = cv.CreateImage(cv.GetSize(im1), im1.depth, im1.nChannels) alpha = step * i beta = 1 - alpha gamma = 0 cv.AddWeighted(im1, alpha, im2, beta, gamma, im3) cv.WriteFrame(my_video, im3) # cv.NamedWindow("im3", cv.CV_WINDOW_NORMAL) # cv.ResizeWindow("im3", 640, 480) # cv.MoveWindow("im3", 640, 500) # cv.ShowImage("im3", im3) # cv.WaitKey(delay)
def doSSIM(frame1, frame2): ''' The equivalent of Zhou Wang's SSIM matlab code using OpenCV. from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html The measure is described in : "Image quality assessment: From error measurement to structural similarity" C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM C++ to Python translation and adaptation by Iñaki Úcar ''' def array2cv(a): dtype2depth = { 'uint8': cv.IPL_DEPTH_8U, 'int8': cv.IPL_DEPTH_8S, 'uint16': cv.IPL_DEPTH_16U, 'int16': cv.IPL_DEPTH_16S, 'int32': cv.IPL_DEPTH_32S, 'float32': cv.IPL_DEPTH_32F, 'float64': cv.IPL_DEPTH_64F, } try: nChannels = a.shape[2] except: nChannels = 1 cv_im = cv.CreateImageHeader((a.shape[1], a.shape[0]), dtype2depth[str(a.dtype)], nChannels) cv.SetData(cv_im, a.tostring(), a.dtype.itemsize * nChannels * a.shape[1]) return cv_im C1 = 6.5025 C2 = 58.5225 img1_temp = array2cv(frame1) img2_temp = array2cv(frame2) nChan = img1_temp.nChannels d = cv.IPL_DEPTH_32F size = img1_temp.width, img1_temp.height img1 = cv.CreateImage(size, d, nChan) img2 = cv.CreateImage(size, d, nChan) cv.Convert(img1_temp, img1) cv.Convert(img2_temp, img2) img1_sq = cv.CreateImage(size, d, nChan) img2_sq = cv.CreateImage(size, d, nChan) img1_img2 = cv.CreateImage(size, d, nChan) cv.Pow(img1, img1_sq, 2) cv.Pow(img2, img2_sq, 2) cv.Mul(img1, img2, img1_img2, 1) mu1 = cv.CreateImage(size, d, nChan) mu2 = cv.CreateImage(size, d, nChan) mu1_sq = cv.CreateImage(size, d, nChan) mu2_sq = cv.CreateImage(size, d, nChan) mu1_mu2 = cv.CreateImage(size, d, nChan) sigma1_sq = cv.CreateImage(size, d, nChan) sigma2_sq = cv.CreateImage(size, d, nChan) sigma12 = cv.CreateImage(size, d, nChan) temp1 = cv.CreateImage(size, d, nChan) temp2 = cv.CreateImage(size, d, nChan) temp3 = cv.CreateImage(size, d, nChan) ssim_map = cv.CreateImage(size, d, nChan) #/*************************** END INITS **********************************/ #// PRELIMINARY COMPUTING cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Pow(mu1, mu1_sq, 2) cv.Pow(mu2, mu2_sq, 2) cv.Mul(mu1, mu2, mu1_mu2, 1) cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq) cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq) cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12) #////////////////////////////////////////////////////////////////////////// #// FORMULA #// (2*mu1_mu2 + C1) cv.Scale(mu1_mu2, temp1, 2) cv.AddS(temp1, C1, temp1) #// (2*sigma12 + C2) cv.Scale(sigma12, temp2, 2) cv.AddS(temp2, C2, temp2) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2)) cv.Mul(temp1, temp2, temp3, 1) #// (mu1_sq + mu2_sq + C1) cv.Add(mu1_sq, mu2_sq, temp1) cv.AddS(temp1, C1, temp1) #// (sigma1_sq + sigma2_sq + C2) cv.Add(sigma1_sq, sigma2_sq, temp2) cv.AddS(temp2, C2, temp2) #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Mul(temp1, temp2, temp1, 1) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Div(temp3, temp1, ssim_map, 1) index_scalar = cv.Avg(ssim_map) #// through observation, there is approximately #// 1% error max with the original matlab program return index_scalar[0]
(int(obj.frames[i, 0]) - 1, int(obj.frames[i, 1]) - 1), (int(obj.frames[i, 0]) + 1, int(obj.frames[i, 1]) + 1), cv.Scalar(255, 255, 255)) if draw_box: if obj.frames != None: col = cv.Scalar(0, 255, 0) else: col = cv.Scalar(0, 0, 255) for j in range(4): cv.Line(contours, obj.box_points[j], obj.box_points[(j + 1) % 4], col) # output images outroi = (0, hist_height, width, height) a = cv.GetSubRect(out, outroi) cv.AddWeighted(contours, 0.5, current_image_frame, 0.5, 0, a) histroi = (0, 0, width, hist_height) a = cv.GetSubRect(out, histroi) cv.Copy(hist_img, a) loop_c += 1 timing['t_draw'] += time.time() - t0 # show the images cv.ShowImage("Depth Stream", contours) cv.ShowImage("conts", out) if writer: bla = cv.CreateImage((width, height + hist_height), cv.IPL_DEPTH_8U, 3) cv.SetData(bla, out.tostring()) cv.WriteFrame(writer, bla) # wait for user input (keys)
def run_real_time_recognition(para_path, Labels): status_dictionary = {} # status, pos, radias, color, text, ,pos, font_color # states: # 0 -> waiting to be hovered # 1 -> hovered waiting to be selected(clicked) # 2 -> selected waiting to be unselected(clicked) start_time = 0 status_dictionary['b1'] = [False, (530, 70), 60, (255, 255, 0), 'Record', (490, 70), (0,0,0), [], False] status_dictionary['b2'] = [False, (380, 70), 60, (0, 255, 0), 'Select', (350, 70), (0,0,0), [], False] status_dictionary['b3'] = [False, (240, 70), 60, (0, 255, 255), 'Billard', (210, 70),(0,0,0), [], False] status_dictionary['b4'] = [False, (100, 270), 90, (255, 255, 255), 'Drag Me', (70, 270),(0,0,0), [], False] global depth,ir, rgb count = 0 # frame_size = (480,640) # Setting web cam config capture=cv.CaptureFromCAM(0) fourcc = cv.CV_FOURCC('X','V','I','D') cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FPS, 25) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480) # Neuronet Configuration resize_row = 20 resize_width = 20 weights = loadmat(para_path) T1 = weights['Theta1'] T2 = weights['Theta2'] history_prediction = [] # smoothing and other purpose history_gesture_pos = [] # smoothing and other purpose # Recording record_st = False rgb_writer = cv.CreateVideoWriter("recording.avi", cv.CV_FOURCC('X','V','I','D'), 5, (640, 480), True) # Capture frames IR, RGB, Depth while True: # Web cam feed (300, 400, 3) rgb_ipl = cv.QueryFrame(capture) # Depth IR feed (depth,_), (ir,_) = get_depth(), get_video(format=2) ir = (ir>150).astype(float) ir = ir*255 ir_ipl = resize_ir_callibrate_with_rgb(ir) new_rgb_ipl = cv.CreateImage(cv.GetSize(rgb_ipl), 8, 3) #Billard Mode yo = rgb_ipl f = iplimage_to_numpy_color(yo) green_mono = f[:,:,1] #image = cv.fromarray(np.array(green_mono[:,:])) #cv.ShowImage('G', image) rgb_np, threshold_np, contour_list = billard_extract_and_draw_countour(f, 20, green_mono, 120, 0) image = cv.fromarray(np.array(rgb_np)) #print contour_list maxx = (0,0,0,0) for pos in contour_list: if pos[1] > maxx[1]: maxx = pos #print maxx for item in contour_list: if maxx != item: cv.Line(image, (maxx[0]+maxx[2]/2, maxx[1]+maxx[3]/2), (item[0]+item[2]/2,item[1]+item[3]/2), (0,255,0), thickness=1, lineType=8, shift=0) #cv.ShowImage('G Threshold', image) new_rgb_ipl = cvMat_to_iplimage_color(image) #cv.ShowImage('G Threshold', new_rgb_ipl) # Hand Sengmentation rgb_np, ir_np, contour_list, history_gesture_pos = real_time_extract_and_draw_countour(ir_ipl, rgb_ipl, 20000, history_gesture_pos) # Gesture Recognition if contour_list: ir_ipl, rgb_ipl, history_prediction = real_time_gesture_recognition_and_labeling(ir_np, rgb_np, contour_list, T1, T2, Labels, history_prediction, False) # Update button status status_dictionary, start_time = update_button_status(contour_list, history_prediction, Labels, status_dictionary, history_gesture_pos, False, start_time) draw_menu_button(ir_ipl, rgb_ipl, status_dictionary, start_time) # resize for full screen display """ rgb_np = iplimage_to_numpy_color(rgb_ipl) rgb_np = imresize(rgb_np, (800, 1066)) image = cv.fromarray(np.array(rgb_np)) cv.ShowImage('rgb', image) """ if status_dictionary['b3'][0]: opacity = 0.4 cv.AddWeighted(new_rgb_ipl, opacity, rgb_ipl, 1 - opacity, 0, rgb_ipl) if status_dictionary['b1'][0]: cv.WriteFrame(rgb_writer, rgb_ipl) else: record_status=False cv.ShowImage('rgb', rgb_ipl) cv.ShowImage('ir', ir_ipl) c=cv.WaitKey(5) if c==27: #Break if user enters 'Esc'. break
def blend(im1, im2, alpha=0.5): new_im = new_from(im1) cv.AddWeighted(im1, alpha, im2, 1-alpha, 0.0, new_im) return new_im
framecounter = 0 w = 5 cv.NamedWindow("camera", cv.CV_WINDOW_AUTOSIZE) capture = cv.CaptureFromCAM(0) img = cv.QueryFrame(capture) (r, c) = cv.GetSize(img) imgGray = cv.CreateImage((r, c), cv.IPL_DEPTH_8U, 1) imgRotate = cv.CreateImage((r, c), cv.IPL_DEPTH_8U, 1) imgAvg = cv.CreateImage((r, c), cv.IPL_DEPTH_8U, 1) rotMat = cv.CreateMat(2, 3, cv.CV_32FC1) while True: img = cv.QueryFrame(capture) framecounter = framecounter + 1 cv.CvtColor(img, imgGray, cv.CV_RGB2GRAY) cv.GetRotationMatrix2D((r / 2, c / 2), w * framecounter, 1.0, rotMat) cv.WarpAffine(imgGray, imgRotate, rotMat) alpha = 0.1 cv.AddWeighted(imgAvg, 1.0 - alpha, imgRotate, alpha, 0.0, imgAvg) cv.ShowImage("camera", imgAvg) if cv.WaitKey(10) == 27: break
def continuousLearnBackground(self, frame): if self.bgLearnRate == 0: return cv.AddWeighted(frame, self.bgLearnRate, self.bg, 1.0 - self.bgLearnRate, 0, self.bg)
while contour: yield contour contour = contour.h_next() cv.Zero(markers) comp_count = 0 for c in contour_iterator(contours): cv.DrawContours(markers, c, cv.ScalarAll(comp_count + 1), cv.ScalarAll(comp_count + 1), -1, -1, 8) comp_count += 1 cv.Watershed(img0, markers) cv.Set(wshed, cv.ScalarAll(255)) # paint the watershed image color_tab = [(cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50) for i in range(comp_count)] for j in range(markers.height): for i in range(markers.width): idx = markers[j, i] if idx != -1: wshed[j, i] = color_tab[int(idx - 1)] cv.AddWeighted(wshed, 0.5, img_gray, 0.5, 0, wshed) cv.ShowImage("watershed transform", wshed)
def __SSIM(self, frame1, frame2): """ The equivalent of Zhou Wang's SSIM matlab code using OpenCV. from http://www.cns.nyu.edu/~zwang/files/research/ssim/index.html The measure is described in : "Image quality assessment: From error measurement to structural similarity" C++ code by Rabah Mehdi. http://mehdi.rabah.free.fr/SSIM C++ to Python translation and adaptation by Iñaki Úcar """ C1 = 6.5025 C2 = 58.5225 img1_temp = self.__array2cv(frame1) img2_temp = self.__array2cv(frame2) nChan = img1_temp.nChannels d = cv.IPL_DEPTH_32F size = img1_temp.width, img1_temp.height img1 = cv.CreateImage(size, d, nChan) img2 = cv.CreateImage(size, d, nChan) cv.Convert(img1_temp, img1) cv.Convert(img2_temp, img2) img1_sq = cv.CreateImage(size, d, nChan) img2_sq = cv.CreateImage(size, d, nChan) img1_img2 = cv.CreateImage(size, d, nChan) cv.Pow(img1, img1_sq, 2) cv.Pow(img2, img2_sq, 2) cv.Mul(img1, img2, img1_img2, 1) mu1 = cv.CreateImage(size, d, nChan) mu2 = cv.CreateImage(size, d, nChan) mu1_sq = cv.CreateImage(size, d, nChan) mu2_sq = cv.CreateImage(size, d, nChan) mu1_mu2 = cv.CreateImage(size, d, nChan) sigma1_sq = cv.CreateImage(size, d, nChan) sigma2_sq = cv.CreateImage(size, d, nChan) sigma12 = cv.CreateImage(size, d, nChan) temp1 = cv.CreateImage(size, d, nChan) temp2 = cv.CreateImage(size, d, nChan) temp3 = cv.CreateImage(size, d, nChan) ssim_map = cv.CreateImage(size, d, nChan) #/*************************** END INITS **********************************/ #// PRELIMINARY COMPUTING cv.Smooth(img1, mu1, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Smooth(img2, mu2, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.Pow(mu1, mu1_sq, 2) cv.Pow(mu2, mu2_sq, 2) cv.Mul(mu1, mu2, mu1_mu2, 1) cv.Smooth(img1_sq, sigma1_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq) cv.Smooth(img2_sq, sigma2_sq, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq) cv.Smooth(img1_img2, sigma12, cv.CV_GAUSSIAN, 11, 11, 1.5) cv.AddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12) #////////////////////////////////////////////////////////////////////////// #// FORMULA #// (2*mu1_mu2 + C1) cv.Scale(mu1_mu2, temp1, 2) cv.AddS(temp1, C1, temp1) #// (2*sigma12 + C2) cv.Scale(sigma12, temp2, 2) cv.AddS(temp2, C2, temp2) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2)) cv.Mul(temp1, temp2, temp3, 1) #// (mu1_sq + mu2_sq + C1) cv.Add(mu1_sq, mu2_sq, temp1) cv.AddS(temp1, C1, temp1) #// (sigma1_sq + sigma2_sq + C2) cv.Add(sigma1_sq, sigma2_sq, temp2) cv.AddS(temp2, C2, temp2) #// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Mul(temp1, temp2, temp1, 1) #// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)) cv.Div(temp3, temp1, ssim_map, 1) index_scalar = cv.Avg(ssim_map) #// through observation, there is approximately #// 1% error max with the original matlab program return index_scalar[0]
# FPS calc if DISPLAY: framecnt = framecnt + 1 ctime = time() if ctime - stime >= 1: print '%ifps' % (framecnt) framecnt = 0 stime = ctime # Get original image img = cv.QueryFrame(cam) # Adaptive diff #cv.Smooth(img, col, cv.CV_GAUSSIAN, 3, 0) cv.AddWeighted(img, 1 - ADAPT, avg, ADAPT, 0, avg) cv.AbsDiff(img, avg, col) cv.CvtColor(col, col, cv.CV_RGB2HSV) cv.Split(col, None, None, val, None) cv.CmpS(val, TH, val, cv.CV_CMP_GE) #cv.Dilate(val, val, None, 18) #cv.Erode(val, val, None, 10) dif = cv.CountNonZero(val) # Show image if it's radically new if (dif < PIXCOUNT): if act < 0: act = ACTLOW else: act -= 1 else:
def imageBlend(a, b, r): img = cv.CreateImage(cv.GetSize(a), cv.IPL_DEPTH_32F, 3) cv.AddWeighted(a, 1. - r, b, r, 0, img) return img