def calculate_laser_position(img): print "calculating moments" moments = cv.Moments(cv.GetMat(img)) posX = -1 posY = -1 area = cv.GetCentralMoment(moments, 0, 0) if area > 0: moment10 = cv.GetSpatialMoment(moments, 1, 0) moment01 = cv.GetSpatialMoment(moments, 0, 1) posX = int(moment10 / area) posY = int(moment01 / area) return posX, posY, area
def main(): # captured image size, change to whatever you want width = 320 height = 240 capture = cv.CreateCameraCapture(0) # Over-write default captured image size cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width) cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) cv.NamedWindow( "output", 1 ) cv.NamedWindow( "processed", 1 ) while True: frame = cv.QueryFrame(capture) cv.Smooth(frame, frame, cv.CV_BLUR, 3) imgColorProcessed = ColorProcess(frame) mat = cv.GetMat(imgColorProcessed) # Calculating the moments moments = cv.Moments(mat, 0) area = cv.GetCentralMoment(moments, 0, 0) moment10 = cv.GetSpatialMoment(moments, 1, 0) moment01 = cv.GetSpatialMoment(moments, 0,1) # Finding a big enough blob if(area > 60000): # Calculating the center postition of the blob posX = int(moment10 / area) posY = int(moment01 / area) # check slave status and send coordinates state = readData() if state == 1: sendData(posX) sendData(posY) print 'x: ' + str(posX) + ' y: ' + str(posY) # update video windows cv.ShowImage("processed", imgColorProcessed) cv.ShowImage("output", frame) if cv.WaitKey(10) >= 0: break return;
def drawCircles(cont,img): li = None if len(cont) != 0: moments = cv.Moments(cont,1) moments10 = cv.GetSpatialMoment(moments,1,0) moments01 = cv.GetSpatialMoment(moments,0,1) area = cv.GetCentralMoment(moments,0,0) if area > 0: x=(int)(moment10/PnB.area) y=(int)(moment01/PnB.area) rad = (int)(math.ceil(math.sqrt(PnB.area/3.14))) cv.Circle(img,(x,y),rad,(255,255,255,0),1,8,0) li = [x,y,img,area] return li
def run(self): while True: img = cv.QueryFrame(self.capture) #blur the source image to reduce color noise cv.Smooth(img, img, cv.CV_BLUR, 3) #convert the image to hsv(Hue, Saturation, Value) so its #easier to determine the color to track(hue) hsv_img = cv.CreateImage(cv.GetSize(img), 8, 3) cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV) #limit all pixels that don't match our criteria, in this case we are #looking for purple but if you want you can adjust the first value in #both turples which is the hue range(120,140). OpenCV uses 0-180 as #a hue range for the HSV color model thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) cv.InRangeS(hsv_img, (120, 80, 80), (140, 255, 255), thresholded_img) #determine the objects moments and check that the area is large #enough to be our object moments = cv.Moments(cv.GetMat(thresholded_img), 0) area = cv.GetCentralMoment(moments, 0, 0) #there can be noise in the video so ignore objects with small areas if (area > 10000): #determine the x and y coordinates of the center of the object #we are tracking by dividing the 1, 0 and 0, 1 moments by the area x = cv.GetSpatialMoment(moments, 1, 0) / area y = cv.GetSpatialMoment(moments, 0, 1) / area #print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area) #create an overlay to mark the center of the tracked object overlay = cv.CreateImage(cv.GetSize(img), 8, 3) cv.Circle(overlay, (int(x), int(y)), 2, (255, 255, 255), 20) #cv.Add(img, overlay, img) #add the thresholded image back to the img so we can see what was #left after it was applied #cv.Merge(thresholded_img, None, None, None, img) #display the image cv.ShowImage(color_tracker_window, img) if cv.WaitKey(10) == 27: break
cv.NamedWindow("video") cv.NamedWindow("thresh") tmp = cv.QueryFrame(capture) imgScribble = cv.CreateImage(cv.GetSize(tmp), 8, 3) #Image that will contain lines posx = 0 posy = 0 while True: frame = cv.QueryFrame(capture) imgYellowTresh = getThresholdImage(frame) #Apply the threshold function moments = cv.Moments(cv.GetMat(imgYellowTresh), 1) moment10 = cv.GetSpatialMoment(moments, 1, 0) moment01 = cv.GetSpatialMoment(moments, 0, 1) area = cv.GetCentralMoment(moments, 0, 0) #Get the center lastx = posx lasty = posy if area == 0: posx = 0 posy = 0 else: posx = moment10 / area posy = moment01 / area if lastx > 0 and lasty > 0 and posx > 0 and posy > 0: #Mean we have received coordinates to print #Draw the line
import cv2.cv as cv import math import time N=50000 print "leak4" while True: seq=list((i*1., i*1.) for i in range(N)) cv.Moments(seq)
def run(self): initialTime = 0. #sets the initial time num_Frames = int( cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_COUNT)) fps = cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS) for ii in range(num_Frames - 8): print('Frame: ' + str(ii) + ' of ' + str(num_Frames)) # read the ii-th frame img = cv.QueryFrame(self.capture) # Blur the source image to reduce color noise cv.Smooth(img, img, cv.CV_BLUR, 3) # Convert the image to hsv(Hue, Saturation, Value) so its # It's easier to determine the color to track(hue) hsv_img = cv.CreateImage(cv.GetSize(img), 8, 3) cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV) # limit all pixels that don't match our criteria, in the is case we are # looking for purple but if you want you can adjust the first value in # both turples which is the hue range(120,140). OpenCV uses 0-180 as # a hue range for the HSV color model thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) # uncomment below for tracking blue # cv.InRangeS(hsv_img, (112, 50, 50), (118, 200, 200), thresholded_img) # tracking red cv.InRangeS(hsv_img, (160, 150, 100), (180, 255, 255), thresholded_img) #determine the objects moments and check that the area is large #enough to be our object thresholded_img2 = cv.GetMat(thresholded_img) moments = cv.Moments(thresholded_img2, 0) area = cv.GetCentralMoment(moments, 0, 0) # there can be noise in the video so ignore objects with small areas if (area > 2500): #determine the x and y coordinates of the center of the object #we are tracking by dividing the 1, 0 and 0, 1 moments by the area x = cv.GetSpatialMoment(moments, 1, 0) / area y = cv.GetSpatialMoment(moments, 0, 1) / area elapsedTime = ii / fps f.write( str(elapsedTime) + ',' + '%013.9f' % x + ',' + '%013.9f' % y + "\n" ) #prints output to the specified output file for later use # # x = int(x) # y = int(y) # # #create an overlay to mark the center of the tracked object # overlay = cv.CreateImage(cv.GetSize(img), 8, 3) # # cv.Circle(overlay, (x, y), 2, (255, 255, 255), 20) # cv.Add(img, overlay, img) # #add the thresholded image back to the img so we can see what was # #left after it was applied # cv.Merge(thresholded_img, None, None, None, img) # # #display the image # cv.ShowImage(color_tracker_window, img) # close the data file f.close()
cv.WaitKey(0) break cv.Smooth(frame, frame, cv.CV_BLUR, 3) hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) thr = cv.CreateImage(cv.GetSize(frame), 8, 1) #Change threshold value cv.InRangeS(hsv, (h1, s1, v1), (h2, s2, v2), thr) moments = cv.Moments(cv.GetMat(thr, 1), 0) area = cv.GetCentralMoment(moments, 0, 0) cv.Line(frame, (160, 0), (160, 240), (0, 0, 255), 3, 8, 0) if (area > 10000): x = cv.GetSpatialMoment(moments, 1, 0) / area y = cv.GetSpatialMoment(moments, 0, 1) / area # overlay = cv.CreateImage(cv.GetSize(frame),8,3) cv.Circle(frame, (int(x), int(y)), 2, (255, 255, 255), 20) # cv.Add(frame,overlay,frame)
f = cv2.flip(f, 1) blur = cv2.medianBlur(f, 5) hsv = cv2.cvtColor(f, cv2.COLOR_BGR2HSV) color = getthresholdedimg(hsv) ## erode = cv2.erode(color,None,iterations = 3) ## dilate = cv2.dilate(erode,None,iterations = 10) thresh = cv2.inRange(hsv, np.array(tume), np.array(hele)) #determine the objects moments and check that the area is large #enough to be our object tr = cv.fromarray(thresh) F = cv.fromarray(f) moments = cv.Moments(tr, 0) area = cv.GetCentralMoment(moments, 0, 0) #there can be noise in the video so ignore objects with small areas if (area > 1000): #determine the x and y coordinates of the center of the object #we are tracking by dividing the 1, 0 and 0, 1 moments by the area x = cv.GetSpatialMoment(moments, 1, 0) / area y = cv.GetSpatialMoment(moments, 0, 1) / area ## print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area) #create an overlay to mark the center of the tracked object overlay = cv.CreateImage(cv.GetSize(F), 8, 3) cv.Circle(overlay, (int(x), int(y)), 2, (255, 255, 255), 20)