示例#1
0
文件: lrf.py 项目: Mnemonic7/lrf
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
示例#2
0
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;
示例#3
0
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
示例#4
0
    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("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
        cv.Line(imgScribble, (int(posx), int(posy)), (int(lastx), int(lasty)),
    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()
示例#7
0
        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)

            #			cv.Merge(thr,None,None,None,frame)
            if (int(x) < 130):
                value = 130 - int(x)
                cv.PutText(frame, "Left[" + str(value) + "]", (int(x), int(y)),
                           font, (255, 255, 0))

                x_count += 1