Exemple #1
0
def hsvProceed(img):
    single = cv.CreateImage(cv.GetSize(img), 8, 1)
    single_c = cv.CreateImage(cv.GetSize(img), 8, 1)
    width = 0
    height = 0
    while (1):
        while (1):
            if (img[height, width][0] >
                    70) and (img[height, width][0] <
                             95) and (img[height, width][1] > 53) and (
                                 img[height, width][1] <
                                 230):  #and(img[height,width][2]<200):
                single[height, width] = 255
                single_c[height, width] = 255
            else:
                single[height, width] = 0
                single_c[height, width] = 0
            height = height + 1
            if (height == 480):
                height = 0
                break
        width = width + 1
        if (width == 640):
            break
    cv.Erode(single, single)
    cv.Dilate(single, single)
    cv.Erode(single_c, single_c)
    cv.Dilate(single_c, single_c)
    # cv.ShowImage("rgb",single)
    # cv.WaitKey()
    return single, single_c
Exemple #2
0
    def processImage(self, curframe):
        cv.Smooth(curframe, curframe)  #Remove false positives

        if not self.absdiff_frame:  #For the first time put values in difference, temp and moving_average
            self.absdiff_frame = cv.CloneImage(curframe)
            self.previous_frame = cv.CloneImage(curframe)
            cv.Convert(
                curframe, self.average_frame
            )  #Should convert because after runningavg take 32F pictures
        else:
            cv.RunningAvg(curframe, self.average_frame,
                          0.05)  #Compute the average

        cv.Convert(self.average_frame,
                   self.previous_frame)  #Convert back to 8U frame

        cv.AbsDiff(curframe, self.previous_frame,
                   self.absdiff_frame)  # moving_average - curframe

        cv.CvtColor(
            self.absdiff_frame, self.gray_frame,
            cv.CV_RGB2GRAY)  #Convert to gray otherwise can't do threshold
        cv.Threshold(self.gray_frame, self.gray_frame, 50, 255,
                     cv.CV_THRESH_BINARY)

        cv.Dilate(self.gray_frame, self.gray_frame, None,
                  15)  #to get object blobs
        cv.Erode(self.gray_frame, self.gray_frame, None, 10)
Exemple #3
0
def hsvProceed(img, camID):
    single = cv.CreateImage(cv.GetSize(img), 8, 1)
    single_c = cv.CreateImage(cv.GetSize(img), 8, 1)
    if camID == 0:
        v_min = 120
    if camID == 1:
        v_min = 100
    width = 0
    height = 0
    while (1):
        while (1):
            if (img[height, width][0] >
                    95) and (img[height, width][0] <
                             140) and (img[height, width][1] > 40) and (
                                 img[height, width][1] <
                                 220) and (img[height, width][2] > v_min):
                single[height, width] = 255.0
                single_c[height, width] = 255.0
            else:
                single[height, width] = 0.0
                single_c[height, width] = 0.0
            height = height + 1
            if (height == 480):
                height = 0
                break
        width = width + 1
        if (width == 640):
            break
    cv.Erode(single, single)
    cv.Dilate(single, single)
    # cv.Erode(single_c, single_c)
    # cv.Dilate(single_c, single_c)
    # cv.ShowImage("rgb",single)
    # cv.WaitKey()
    return single, single_c
Exemple #4
0
def hsvProceed(img, camID):  #img为一三通道的HSV图像,camID
    #初始化
    v_min = 0
    h_max = 0
    h_min = 0
    s_max = 0
    s_min = 0
    width = 0
    height = 0
    #创建两个单通道图像
    single = cv2.CreateImage(cv2.GetSize(img), 8, 1)
    single_c = cv2.CreateImage(cv2.GetSize(img), 8, 1)
    #为不同的摄像头分别设置h,s,v的阈值区间
    if camID == 0:
        v_min = 120
        h_max = 138
        h_min = 95
        s_max = 256
        s_min = 30
    if camID == 1:
        v_min = 10
        h_max = 140
        h_min = 95
        s_max = 256
        s_min = 30

