Ejemplo n.º 1
0
def diff(img1, img2, result):
    im1 = cv.LoadImage(img1, 1)
    im2 = cv.LoadImage(img2, 1)

    mat1 = cv.GetMat(im1)
    mat2 = cv.GetMat(im2)

    a = numpy.asarray(mat1)

    diff = cv.CloneMat(mat1)
    cv.AbsDiff(mat1, mat2, diff)

    c = numpy.asarray(diff)

    difference = c.sum(axis=2)
    differenceNonZeroes = difference.nonzero()
    res = float(differenceNonZeroes[0].size) / difference.size

    diffNonZeroes = c.nonzero()
    positions = []
    for pos in range(len(diffNonZeroes[0])):
        for dim in range(len(diffNonZeroes)):
            positions.append(diffNonZeroes[dim][pos])
        a[positions[0]][positions[1]] = [0, 0, 255]
        positions = []

    cv.SaveImage(result, cv.fromarray(a))

    return res
Ejemplo n.º 2
0
def update_mhi(img, dst, diff_threshold):
    global last
    global mhi
    global mask
    timestamp = time.clock() / CLOCKS_PER_SEC  # get current time in seconds
    size = cv.GetSize(img)  # get current frame size
    idx1 = last
    if not mhi or cv.GetSize(mhi) != size:
        for i in range(N):
            buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
            cv.Zero(buf[i])
        mhi = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
        cv.Zero(mhi)  # clear MHI at the beginning
        mask = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)

    cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY)  # convert frame to grayscale
    idx2 = (last + 1) % N  # index of (last - (N-1))th frame
    last = idx2
    silh = buf[idx2]
    cv.AbsDiff(buf[idx1], buf[idx2], silh)  # get difference between frames
    cv.Threshold(silh, silh, diff_threshold, 1,
                 cv.CV_THRESH_BINARY)  # and threshold it
    cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION)  # update MHI
    cv.CvtScale(mhi, mask, 255. / MHI_DURATION,
                (MHI_DURATION - timestamp) * 255. / MHI_DURATION)
    cv.Zero(dst)
    cv.Merge(mask, None, None, None, dst)
Ejemplo n.º 3
0
def detect_card(grey_image, grey_base, thresh=100):
    diff = cv.CloneImage(grey_image)
    cv.AbsDiff(grey_image, grey_base, diff)

    edges = cv.CloneImage(grey_image)
    cv.Canny(diff, edges, thresh, thresh)

    contours = cv.FindContours(edges, cv.CreateMemStorage(0))
    edge_pts = []
    c = contours
    while c is not None:
        if len(c) > 10:
            edge_pts += list(c)
        if len(c) == 0:  #'cus opencv is buggy and dumb
            break
        c = c.h_next()

    if len(edge_pts) == 0:
        return None
    hull = cv.ConvexHull2(edge_pts, cv.CreateMemStorage(0), cv.CV_CLOCKWISE, 1)
    lines = longest_lines(hull)
    perim = sum(l['len'] for l in lines)
    #print perim

    #likely to be a card. . .
    #if abs(perim - 1200) < 160:
    if perim > 700:
        #extrapolate the rectangle from the hull.
        #if our 4 longest lines make up 80% of our perimiter
        l = sum(l['len'] for l in lines[0:4])
        #print "l = ",l
        if l / perim > 0.7:
            #we probably have a high-quality rectangle. extrapolate!
            sides = sorted(lines[0:4], key=lambda x: x['angle'])
            #sides are in _some_ clockwise order.
            corners = [None] * 4

            # TODO: figure out why we can get an IndexError on xrange(4)
            try:
                for n in xrange(4):
                    corners[n] = line_intersect(sides[n], sides[(n + 1) % 4])
                if not all(corners):
                    return None
            except IndexError:
                print >> sys.stderr, "detect_card() IndexError(), we should track down why this happens exactly"
                return None
            #rotate corners so top-left corner is first.
            #that way we're clockwise from top-left
            sorted_x = sorted(c[0] for c in corners)
            sorted_y = sorted(c[1] for c in corners)
            top_left = None
            for index, (x, y) in enumerate(corners):
                if sorted_x.index(x) < 2 and sorted_y.index(y) < 2:
                    top_left = index
            if top_left is None:
                return None
            #return rotated list
            return corners[top_left:] + corners[:top_left]

    return None
Ejemplo n.º 4
0
 def get_motion_mask(self, img, diff_threshold=30):
     self.timestamp = time.clock(
     ) / self.CLOCKS_PER_SEC  # get current time in seconds
     size = cv.GetSize(img)  # get current frame size
     idx1 = self.last
     if not self.mhi or cv.GetSize(self.mhi) != size:
         for i in range(self.N):
             self.buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
             cv.Zero(self.buf[i])
         self.mhi = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         cv.Zero(self.mhi)  # clear MHI at the beginning
         self.orient = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         self.segmask = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
         self.mask = cv.CreateImage(size, 8, 1)
         self.test = cv.CreateImage(size, 8, 3)
     cv.CvtColor(img, self.buf[self.last],
                 cv.CV_BGR2GRAY)  # convert frame to grayscale
     #self.buf[self.last] = cv.CloneImage(img)
     idx2 = (self.last + 1) % self.N  # index of (last - (N-1))th frame
     self.last = idx2
     self.silh = self.buf[idx2]
     cv.AbsDiff(self.buf[idx1], self.buf[idx2],
                self.silh)  # get difference between frames
     cv.Threshold(self.silh, self.silh, diff_threshold, 1,
                  cv.CV_THRESH_BINARY)  # and threshold it
     cv.UpdateMotionHistory(self.silh, self.mhi, self.timestamp,
                            self.MHI_DURATION)  # update MHI
     cv.CvtScale(self.mhi, self.mask, 255. / self.MHI_DURATION,
                 (self.MHI_DURATION - self.timestamp) * 255. /
                 self.MHI_DURATION)
     #cv.ShowImage("motion mask", self.mask)
     max_rect = self.segment_motion()
     return self.mask
Ejemplo n.º 5
0
def update_mhi(img, dst, diff_threshold):
    global last
    global mhi
    global storage
    global mask
    global orient
    global segmask
    timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds
    size = cv.GetSize(img) # get current frame size
    idx1 = last
    if not mhi or cv.GetSize(mhi) != size:
        for i in range(N):
            buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
            cv.Zero(buf[i])
        mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        cv.Zero(mhi) # clear MHI at the beginning
        orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1)
        mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1)
    
    cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale
    idx2 = (last + 1) % N # index of (last - (N-1))th frame
    last = idx2
    silh = buf[idx2]
    cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames
    cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it
    cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI
    cv.CvtScale(mhi, mask, 255./MHI_DURATION,
                (MHI_DURATION - timestamp)*255./MHI_DURATION)
    cv.Zero(dst)
    cv.Merge(mask, None, None, None, dst)
    cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3)
    if not storage:
        storage = cv.CreateMemStorage(0)
    seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA)
    for (area, value, comp_rect) in seq:
        if comp_rect[2] + comp_rect[3] > 100: # reject very small components
            color = cv.CV_RGB(255, 0,0)
            silh_roi = cv.GetSubRect(silh, comp_rect)
            mhi_roi = cv.GetSubRect(mhi, comp_rect)
            orient_roi = cv.GetSubRect(orient, comp_rect)
            mask_roi = cv.GetSubRect(mask, comp_rect)
            angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION)

            count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI
            if count < (comp_rect[2] * comp_rect[3] * 0.05):
                continue

            magnitude = 30.
            center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2))
            cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0)
            cv.Line(dst,
                    center,
                    (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)),
                     cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))),
                    color,
                    3,
                    cv.CV_AA,
                    0)
Ejemplo n.º 6
0
def FrameMask(old_frame, frame):

    if MovingHead():
        return None
    mask = cv.CloneImage(old_frame)
    cv.AbsDiff(old_frame, frame, mask)
    cv.Threshold(mask,mask, 15, 255, cv.CV_THRESH_BINARY)

    return mask
Ejemplo n.º 7
0
 def accumulateBackground(self, img):
     cv.CvtScale(img, self.Iscratch, 1, 0)
     if (not self.first):
         cv.Acc(self.Iscratch, self.IavgF)
         cv.AbsDiff(self.Iscratch, self.IprevF, self.Iscratch2)
         cv.Acc(self.Iscratch2, self.IdiffF)
         self.Icount += 1.0
     self.first = 0
     cv.Copy(self.Iscratch, self.IprevF)
Ejemplo n.º 8
0
  def processMotion(self):
    """
    Take a raw input image frame from the camera and perform motion detection using
    the current frame plus considering several previous frames and return the CV
    image that should be given to the Region network.
    """
    #find motion image, then find feature corners from that
    if self._prevIplImage:
      cv.AbsDiff(self._inputImage, self._prevIplImage, self._diffImage)
    else:
      cv.Copy(self._inputImage, self._diffImage)
      
    cv.Copy(self._inputImage, self._prevIplImage) #save as t-1 image for next frame
    
    #(src, dest, threshold, maxVal, type)
    cv.Threshold(self._diffImage, self._threshImage, 16.0, 255.0, cv.CV_THRESH_BINARY)
    
    #For now, disable segmentMotion and return all motion in frame...
    if self._threshImage!=None:
      return (0,0, self._threshImage.width, self._threshImage.height)
    
    ###Experimental: segment motion to return only the most 'interesting' area
    tsec = clock()
    #(silhouette, mhi, timestamp, duration)
    cv.UpdateMotionHistory(self._threshImage, self._historyImage, tsec, 0.3)

    #(mhi, segMask, storage, timestamp, segThresh)
    #return: [tuple(area, value, rect)], (float, CvScalar, CvRect)
    seqs = cv.SegmentMotion(self._historyImage, self._segMaskImage, \
                            self._memStorage, tsec, 1.0)
    
    #cv.Copy(self._threshImage, self._inputImage)
    #cv.Threshold(self._segMaskImage, self._threshImage, 0.0, 250.0, CV_THRESH_BINARY)
    
    rects = []
    for seq in seqs:
      seqRect = seq[2] #CvRect = tuple (x, y, width, height)
      if(seqRect[2] > 4 and seqRect[3] > 4):
        rects.append(seqRect)
    
    #find the 3rd largest area and only keep those rects
    if len(rects) > 0:
      areas = [x[2]*x[3] for x in rects]
      areas.sort()
      minArea = areas[0]
      if len(areas) > 1:
        minArea = areas[len(areas)-1]
      
      rectsOk = [x for x in rects if x[2]*x[3] >= minArea]
      rect = rectsOk[0]
      
      #center the largest rect
      cRect = (rect[0]+(rect[2]/2), rect[1]+(rect[3]/2))
      return rect
    
    return None #none means no motion bounding box detected
def accumulateBackground(img):
    global first, Icount
    cv.CvtScale(img, Iscratch, 1, 0)
    if (not first):
        cv.Acc(Iscratch, IavgF)
        cv.AbsDiff(Iscratch, IprevF, Iscratch2)
        cv.Acc(Iscratch2, IdiffF)
        Icount += 1.0
    first = 0
    cv.Copy(Iscratch, IprevF)
Ejemplo n.º 10
0
def estimaBG(frames):

    npixels = cv.GetSize(frames[0])[1] * cv.GetSize(frames[0])[0]

    gray = cv.CreateImage(cv.GetSize(frames[0]), cv.IPL_DEPTH_8U, 1)
    frAbsDif = cv.CloneImage(gray)
    frThrDif = cv.CloneImage(gray)
    frAnt = cv.CloneImage(gray)

    # converte cada quadro para cinza
    cinzas = []
    for fr in frames:
        gray = cv.CreateImage(cv.GetSize(frames[0]), cv.IPL_DEPTH_8U, 1)
        cv.CvtColor(fr, gray, cv.CV_RGB2GRAY)
        cinzas.append(gray)

    iframe = 1
    fundos = []
    for fr in cinzas[1:]:
        frAnt = cinzas[iframe - 1]
        cv.AbsDiff(frAnt, fr, frAbsDif)

        pixelsDiferentesAbs = cv.CountNonZero(frAbsDif)

        # limiariza para evitar diferencas muito pequenas.
        cv.Threshold(frAbsDif, frThrDif, 120, 255, cv.CV_THRESH_TOZERO)
        # se a direrença for zero, considera a imagem para a mediana dos fundos
        pixelsDiferentesThr = cv.CountNonZero(frThrDif)
        print("Pixels diferentes do threeshold: " + str(pixelsDiferentesThr))

        if pixelsDiferentesThr < 30:
            #print 'Imagem adicionada para determinacao do background...'
            fundos.append((frames[iframe], cv.CloneImage(frThrDif),
                           cv.CloneImage(frThrDif), pixelsDiferentesAbs,
                           pixelsDiferentesThr))
        iframe += 1

    if not fundos:
        print 'Não foi possivel determinar o fundo usando os primeiros %d frames da imagem' % len(
            frames)
        sys.exit(1)

    # pega o menor threshold absoluto, ordenando e pegando o primeiro.
    def porThr(a, b):
        return cmp(a[3], b[3])

    fundos.sort(porThr)

    # retorna o que apresentou melhor diferença com relação ao anterior
    return fundos[0][0]

    return fr
Ejemplo n.º 11
0
    def process_motion(self,img):
        center = (-1, -1)
        # a lot of stuff from this section was taken from the code motempl.py, 
        #  openCV's python sample code
        timestamp = time.clock() / self.clocks_per_sec # get current time in seconds
        idx1 = self.last
        cv.CvtColor(img, self.buf[self.last], cv.CV_BGR2GRAY) # convert frame to grayscale
        idx2 = (self.last + 1) % self.n_frames 
        self.last = idx2
        silh = self.buf[idx2]
        cv.AbsDiff(self.buf[idx1], self.buf[idx2], silh) # get difference between frames
        cv.Threshold(silh, silh, 30, 1, cv.CV_THRESH_BINARY) # and threshold it
        cv.UpdateMotionHistory(silh, self.mhi, timestamp, self.mhi_duration) # update MHI
        cv.ConvertScale(self.mhi, self.mask, 255./self.mhi_duration, 
                        (self.mhi_duration - timestamp)*255./self.mhi_duration)
        cv.SetZero(img)
        cv.Merge(self.mask, None, None, None, img)
        cv.CalcMotionGradient(self.mhi, self.mask, self.orient, self.max_time_delta, self.min_time_delta, 3)
        seq = cv.SegmentMotion(self.mhi, self.segmask, self.storage, timestamp, self.max_time_delta)
        inc = 0
        a_max = 0
        max_rect = -1
    
        # there are lots of things moving around
        #  in this case just find find the biggest change on the image
        for (area, value, comp_rect) in seq:
            if comp_rect[2] + comp_rect[3] > 60: # reject small changes
                if area > a_max: 
                    a_max = area
                    max_rect = inc
            inc += 1

        # found it, now just do some processing on the area.
        if max_rect != -1:
            (area, value, comp_rect) = seq[max_rect]
            color = cv.CV_RGB(255, 0,0)
            silh_roi = cv.GetSubRect(silh, comp_rect)
            # calculate number of points within silhouette ROI
            count = cv.Norm(silh_roi, None, cv.CV_L1, None)

            # this rectangle contains the overall motion ROI
            cv.Rectangle(self.motion, (comp_rect[0], comp_rect[1]), 
                         (comp_rect[0] + comp_rect[2], 
                          comp_rect[1] + comp_rect[3]), (0,0,255), 1)

            # the goal is to report back a center of movement contained in a rectangle
            # adjust the height based on the number generated by the slider bar
            h = int(comp_rect[1] + (comp_rect[3] * (float(self.height_value) / 100)))
            # then calculate the center
            center = ((comp_rect[0] + comp_rect[2] / 2), h)

        return center
 def test_crop_oriented_boxes(self):
     box_num = 0
     for cropped_image in oriented_bounding_boxes.cropped_oriented_boxes(
             "test/test_case_bbox.xml", "test/test_case.png"):
         box_num += 1
         gold_standard = cv.LoadImage("test/test_case_%03d.png" % box_num)
         self.assertEquals(cv.GetElemType(cropped_image),
                           cv.GetElemType(gold_standard))
         self.assertEquals(cv.GetSize(cropped_image),
                           cv.GetSize(gold_standard))
         diff = cv.CloneImage(gold_standard)
         cv.AbsDiff(cropped_image, gold_standard, diff)
         self.assertEquals(cv.Sum(diff), (0, 0, 0, 0))
Ejemplo n.º 13
0
    def tryToDetect(self):
        if time.time() - self.lastTime >= 0.5:
            self.current = self.getGrayImage(cv.GetSize(self.previous))
            diffImg = cv.CloneImage(self.previous)
            cv.AbsDiff(self.previous, self.current, diffImg)
            avg = cv.Avg(diffImg)

            self.previous = cv.CloneImage(self.current)
            self.lastTime = time.time()

            self.motionFactor = self.getMotionFactor(avg[0])
            self.actualAvgDiff = avg[0]
        else:
            self.getFrame()
Ejemplo n.º 14
0
    def getDiff(self, frame):
        diffImg = cv.CloneImage(self.referencedImage)
        cv.Smooth(frame, frame, cv.CV_GAUSSIAN, 9, 0)
        cv.AbsDiff(self.referencedImage, frame, diffImg)

        greyImg = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)

        cv.CvtColor(diffImg, greyImg, cv.CV_RGB2GRAY)
        cv.Threshold(greyImg, greyImg, 30, 255, cv.CV_THRESH_BINARY)
        cv.Dilate(greyImg, greyImg, None, 9)
        cv.Erode(greyImg, greyImg, None, 5)

        # return greyImg
        return greyImg
Ejemplo n.º 15
0
def get_diff(old, new):
    """ Returns the difference between two BGR images.
    """
    size = cv.GetSize(old)
    diff = cv.CreateImage(size, 8, 1)
    old_grayscale = cv.CreateImage(size, 8, 1)
    new_grayscale = cv.CreateImage(size, 8, 1)
    cv.CvtColor(old, old_grayscale, cv.CV_BGR2GRAY)
    cv.CvtColor(new, new_grayscale, cv.CV_BGR2GRAY)
    cv.AbsDiff(old_grayscale, new_grayscale, diff)
    cv.Smooth(diff, diff, smoothtype=cv.CV_GAUSSIAN, param1=3, param2=3)
    cv.Threshold(diff, diff, 16, 255, cv.CV_THRESH_BINARY)
    cv.Smooth(diff, diff, smoothtype=cv.CV_GAUSSIAN, param1=13, param2=13)
    cv.Threshold(diff, diff, 200, 255, cv.CV_THRESH_BINARY)
    return diff
def detecta(imagem):

    cv.Smooth(imagem, imagem, cv.CV_GAUSSIAN, 3)
    maiorArea = 0
    listaContornos = []
    listaVertices = []

    cv.AbsDiff(imagem, fundo, mascara)
    cv.CvtColor(mascara, cinza, cv.CV_BGR2GRAY)
    cv.Threshold(cinza, cinza, 50, 255, cv.CV_THRESH_BINARY)

    cv.Dilate(cinza, cinza, None, 18)
    cv.Erode(cinza, cinza, None, 18)

    armazenamento = cv.CreateMemStorage(0)
    contorno = cv.FindContours(cinza, armazenamento, cv.CV_RETR_LIST,
                               cv.CV_LINK_RUNS)

    while contorno:
        vertices_do_retangulo = cv.BoundingRect(list(contorno))
        listaVertices.append(vertices_do_retangulo)

        listaContornos.append(cv.ContourArea(contorno))
        maiorArea = max(listaContornos)
        maiorArea_index = listaContornos.index(maiorArea)
        retangulo_de_interesse = listaVertices[maiorArea_index]

        contorno = contorno.h_next()

        ponto1 = (retangulo_de_interesse[0], retangulo_de_interesse[1])
        ponto2 = (retangulo_de_interesse[0] + retangulo_de_interesse[2],
                  retangulo_de_interesse[1] + retangulo_de_interesse[3])
        cv.Rectangle(imagem, ponto1, ponto2, cv.CV_RGB(0, 0, 0), 2)
        cv.Rectangle(cinza, ponto1, ponto2, cv.CV_RGB(255, 255, 255), 1)
        largura = ponto2[0] - ponto1[0]
        altura = ponto2[1] - ponto1[1]
        cv.Line(cinza, (ponto1[0] + largura / 2, ponto1[1]),
                (ponto1[0] + largura / 2, ponto2[1]), cv.CV_RGB(255, 255,
                                                                255), 1)
        cv.Line(cinza, (ponto1[0], ponto1[1] + altura / 2),
                (ponto2[0], ponto1[1] + altura / 2), cv.CV_RGB(255, 255,
                                                               255), 1)
        global x
        x = ((640 / 2 - (ponto1[0] + (largura / 2))) * -1) / 5

    cv.ShowImage("Webcam", imagem)
    cv.ShowImage("Mascara", mascara)
    cv.ShowImage("Cinza", cinza)
Ejemplo n.º 17
0
def image_processor():
    cv.Smooth(gray_image, gray_image, cv.CV_GAUSSIAN, 3,
              3)  #Blurring to remove some noise
    cv.AbsDiff(prev_image, gray_image,
               accumulator)  #Getting the difference image
    cv.InRangeS(accumulator, threshold_limit1_lower, threshold_limit1_upper,
                accumulator)  #Thresholding the difference image
    cv.Dilate(accumulator, accumulator, None,
              2)  #Dilating the thresholded difference image
    cv.Add(accumulator, sum_image, sum_image,
           accumulator)  #Adding the image to a register to use fading
    cv.SubS(sum_image, fading_factor, sum_image)  #Fading
    cv.InRangeS(sum_image, threshold_limit2_lower, threshold_limit2_upper,
                accumulator)  #Thresholding the fading image
    cv.Copy(gray_image, prev_image)
    cv.Copy(accumulator, temp_image)
Ejemplo n.º 18
0
 def detectMotion(self, curr):
     assert (curr.nChannels == 1)
     if len(self.history_frames) < self.nHistory:
         self.history_frames.append(curr)
         return curr
     else:
         oldest_frame = self.history_frames.pop(0)
         self.history_frames.append(curr)
     size = (curr.width, curr.height)
     motion_frame = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
     cv.AbsDiff(oldest_frame, curr, motion_frame)
     cv.CmpS(motion_frame, self.threshold, motion_frame, cv.CV_CMP_GT)
     # Eliminate disperse pixels, which occur because of
     # the noise of the camera
     img_temp = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
     cv.Erode(motion_frame, img_temp)
     cv.Dilate(img_temp, motion_frame)
     return motion_frame
Ejemplo n.º 19
0
def difference(il,tol):
    if(len(list(il))<2):
        return True

    i1=list(il)[0]
    i2=list(il)[1]
    temp = cv.CreateImage((i1.width,i1.height),i1.depth,i1.nChannels)
    cv.AbsDiff(i1,i2,temp)
    #cv.ConvertScale(temp,temp,20)
    #cv.ShowImage("difference",temp)
    mean = cv.Avg(temp)
    #cv.SetImageCOI(temp,2)
    #mmlvec = cv.MinMaxLoc(temp)
    #print mean,mmlvec

    mean = (mean[0]+mean[1]+mean[2])/3
    if(mean > tol):
        return True
    return False
Ejemplo n.º 20
0
    def run(self):
        while True:
            if time.time() - self.lastTime >= 0.5:
                self.current = self.getGrayImage(cv.GetSize(self.previous))
                diffImg = cv.CloneImage(self.previous)
                cv.AbsDiff(self.previous, self.current, diffImg)
                avg = cv.Avg(diffImg)
                #print "\t" + str(avg[0] > 5)
                print "\t" + str(avg[0])

                self.previous = cv.CloneImage(self.current)
                self.lastTime = time.time()
            else:
                self.getFrame()

            if time.time() - self.startTime > 20:
                break

        print "\n>> END <<\n"
Ejemplo n.º 21
0
def evaluatePolygonLabelRaw(polygons, gtImage, maskImage, foundZeros, image):
    #splat the results to an image
    labelImage = getBlankLabelImage(image)
    visualizePolygons(labelImage, polygons)

    #get the label image and rescale it to the correct size, if necessary
    labelImageRescale = cv.CreateImage(cv.GetSize(gtImage), cv.IPL_DEPTH_8U, 1)
    cv.Resize(labelImage, labelImageRescale)
    labelImage = labelImageRescale

    #compute the difference -- this is 0 if correct
    cv.AbsDiff(labelImage, gtImage, labelImage)
    w, h = cv.GetSize(gtImage)

    #mask out unknown values
    cv.Set(labelImage, 1, maskImage)

    #return (Total Pixels - incorrect) / (total pixels)
    nPixValid = w * h - foundZeros
    return float(w * h - cv.CountNonZero(labelImage)) / nPixValid
Ejemplo n.º 22
0
def doloop():
	global depth, rgb
	for i in range(1,10):
		(depth,_), (rgb,_) = get_depth(), get_video()
		bg=cv.CloneMat(cv.fromarray(depth.astype(numpy.uint8)))
	scratch = cv.CreateImage((640,480),8,1)
	scratch2 = cv.CreateImage((640,480),8,1)
	cv.SaveImage('bg.png',bg)xz
	
	while True:
		# Get a fresh frame
		(depth,_), (rgb,_) = get_depth(), get_video()
		depth=cv.fromarray(depth.astype(numpy.uint8))
		cv.AbsDiff(bg,depth,scratch)
		cv.Sub(scratch,2,10,scratch2)
		# cv.ConvertScale(scratch,scratch2,50)
		cv.Add(depth,scratch2,scratch2)
        
		# Simple Downsample
		cv.ShowImage('both',scratch2)
		cv.WaitKey(10)
Ejemplo n.º 23
0
    def motiondetect(self, img):
        gray = cv.CreateImage(self.size, 8, 1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        if not self.oldimg or (time.time() - self.lasttime) > 1:
            self.oldimg = cv.CreateImage(self.size, 8, 1)
            cv.Copy(gray, self.oldimg)
            self.lasttime = time.time()
            return 0

        self.lasttime = time.time()

        diff = cv.CreateImage(self.size, 8, 1)
        cv.AbsDiff(gray, self.oldimg, diff)

        thr = cv.CreateImage(self.size, 8, 1)
        movement = cv.CreateImage(self.size, 8, 1)
        cv.SetZero(movement)

        cv.Threshold(diff, thr, 30, 1, cv.CV_THRESH_BINARY)
        movecount = cv.CountNonZero(thr)

        color = cv.CreateImage(self.size, 8, 3)
        cv.Set(color, (255, 0, 0))

        cv.Copy(color, img, thr)

        cv.Copy(diff, movement, thr)
        cv.ShowImage("movement" + str(self.channel), movement)

        writetext(
            img, 'c' + str(self.channel) + ' a' +
            str(int(self.core.bucket.value)) + ' m' + str(movecount))

        cv.ShowImage("camera" + str(self.channel), img)

        cv.Copy(gray, self.oldimg)
        self.lasttime = time.time()

        return movecount
Ejemplo n.º 24
0
    def init_track_window(self, frame):
        win_name = 'Tracks'
        cv.NamedWindow(win_name, cv.CV_WINDOW_NORMAL)
        frame_size = cv.GetSize(frame)
        # Sample Grayscale Image
        gray = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
        '''Edge Detection for giving background reference to the tracks'''
        canny_thres = 300 if self.opt == 'file' else 150
        # Plain White Image for inverting purposes
        white_image = cv.CloneImage(gray)
        cv.Set(white_image, 255)
        # Detecting edge, inverting colors and creating a colored image
        gray_frame = cv.CloneImage(gray)
        cv.CvtColor(frame, gray_frame, cv.CV_RGB2GRAY)
        cv.Smooth(gray_frame, gray_frame, cv.CV_GAUSSIAN, 3, 0)
        cv.Canny(gray_frame, gray_frame, canny_thres, 0, 3)
        cv.Threshold(gray_frame, gray_frame, 70, 70, cv.CV_THRESH_BINARY)
        cv.AbsDiff(gray_frame, white_image, gray_frame)
        track_image = cv.CreateImage(frame_size, 8, 3)
        cv.CvtColor(gray_frame, track_image, cv.CV_GRAY2RGB)

        return track_image, win_name
Ejemplo n.º 25
0
def get_motion_mask(first_frame, second_frame):
    cv.Smooth(first_frame, first_frame, cv.CV_GAUSSIAN, GAUSSIAN_BLUR_FACTOR_1,
              0)  #smooth/blur inicial para eliminar ruido
    cv.Smooth(second_frame, first_frame, cv.CV_GAUSSIAN,
              GAUSSIAN_BLUR_FACTOR_1,
              0)  #smooth/blur inicial para eliminar ruido
    #cv.RunningAvg( color_image, running_average_image, 0.320, None )#obtencion de un promedio de frames
    #cv.ConvertScale( running_average_image, running_average_in_display_color_depth, 1.0, 0.0 ) #conversion de escala
    cv.AbsDiff(first_frame, second_frame,
               difference)  #diferencia entre la imagen promedio y la original
    cv.CvtColor(difference, grey_image,
                cv.CV_RGB2GRAY)  #conversion de la imagen a escala de grises
    cv.Threshold(grey_image, grey_image, BINARY_THRESHOLD_1, 255,
                 cv.CV_THRESH_BINARY)  #binarizacion de la imagen
    #dilatacion y afinamiento de las manchas para crear blobs mas definidos
    cv.Dilate(grey_image, grey_image, None, 4)
    cv.Erode(grey_image, grey_image, None, 4)
    cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, GAUSSIAN_BLUR_FACTOR_2,
              0)  #otro smooth aplicado para eliminar pequenas manchas
    cv.Threshold(
        grey_image, grey_image, BINARY_THRESHOLD_2, 255,
        cv.CV_THRESH_BINARY)  #rebinarizacion para eliminar pequenas manchas
    return grey_image
Ejemplo n.º 26
0
    # FPS calc
    if DISPLAY:
        framecnt = framecnt + 1
        ctime = time()
        if ctime - stime >= 1:
            print '%ifps' % (framecnt)
            framecnt = 0
            stime = ctime

    # Get original image
    img = cv.QueryFrame(cam)

    # Adaptive diff
    #cv.Smooth(img, col, cv.CV_GAUSSIAN, 3, 0)
    cv.AddWeighted(img, 1 - ADAPT, avg, ADAPT, 0, avg)
    cv.AbsDiff(img, avg, col)
    cv.CvtColor(col, col, cv.CV_RGB2HSV)
    cv.Split(col, None, None, val, None)
    cv.CmpS(val, TH, val, cv.CV_CMP_GE)
    #cv.Dilate(val, val, None, 18)
    #cv.Erode(val, val, None, 10)
    dif = cv.CountNonZero(val)

    # Show image if it's radically new
    if (dif < PIXCOUNT):
        if act < 0:
            act = ACTLOW
        else:
            act -= 1
    else:
        if act > 0:
Ejemplo n.º 27
0
Archivo: track.py Proyecto: Edsby/track
    frameT0 = time.time()

    displayImage = cv.CloneImage(cameraImage)
    colourImage = cv.CloneImage(displayImage)

    cv.Smooth(colourImage, colourImage, cv.CV_GAUSSIAN, 19, 0)

    # Use the Running Average as the static background
    # a = 0.020 leaves artifacts lingering way too long.
    # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
    cv.RunningAvg(colourImage, runningAverageImage, 0.64, None)
    cv.ConvertScale(runningAverageImage, runningAverageInDisplayColourDepth,
                    1.0, 0)

    #Get the difference between the content and the running average
    cv.AbsDiff(colourImage, runningAverageInDisplayColourDepth, difference)

    #Convert the difference image to grayscale
    cv.CvtColor(difference, greyImage, cv.CV_RGB2GRAY)

    # Threshold to difference image to a black and whit motion mask
    cv.Threshold(greyImage, greyImage, 2, 255, cv.CV_THRESH_BINARY)
    # Smooth and Threshold again to eliminate artifacts
    cv.Smooth(greyImage, greyImage, cv.CV_GAUSSIAN, 19, 0)
    cv.Threshold(greyImage, greyImage, 240, 255, cv.CV_THRESH_BINARY)

    # Turn the greay image into an array
    greyImageAsArray = numpy.asarray(cv.GetMat(greyImage))
    nonBlackCoordinatesArray = numpy.where(greyImageAsArray > 3)
    nonBlackCoordinatesArray = zip(nonBlackCoordinatesArray[1],
                                   nonBlackCoordinatesArray[0])
Ejemplo n.º 28
0
 def absdiff(self, input1, input2):
     output = cv.CloneImage(input1)
     cv.AbsDiff(input1, input2, output)
     return output
Ejemplo n.º 29
0
    def run(self):
        # Capture first frame to get size
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)
        new_size = ( frame_size[0] / 2, frame_size[1] / 2)
        color_image = cv.CreateImage(new_size, 8, 3)
        grey_image = cv.CreateImage(new_size, cv.IPL_DEPTH_8U, 1)
        moving_average = cv.CreateImage(new_size, cv.IPL_DEPTH_32F, 3)
        font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, 1)
        first = True
        k = 0        
        while True:
            k+=1

            captured_image = cv.QueryFrame(self.capture)
            color_image = cv.CreateImage(new_size, captured_image.depth, captured_image.nChannels)
            cv.Resize(captured_image, color_image)
            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)

            if first:
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
                first = False
            else:
                cv.RunningAvg(color_image, moving_average, 0.020, None)

            # 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 to grayscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Convert the image to black and white.
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            # Dilate and erode to get people blobs
            cv.Dilate(grey_image, grey_image, None, 18)
            cv.Erode(grey_image, grey_image, None, 10)

            storage = cv.CreateMemStorage(0)
            contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_TC89_KCOS)
            points = []
            #cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(255,0,255), 2, 1, 8, (0, 0))
            i = 0
            while contour:
                self.observed_occupancy = True

                bound_rect = cv.BoundingRect(list(contour))

                center_x = bound_rect[0] + (bound_rect[2]/2)
                center_y = bound_rect[1] + (bound_rect[3]/2)
                #if center_y < 200:
                #    continue
                i+=1
                closest_distance = 10000
                closest_object = None
                for to in self.tracked_objects: 
                    current_distance = math.hypot(to.latest_position[0] - center_x, to.latest_position[1] - center_y)
                    closest_distance = min(closest_distance, current_distance)                    
                    #print "DISTANCES: ", str(closest_distance), str(current_distance)
                    if current_distance == closest_distance:
                        closest_object = to

                if closest_object is None:
                    #print "OBJECT IS NEW"
                    self.tracked_objects.append(TrackedObject((center_x, center_y), [(center_x, center_y)], "new"))
                else: 
                    #print "CLOSEST OBJECT: ", closest_object.latest_position
                    closest_object.movement_vector.append((center_x, center_y))
                    closest_object.latest_position = (center_x, center_y)
                #print "AMOUNT OF OBJECTS: ", str(len(self.tracked_objects))

                if closest_object is not None:
                    cv.Line(color_image, closest_object.latest_position, (center_x, center_y), cv.CV_RGB(0,255,0))
                   
                    #closest_x = min(closest_x, to.latest_position[0])
                    #closest_y = min(closest_y, to.latest_position[0])

                contour = contour.h_next()

                pt1 = (bound_rect[0], bound_rect[1])
                pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
                points.append(pt1)
                points.append(pt2)
                cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(0,0,255), 1)
                cv.PutText(color_image, str(i), pt1, font, cv.CV_RGB(255,0,255))
                cv.Circle(color_image, (center_x, center_y), 2, cv.CV_RGB(255,0,255), 2, 8, 0)

            #print "LEN ", len(self.tracked_objects)
            #if len(self.tracked_objects) > 0 and self.tracked_objects[0] is not None:
            #    #print "ENTRE"
            #    obj_vector = self.tracked_objects[0].movement_vector
            #    print "MVV LEN ", len(obj_vector)
            #    for index in range(0, len(obj_vector)-2):
            #        try:
            #            print "Index ", index, "len(obj_vector) ", len(obj_vector)
            #            cv.Line(color_image, obj_vector[index], obj_vector[index+1], cv.CV_RGB(0,255,0))
            #
            #        except: print "oops"

            #print "Iteration ", k, " Vector: ", vectors["1"]
            cv.ShowImage("Target", color_image)

            time_passed = time.time() - self.last_request
            request_threshold = 60
            if time_passed > request_threshold:
                self.send_occupancy()
                self.send_image(color_image)
            
            
            #Listen for ESC key
            c = cv.WaitKey(10)
            #c = cv.WaitKey(7) % 0x100
            if c == 27:
                break  
Ejemplo n.º 30
0
def measure(im, debug=False):
    gray = image.rgb2gray(im)
    size = cv.GetSize(im)
    total = float(size[0] * size[1])
    l = image.sub(gray, image.gaussian(gray, 5))
    l2 = image.sub(gray, image.gaussian(gray, 9))
    edges = image.dilate(image.auto_edges(im, percentage=0.2))
    if debug:
        image.show(image.threshold(l, threshold=1),
                   "Before Edge Removal (kernel=5)")
        image.show(image.threshold(l2, threshold=1),
                   "Before Edge Removal (kernel=9)")
    cv.Set(l, 0, image.threshold(edges, threshold=1))
    cv.Set(l2, 0, image.threshold(edges, threshold=1))

    l = image.threshold(l, threshold=1)
    l2 = image.threshold(l2, threshold=1)

    if debug:
        image.show(image.threshold(edges, threshold=1), "Edges")
        image.show(l, "After Edge Removal (kernel=5)")
        image.show(l2, "After Edge Removal (kernel=9)")

    noise2 = image.new_from(gray)
    cv.EqualizeHist(gray, noise2)
    cv.AbsDiff(noise2, gray, noise2)
    cv.Set(noise2, 0,
           image.threshold(image.sobel(im, xorder=2, yorder=2), threshold=4))
    diff = image.cv2array(noise2)
    if debug:
        image.show(noise2, "DIFF")
        print "M", diff.mean(), "S", diff.std()
    diff_stat = (diff.mean(), diff.std())
    percent_noise = cv.CountNonZero(noise2) / total
    if debug:
        image.show(noise2, "NOISE2")

    # magical, I don't understand how this works
    _, sat, _ = image.split(image.rgb2hsv(im))
    edges = image.auto_edges(im)
    l, u, v = tuple(map(image.equalize_hist, image.split(image.rgb2luv(im))))
    u, v = tuple(map(image.gaussian, (u, v)))
    if debug:
        image.show(l, "1. L")
        image.show(u, "1. U")
        image.show(v, "1. V")
    la, ua, va, uva = tuple(map(image.cv2array, (l, u, v, image.And(l, u, v))))
    test = image.new_from(gray)
    test2 = image.new_from(gray)
    cv.Xor(u, v, test)
    if debug:
        image.show(test, "2. U Xor V")
    cv.Set(test, 0, image.dilate(edges))
    #cv.Set(test, 0, image.invert(image.threshold(sat, threshold=8)))
    uv_score = cv.CountNonZero(test) / total
    if debug:
        image.show(
            test, "3. U Xor V - dilate(Edges) - invert(threshold(Saturation))")

    g = Grid(size)
    images = map(image.cv2array, g.split_into(test, 6))
    arr = image.cv2array(test)
    avg_mean, avg_std = arr.mean(), arr.std()

    #ms = [(a.mean(), a.std()) for a in images]
    #min_mean = min_std = 255
    #max_mean = max_std = 0
    #for m,s in ms:
    #    min_mean = min(min_mean, m)
    #    min_std = min(min_std, s)
    #    max_mean = max(max_mean, m)
    #    max_std = max(max_std, s)
    #if debug:
    #    print min_mean, min_std
    #    print avg_mean, avg_std
    #    print max_mean, max_std
    #
    #score = uv_score, min_mean, avg_mean, avg_std, max_mean
    uv_score = uv_score, avg_std

    score = cv.CountNonZero(l) / total,  cv.CountNonZero(l2) / total, \
        diff_stat[0], diff_stat[1], uv_score

    return l, score