def update_histogram(self, region): cv.SetImageROI(self.hue, region) cv.SetImageROI(self.sat, region) cv.CalcArrHist([self.hue, self.sat], self.face_hist, 1) #cv.NormalizeHist(self.face_hist, 255) cv.ResetImageROI(self.hue) cv.ResetImageROI(self.sat)
def run(self): while True: frame = cv.QueryFrame( self.capture) # Run the cam-shift if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x,y,w,h = self.selection cv.Rectangle(frame, (x,y), (x+w,y+h), (255,255,255)) sel = cv.GetSubRect(self.hue, self.selection ) cv.CalcArrHist( [sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue( hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox( frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) cv.ShowImage("Output",frame) cv.WaitKey(0)
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False while True: frame = cv.QueryFrame(self.capture) # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) print(self.hue) cv.Split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) # Run the cam-shift cv.CalcArrBackProject([self.hue], backproject, hist) if self.track_window and is_rect_nonzero(self.track_window): crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x, y, w, h = self.selection cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) sel = cv.GetSubRect(self.hue, self.selection) cv.CalcArrHist([sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) if not backproject_mode: #frame=cv.Flip(frame) cv.ShowImage("CamShiftDemo", frame) else: cv.ShowImage("CamShiftDemo", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode
def do_camshift(self, cv_image): """ Get the image size """ image_size = cv.GetSize(cv_image) image_width = image_size[0] image_height = image_size[1] """ Convert to HSV and keep the hue """ hsv = cv.CreateImage(image_size, 8, 3) cv.CvtColor(cv_image, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(image_size, 8, 1) cv.Split(hsv, self.hue, None, None, None) """ Compute back projection """ backproject = cv.CreateImage(image_size, 8, 1) """ Run the cam-shift algorithm """ cv.CalcArrBackProject( [self.hue], backproject, self.hist ) if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect """ If mouse is pressed, highlight the current selected rectangle and recompute the histogram """ if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(cv_image, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(cv_image, cv_image, 0.5) cv.Copy(save, sub) x,y,w,h = self.selection cv.Rectangle(cv_image, (x,y), (x+w,y+h), (255,255,255)) sel = cv.GetSubRect(self.hue, self.selection ) cv.CalcArrHist( [sel], self.hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist) if max_val != 0: cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox( cv_image, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) roi = RegionOfInterest() roi.x_offset = int(min(image_width, max(0, track_box[0][0] - track_box[1][0] / 2))) roi.y_offset = int(min(image_height, max(0, track_box[0][1] - track_box[1][1] / 2))) roi.width = int(track_box[1][0]) roi.height = int(track_box[1][1]) self.ROI.publish(roi) cv.ShowImage("Histogram", self.hue_histogram_as_image(self.hist)) if not self.backproject_mode: return cv_image else: return backproject
def calcularHistograma(self): cv.CalcArrHist([self.imagem_destino], self.histograma) valorMaximoMinimo = cv.GetMinMaxHistValue(self.histograma) cv.Rectangle(self.imagemHistograma, (0, 0), (512, 100), cv.CV_RGB(0, 0, 0), -1) for i in range(512): valor = cv.QueryHistValue_1D(self.histograma, i) normalizado = cv.Round(valor * 100 / valorMaximoMinimo[1]) cv.Line(self.imagemHistograma, (i, 100), (i, 100 - normalizado), cv.CV_RGB(255, 255, 255))
def update_histogram(self, face): (x, y, w, h) = face x2 = int(x + w * FACE_BORDER) y2 = int(y + h * FACE_BORDER) w2 = int(w - w * FACE_BORDER * 2) h2 = int(h - h * FACE_BORDER * 2) cv.SetImageROI(self.hue, (x2, y2, w2, h2)) cv.SetImageROI(self.sat, (x2, y2, w2, h2)) cv.CalcArrHist([self.hue, self.sat], self.hist, 1) cv.NormalizeHist(self.hist, 255) cv.ResetImageROI(self.hue) cv.ResetImageROI(self.sat) cv.Rectangle(self.visualize, (x, y), (x + w, y + h), (255, 0, 0)) cv.Rectangle(self.visualize, (x2, y2), (x2 + w2, y2 + h2), (128, 150, 0))
def update(self, im=None): if im is not None: self.im = im cv.CalcArrHist([self.im], self.hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist) cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0) cv.Set(self.hist_image, cv.ScalarAll(255)) bin_w = round(float(self.hist_image.width) / self.hist_size) for i in range(self.hist_size): cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height), (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])), self.color, -1, 8, 0) cv.ShowImage(self.title, self.hist_image)
def set_hist(self, frame, selection): sub = cv.GetSubRect(frame, selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x, y, w, h = selection # rectangular piece of frame cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) sel = cv.GetSubRect(self.hue, selection) cv.CalcArrHist([sel], self.hist, 0) # get the most prevalent color in the histogram (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist) if max_val != 0: cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val) print "Val set to " + str(max_val)
def OCVHistogram(frame, ranges=[[0, 256]], hist_size=64): """Create a histogram of given frame""" if frame.nChannels != 1: dest = OCVCopyGrayscale(frame) else: dest = frame hist_image = cv.CreateImage((dest.width, dest.height), 8, 1) hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcArrHist([dest], hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist) cv.Scale(hist.bins, hist.bins, float(hist_image.height) / max_value, 0) cv.Set(hist_image, cv.ScalarAll(255)) bin_w = round(float(hist_image.width) / hist_size) for i in range(hist_size): cv.Rectangle(hist_image, (int(i * bin_w), hist_image.height), (int( (i + 1) * bin_w), hist_image.height - cv.Round(hist.bins[i])), cv.ScalarAll(0), -1, 8, 0) return hist_image
def recompute_histogram(self, hist): sel = cv.GetSubRect(self.hue, self.selection) cv.CalcArrHist([sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)
def OnIdle( self, ): """Request refresh of the context whenever idle. track, get position, update camera, then redraw""" hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0,180)], 1 ) backproject_mode = False while True: frame = cv.QueryFrame(self.capture) # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.Split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) # Run the cam-shift cv.CalcArrBackProject( [self.hue], backproject, hist ) if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x,y,w,h = self.selection cv.Rectangle(frame, (x,y), (x+w,y+h), (0,0,255)) sel = cv.GetSubRect(self.hue, self.selection ) cv.CalcArrHist( [sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue( hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox(frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 ) # find centroid coordinate (x,y) and area (z) selection_centroid = track_box[0] global xposition xposition = selection_centroid[0] global yposition yposition = selection_centroid[1] width_height = track_box[1] # writes output of coordinates to seed file if needed # with open('seed.txt', 'a') as f: # value = (xposition, yposition) # s = str(value) + '\n' # f.write(s) # # f.write('end_of_session') # f.close() # print outs print "x: " + str(xposition) print "y: " + str(yposition) selection_area = width_height[0]*width_height[1] # print "The width is: " + str(width_height[0]) + " The height is: " + str(width_height[1]) # print "centroid is: " + str(selection_centroid) # return "centroid is: " + str(selection_centroid) print "area: " + str(selection_area) # return "area is: " + str(selection_area) if not backproject_mode: cv.ShowImage( "CamShiftDemo", frame ) else: cv.ShowImage( "CamShiftDemo", backproject) cv.ShowImage( "Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(10) if c == 27: # escape key break elif c == ord("b"): # show backproject mode with "b" key backproject_mode = not backproject_mode self.triggerRedraw(1) return 1
def camshift_track(roi_selection, img): """ setup_camshift: - is_rect_nonzero(r) - hue_histogram_as_image(hist) - making_selection(roi_selection) - create_hist() - placeholder() compute_camshift_centroid: - isolate_hue - compute_back_projection - recompute_histogram(roi_selection) - run_camshift - draw ellipse - find_centroid(track_box) - show_hist """ # setup_camshift def is_rect_nonzero(r): (_, _, w, h) = r return (w > 0) and (h > 0) def hue_histogram_as_image(hist): """ Returns representation of a hue histogram """ 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) return histimg # 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) # end of making_selection # create_hist() hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) # end of create_hist # placeholder() backproject_mode = False # end of placeholder while True: # capture = cv.CaptureFromCAM(0) # frame = cv.QueryFrame(capture) # isolate_hue # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) hue = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.Split(hsv, hue, None, None, None) # end isolate_hue # Compute back projection backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) # end compute back projection # highlight the current selected rectangle and recompute the histogram w = xmax - xmin h = ymax - ymax xmin, ymin, w, h = selection cv.Rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255)) # end highlight # Run the camshift cv.CalcArrBackProject([hue], backproject, hist) # end run camshift # draw ellipse print "selection is" + str(selection) if selection and is_rect_nonzero(selection): crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, selection, crit) selection = rect sel = cv.GetSubRect(hue, selection) cv.CalcArrHist([sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif selection and is_rect_nonzero(selection): cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) # end draw ellipse # find centroid coordinate (x,y) and area (z) selection_centroid = track_box[0] xposition = selection_centroid[0] yposition = selection_centroid[1] width_height = track_box[1] # selection_area = width_height[0]*width_height[1] face_centroid_camshift = (xposition, yposition) # return face_centroid_camshift # end find centroid # show hist if backproject_mode: # cv.ShowImage( "CamShiftDemo", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) # end show hist print "I made it this far" # print face_centroid_camshift return face_centroid_camshift
def pipeline(self): presentation = [] self.orig = self.source.grab_frame() cv.Resize(self.orig, self.small) cv.CvtColor(self.small, self.hsv, cv.CV_BGR2HSV) cv.Split(self.hsv, self.hue, self.sat, self.bw, None) cv.Copy(self.small, self.visualize) presentation.append((self.visualize, 'input')) face = self.find_face(self.small) if face: sub_face = self.face_region(face, FACE_BORDER) self.update_histogram(sub_face) self.draw_face(self.visualize, face, sub_face) hue_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1) sat_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1) (x, y, w, h) = face cv.Copy(self.hue, hue_bg) cv.Copy(self.sat, sat_bg) cv.Rectangle(hue_bg, (x, y), (x + w, y + h), 0, cv.CV_FILLED) cv.Rectangle(sat_bg, (x, y), (x + w, y + h), 0, cv.CV_FILLED) cv.CalcArrHist([self.hue, self.sat], self.bg_hist, 1) cv.NormalizeHist(self.bg_hist, 255) bp_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1) cv.CalcArrBackProject([self.hue, self.sat], bp_bg, self.bg_hist) self.normalize(bp_bg) presentation.append((bp_bg, 'background bp')) bp = self.backproject() presentation.append((bp, 'forground bp')) self.normalize(bp) compare = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1) compare_th = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1) cv.Cmp(bp, bp_bg, compare, cv.CV_CMP_GT) #cv.AddS(bp_bg, 1, bp_bg) #cv.Div(bp, bp_bg, compare) self.normalize(compare) cv.Threshold(compare, compare_th, self.threshold_value, 255, cv.CV_THRESH_BINARY) presentation.append((compare_th, 'compare')) th = self.threshold(bp) presentation.append((th, 'normal th')) morphed = self.morphology(th) # make dark copy of original cv.Copy(self.small, self.result) cv.ConvertScale(self.result, self.result, 0.2) cv.Copy(self.small, self.result, morphed) contours = self.find_contours(morphed) self.draw_contours(self.result, contours) limbs = self.find_limbs(contours) limbs = self.sort_limbs(limbs) self.draw_limbs(self.result, limbs) presentation.append((self.result, 'result')) self.make_sound(limbs) # combine and show the results combined = self.combine_images(presentation) if face: sub_face = self.face_region(face, FACE_BORDER) self.draw_face(self.result, face, sub_face) cv.ShowImage('Skin Detection', combined) if STORE: cv.WriteFrame(self.writer, self.combined)
exit(1) # draw face faceRect = faces[0][0] face_center = (faceRect[0] + faceRect[2]/2, faceRect[1] + faceRect[3]/2) faceSubRect = sub_region(faceRect) (x, y, w, h) = faceRect cv.Rectangle(frameShow, (x,y), (x+w,y+h), cv.Scalar(0, 0, 255), 3) (x, y, w, h) = faceSubRect cv.Rectangle(frameShow, (x,y), (x+w,y+h), cv.Scalar(0, 255, 255), 3) #calculate histogram cv.SetImageROI(frameH, faceSubRect) cv.SetImageROI(frameS, faceSubRect) cv.CalcArrHist([frameH, frameS], hist, 0) # turn this on or off: #cv.NormalizeHist(hist, 1) cv.NormalizeHist(hist, 5000) cv.ResetImageROI(frameH) cv.ResetImageROI(frameS) #convert histogram to image histImg = cv.GetMat(hist.bins, True) #make backprojection cv.CalcArrBackProject([frameH, frameS], frameBP, hist) cv.Normalize(frameBP, frameBP, 0, 255, 32)