# 提取红色部分到single,组建单通道图像

    while (1):
        while (1):
            if (img[height, width][0] >=
                    h_min) and (img[height, width][0] < h_max) and (
                        img[height, width][1] >=
                        s_min) and (img[height, width][1] < s_max) and (
                            img[height, width][2] >= v_min):  #筛选红色部分
                single[height, width] = 255.0
                single_c[height, width] = 255.0
            else:
                single[height, width] = 0.0
                single_c[height, width] = 0.0
            height = height + 1
            if (height == 480):
                height = 0
                break
        width = width + 1
        if (width == 640):
            break
#形态学处理
    cv2.Erode(single, single)
    cv2.Dilate(single, single)

    return single, single_c
    def applyEffect(self, image, width, height):
        ipl_img = cv2.cv.CreateImageHeader((image.shape[1], image.shape[0]), cv.IPL_DEPTH_8U, 3)
        cv2.cv.SetData(ipl_img, image.tostring(), image.dtype.itemsize * 3 * image.shape[1])

        gray = cv.CreateImage((width, height), 8, 1)  # tuple as the first arg

        dst_img = cv.CreateImage(cv.GetSize(ipl_img), cv.IPL_DEPTH_8U, 3)  # _16S  => cv2.cv.iplimage
        if self.effect == 'dilate':
            cv.Dilate(ipl_img, dst_img, None, 5)
        elif self.effect == 'laplace':
            cv.Laplace(ipl_img, dst_img, 3)
        elif self.effect == 'smooth':
            cv.Smooth(ipl_img, dst_img, cv.CV_GAUSSIAN)
        elif self.effect == 'erode':
            cv.Erode(ipl_img, dst_img, None, 1)

        cv.Convert(dst_img, ipl_img)
        return self.ipl2tk_image(dst_img)
Exemple #6
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)   
Exemple #7
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
Exemple #8
0
def dilateImage(im, nbiter=0):
    for i in range(nbiter):
        cv.Dilate(im, im)
Exemple #9
0
def Dilation(pos):
    element = cv.CreateStructuringElementEx(pos * 2 + 1, pos * 2 + 1, pos, pos,
                                            element_shape)
    cv.Dilate(src, dest, element, 1)
    cv.ShowImage("Erosion & Dilation", dest)
Exemple #10
0
def Closing(pos):
    element = cv.CreateStructuringElementEx(pos * 2 + 1, pos * 2 + 1, pos, pos,
                                            element_shape)
    cv.Dilate(src, image, element, 1)
    cv.Erode(image, dest, element, 1)
    cv.ShowImage("Opening & Closing", dest)
Exemple #11
0
            if (img[height,width][0] >= h_min)and(img[height,width][0] < h_max)and(img[height,width][1] >= s_min)and(img[height,width][1] < s_max)and(img[height,width][2]>=v_min):#筛选红色部分
                single[height,width] = 255.0
                single_c[height, width] = 255.0
            else:
                single[height, width] = 0.0
                single_c[height, width] = 0.0
            height =height+1
            if(height==480):
                height=0
                break
        width=width+1
        if(width==640):
            break
	#形态学处理
    cv2.Erode(single,single)
    cv2.Dilate(single, single)
   
    return single,single_c
#找红球的主要逻辑部分
def findball(IP,PORT):
  
    camID = 1 #预设调用下摄像头

    count = 0 
    #优先使用下摄像头找三次,若找到返回其在图像中的位置以及camID,否则调用上摄像头找三次,找到后返回其在图像中的位置以及camID
    while True:
      count = count + 1
      SIZE,LEFT,TOP = showNaoImage(IP, PORT,camID)
      if (SIZE==0)&(LEFT==0)&(TOP==0):
          if (count==3):
              if camID==0: