Ejemplo n.º 1
0
def DotCount():
    Red_Dot = IsolateRedDot()
    DotArea = cv.ContourArea(contours[0])
    DotFrame = cv.boundingRect(contours[0])
    FrameArea = (DotFrame[1] - DotFrame[0]) * (DotFrame[2] - DotFrame[1])
    DotCount = int(FrameArea / DotArea)
    return DotCount
 def update(self, image):
     locs = []
     if self.avg is None:
         self.avg = image.astype("float")
         return locs
     cv2.accumWeighted(image, self.avg, self.accumWeight)
     diff = cv2.absdiff(image, cv2.convertScaleAbs(self.avg))
     thresh = cv2.threshold(image, self.delta, 255, cv2.THRESH_BINARY)[1]
     thresh = cv2.dilate(thresh, None, iterations=2)
     cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                             cv2.CHAIN_APPROX_SIMPLE)
     for c in cnts:
         if cv2.ContourArea(c) > self.minArea:
             locs.append(c)
     return locs
Ejemplo n.º 3
0
 def somethingHasMoved(self):
     # Find contours
     storage = cv.CreateMemStorage(0)
     contours = cv.FindContours(self.gray_frame, storage,
                                cv.CV_RETR_EXTERNAL,
                                cv.CV_CHAIN_APPROX_SIMPLE)
     self.currentcontours = contours  # Save contours
     while contours:  # For all contours compute the area
         self.currentsurface += cv.ContourArea(contours)
         contours = contours.h_next()
     avg = (
         self.currentsurface * 100
     ) / self.surface  # Calculate the average of contour area on the total size
     self.currentsurface = 0  # Put back the current surface to 0
     if avg > self.threshold:
         return True
     else:
         return False
Ejemplo n.º 4
0
def marker_points(image):

    stamp = time.time()

    ## convert rgb to grayscale and blur it
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray_image = cv2.GaussianBlur(gray_image, (11,11),0)   
    (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray_image)  

    ##threshold the image to reveal light regions in the blurred image
    thresh = cv2.threshold(gray_image,120,255,cv2.THRESH_BINARY)[1]
    ## perform a series of erosions and dilations to remove any small blobs of noise from the thresholded image
    cv2.Dilate(thresh, thresh, None, 18)  
    cv2.Erode(thresh, thresh, None, 10)
    thr = thresh.copy()

    #Create Kalman Filter
    kalman = cv2.CreateKalman(4,2,0)
    kalman_state = cv2.CreateMat(4, 1, cv2.CV_32FC1)
    kalman_process_noise = cv2.CreateMat(4, 1, cv2.CV_32FC1)
    kalman_measurement = cv2.CreateMat(2, 1, cv2.CV_32FC1)

    cp1 = []
    cp11 = []
    points = []
    
    #find the bright contours 
    (_,cnts, _) = cv2.findContours(thr, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    print "Found %d LEDs" % (len(cnts))

        
    while cnts:
        ##find area and compute the blob centers 
        area=cv2.ContourArea(list(cnts))
        
        for c in range(len(cnts)):
            M = cv2.moments(cnts[c])
            cp1 = (int(M["m10"]/M["m00"]),int(M["m01"]/M["m00"]))
            cp11.append(cp1)
        

            # set previous state for prediction
            kalman.state_pre[0,0]  = cp1
            kalman.state_pre[1,0]  = 0
           
            # set Kalman Filter
            cv2.SetIdentity(kalman.measurement_matrix, cv2.RealScalar(1))
            cv2.SetIdentity(kalman.process_noise_cov, cv2.RealScalar(1e-5))
            cv2.SetIdentity(kalman.measurement_noise_cov, cv2.RealScalar(1e-1))
            cv2.SetIdentity(kalman.error_cov_post, cv2.RealScalar(1))

            #Prediction
            kalman_prediction = cv2.KalmanPredict(kalman)
            predict_pt  = (int(kalman_prediction[0,0]),int( kalman_prediction[1,0]))
            predict_pt1.append(predict_pt)
            
            #Correction
            kalman_estimated = cv2.KalmanCorrect(kalman, kalman_measurement)
            state_pt = (kalman_estimated[0,0], kalman_estimated[1,0])

            #Measurement
            kalman_measurement[0, 0] = cp11[0]
            
    
    if len(cnts)==6: 
        #draw circle around brightest blobs        
        #for i in cnts:
            #cv2.circle(image, (i[1],i[0]), 3, (0, 0, 255), -1)
            #cv2.imshow("IMAGE", image) 
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        #return np.array(pixels,dtype = "double")
        return np.array(cp11,dtype = "double")
        
    elif len(cnts)>6:
        print ("No marker")
        return marker_points(image)
    else:
        print("No marker")
        return marker_points(image)   
Ejemplo n.º 5
0
def choose_land(img,rect,contours):
    white_l = 0.0
    black_l = 0.0
    white_r = 0.0
    black_r = 0.0
    white_u = 0.0
    black_u = 0.0
    white_d = 0.0
    black_d = 0.0
    white_black = 0
    area_land = 0.0
    area_land = cv2.ContourArea(contours)
    rect_center_x = rect[0] + rect[2] / 2
    rect_center_y = rect[1] + rect[3] / 2
    center_value = img[rect_center_y, rect_center_x][0]
    left_up = img[rect[1], rect[0]][0]
    left_down = img[rect[1] + rect[3], rect[0]][0]
    right_up = img[rect[1], rect[0] + rect[2]][0]
    right_down = img[rect[1] + rect[3], rect[0] + rect[2]][0]
    fourpoints = left_down + left_up + right_down + right_up
    Area = float(rect[2])*rect[3]
    for i in range(rect[0], (rect[0] + rect[2] / 2), 1):
        for j in range(rect[1], rect[1] + rect[3], 1):
            if img[j, i][0] == 255.0:
                white_l = white_l + 1
            else:
                black_l = black_l + 1

    for i in range((rect[0] + rect[2] / 2), (rect[0] + rect[2]), 1):
        for j in range(rect[1], rect[1] + rect[3], 1):
            if img[j, i][0] == 255.0:
                white_r = white_r + 1
            else:
                black_r = black_r + 1

    for i in range(rect[0], (rect[0] + rect[2]), 1):
        for j in range(rect[1], (rect[1] + rect[3] / 2), 1):
            if img[j, i][0] == 255.0:
                white_u = white_u + 1
            else:
                black_u = black_u + 1

    for i in range(rect[0], (rect[0] + rect[2]), 1):
        for j in range((rect[1] + rect[3] / 2), (rect[1] + rect[3]), 1):
            if img[j, i][0] == 255.0:
                white_d = white_d + 1
            else:
                black_d = black_d + 1
	#根据黑白像素比筛选,大于0.5,小于1.5
    white = (white_r + white_l + white_d + white_u) / 2
    black = (black_d + black_u + black_r + black_l) / 2
    radio_b_w = black/white
    if radio_b_w > 0.5:
        if radio_b_w < 1.5:
          white_black = 1

    p = area_land/Area #p值为矩形框的面积与球的圆形面积的比值
    if p > 0.65:
        if p < 0.88:
            p = 1
    R1 = area_land/3.14159
    #R1 = math.sqrt(R1)
    R2 = (rect[2]*rect[3])/4
    R = R1 - R2
    R = abs(R)  #R为圆形的半径与矩形框边长的一半的差值
    if R < 60:
        R = 1

    ret = 0
    if white_black == 1:
     if p == 1:
        if center_value == 255.0:
           if fourpoints >1000 :
             if R == 1:
                ret = 1

    return ret
Ejemplo n.º 6
0
    def run(self):
        # Capture first frame to get size
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        width = frame.width
        height = frame.height
        surface = width * height  #Surface area of the image
        cursurface = 0  #Hold the current surface that have changed

        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
        moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)
        difference = None

        while True:
            color_image = cv.QueryFrame(self.capture)

            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3,
                      0)  #Remove false positives

            if not difference:  #For the first time put values in difference, temp and moving_average
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
            else:
                cv.RunningAvg(color_image, moving_average, 0.020,
                              None)  #Compute the average

            # Convert the scale of the moving average.
            cv.ConvertScale(moving_average, temp, 1.0, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, difference)

            #Convert the image so that it can be thresholded
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            cv.Dilate(grey_image, grey_image, None, 18)  #to get object blobs
            cv.Erode(grey_image, grey_image, None, 10)

            # Find contours
            storage = cv.CreateMemStorage(0)
            contours = cv.FindContours(grey_image, storage,
                                       cv.CV_RETR_EXTERNAL,
                                       cv.CV_CHAIN_APPROX_SIMPLE)

            backcontours = contours  #Save contours

            while contours:  #For all contours compute the area
                cursurface += cv.ContourArea(contours)
                contours = contours.h_next()

            avg = (
                cursurface * 100
            ) / surface  #Calculate the average of contour area on the total size
            if avg > self.ceil:
                print("Something is moving !")
            #print avg,"%"
            cursurface = 0  #Put back the current surface to 0

            #Draw the contours on the image
            _red = (0, 0, 255)
            #Red for external contours
            _green = (0, 255, 0)
            # Gren internal contours
            levels = 1  #1 contours drawn, 2 internal contours as well, 3 ...
            cv.DrawContours(color_image, backcontours, _red, _green, levels, 2,
                            cv.CV_FILLED)

            cv.ShowImage("Target", color_image)

            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break