def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = True 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) cv.CalcArrBackProject([self.hue], backproject, hist) # Run the cam-shift (if the a window is set and != 0) 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) #Call the camshift !! self.track_window = rect #Put the current rectangle as the tracked area # If mouse is pressed, highlight the current selected rectangle and recompute histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) #Get specified area #Make the effect of background shadow when selecting a window save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) #Draw temporary rectangle x, y, w, h = self.selection cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) #Take the same area but in hue image to calculate histogram sel = cv.GetSubRect(self.hue, self.selection) cv.CalcArrHist([sel], hist, 0) #Used to rescale the histogram with the max value (to draw it later on) (_, 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): #If window set draw an elipseBox cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) cv.ShowImage("CamShiftDemo", frame) cv.ShowImage("Backprojection", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) % 0x100 if c == 27: break
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False while True: frame = cv.QueryFrame(self.capture) cv.Flip(frame, frame, 0) # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, frame, cv.CV_RGB2BGR) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.CvtColor(frame, hsv, cv.CV_RGB2HSV) 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), (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: cv.ShowImage("CamShiftDemo", frame) else: cv.ShowImage("CamShiftDemo", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) % 0x100 if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode
def detect_and_draw(self, imgmsg): if self.pause: return # frame = cv.QueryFrame( self.capture ) frame = self.br.imgmsg_to_cv(imgmsg, "bgr8") # 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, 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 x, y, w, h = rect self.bbpub.publish(RegionOfInterest(x, y, w, h, False)) proba_msg = self.br.cv_to_imgmsg(backproject) proba_msg.header = imgmsg.header self.bppub.publish(proba_msg) # 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], 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(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) self.frame = frame self.backproject = backproject
def camshift(x, y, w, h, selection): print "Performing camshift with x:{} y:{} w:{} h:{}".format(x, y, w, h) print selection hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) while True: print "entered loop" #camshift termination criteria (10 iterations without movement of 1 pixel ends camshift) frame = cv.QueryFrame(cap) cv.Flip(frame, frame, 1) #print "switching to HSV" 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) #compute back projection # print "back projection" backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CalcArrBackProject([hue], backproject, hist) #run the camshift #print "camshift" print "Selection" #pdb.set_trace() print selection crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, selection, crit) print "rect" print rect if rect[0] > 0 and rect[1] > 0: selection = rect print "SelectionNew" print selection print "track_box" print track_box #draw the surrounding ellipse # print "ellipse" cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) #draw image #print "drawing image" cv.ShowImage("CamShift", frame) if cv.WaitKey(1) & 0xFF == ord('q'): break
def camshift(x, y, w, h): global numLoops global cap for x in range(0, numLoops): frame = cv.QueryFrame(cap) track_window = (x, y, w, h) crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, track_window, crit) try: #prints the center x and y value of the tracked ellipse coord = track_box[0] print "center = {}".format(coord) except UnboundLocalError: print "track_box is None" cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0)
def graphShape(ellipBox, grid, ratio, graph=None, label="", color=None, flags=0): # 1 = image # 2 = circle # 4 = ellipse # 8 = other graph ellipse = circle = image = writeOther = False if (flags & 0x01) != 0: image = True if (flags & 0x02) != 0: ellipse = True if (flags & 0x04) != 0: circle = True if (flags & 0x08) != 0: writeOther = True (cent, size, angle) = ellipBox w, l = size circum = circumference(l / 2, w / 2) d = circum / np.pi excelEllipeBox = (tuple(np.divide(cent, ratio)), tuple(np.round(np.divide(size, ratio))), angle) circBox = (tuple(np.divide(cent, ratio)), tuple(np.round((d / ratio, d / ratio))), angle) th = np.linspace(STARTANG, STARTANG + 2 * np.pi, np.round(circum / ratio * 2)) thet, h = mkPeriod(th, circBox, grid) f = interpolate.interp1d( np.concatenate( (thet - 2 * np.pi, thet, thet + 2 * np.pi, thet + 4 * np.pi)), np.concatenate((h, h, h, h))) writeCircHeight = f(writeTheta) thet, h = mkPeriod(th, excelEllipeBox, grid) f = interpolate.interp1d( np.concatenate( (thet - 2 * np.pi, thet, thet + 2 * np.pi, thet + 4 * np.pi)), np.concatenate((h, h, h, h))) writeEllipHeight = f(writeTheta) areaCirc = circum * mmperpixel / len(writeCircHeight) * np.sum( writeCircHeight) areaEllip = circum * mmperpixel / len(writeEllipHeight) * np.sum( writeEllipHeight) r, g, b = color ovalR, ovalG, ovalB = 255 - r, 255 - g, 255 - b if image is True: cv.Circle(imgColor, tuple(np.around(cent).astype('int')), int(d / 2), color=cv.RGB(r, g, b)) cv.EllipseBox(imgColor, ellipBox, cv.RGB(ovalR, ovalG, ovalB)) #dataWriter.writerow(np.around(circHeight,decimals=3)) #dataWriter.writerow(np.around(ellipHeight,decimals=3)) #np.savetxt(dataFile,circHeight,fmt='%.4f',newline=',',delimiter='\n') #np.savetxt(dataFile,ellipHeight,fmt='%.4f',newline=',',delimiter='\n') #f = interpolate.interp1d(th,circHeight-ellipHeight) #diffHeight = f(th) dataFile.write( "%s,%s,%.0fx%.0f,%.3f,%s\n" % (label, 'ellipse', l * mmperpixel * 1000, w * mmperpixel * 1000, areaEllip, fmtCSVArr(writeEllipHeight[1:]))) dataFile.write( "%s,%s,%.0fx%.0f,%.3f,%s\n" % (label, 'circle', d * mmperpixel * 1000, d * mmperpixel * 1000, areaCirc, fmtCSVArr(writeCircHeight[1:]))) if writeOther is True: tsnitGraph.plot(np.linspace(0, 360, len(writeCircHeight)), writeCircHeight, color=(r / 255., g / 255., b / 255.), label=(label + " cir AUC " + str(round(areaCirc / 1000, 3)) + " mm^2")) if ellipse is True: #dataFile.write(label+','++','+fmtCSVArr(writeEllipHeight)+"\n") graph.plot(np.linspace(0, 360, len(writeEllipHeight)), writeEllipHeight, color=(ovalR / 255., ovalG / 255., ovalB / 255.), label=(label + " ellip AUC " + str(round(areaEllip / 1000, 3)) + " mm^2")) if circle is True: #dataFile.write(label+','+'%.0fx%.0f'%()+','+fmtCSVArr(writeCircHeight)+"\n") graph.plot(np.linspace(0, 360, len(writeCircHeight)), writeCircHeight, color=(r / 255., g / 255., b / 255.), label=(label + " cir AUC " + str(round(areaCirc / 1000, 3)) + " mm^2"))
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False i = 1 o_x = 0 o_y = 0 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), (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) #print track_box trace_val = track_box[0] f_x = trace_val[0] f_y = trace_val[1] print 'value1', f_x print 'value2', f_y if i % 10 == 0: o_x = f_x o_y = f_y if (f_x != o_x): a = (f_x - o_x) / float(10) round(a) cam.Azimuth(-a) if (f_y != o_y): a = (f_y - o_y) / float(10) round(a) cam.Elevation(-a) ren1.ResetCameraClippingRange() renWin.Render() i += 1 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(7) % 0x100 if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False print "hitting run section" x = 0 while True: #print x #x = x + 1 frame = cv.QueryFrame(self.capture) cv.Flip(frame, frame, 1) # 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) print self.track_window (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect print self.track_window try: #prints the center x and y value of the tracked ellipse coord = track_box[0] print "center = {}".format(coord) if (coord[0] < 320): print "move right" # ser.write("R") elif (coord[0] == 320): print "do nothing" else: print "move left" # ser.write("L") except UnboundLocalError: print "track_box is None" # 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): print track_box cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) 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(7) % 0x100 if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode