Exemple #1
0
def threshold_image(image, n=[]):
    """Record the first 5 images to get a background, then diff current frame with the last saved frame.
    """
    if len(n) < 5:
        # n[4] will be our background
        # First capture a few images
        n.append(cv.cvCloneMat(image))
        if len(n) == 5:
            # last time here 
            # could do averaging here.
            pass
        return image

    original = n[4]
    differenceImage  = cv.cvCloneMat( image )
    cv.cvAbsDiff( image, original, differenceImage )
    
    """The threshold value determines the amount of "Change" required 
    before something will show up"""
    thresholdValue = 50     # 32 
    cv.cvThreshold( differenceImage, differenceImage, thresholdValue, 255, cv.CV_THRESH_BINARY )
    
    # Convert to one channel
    gray = cv.cvCreateImage( cv.cvGetSize(differenceImage), 8, 1 )
    cv.cvCvtColor( differenceImage, gray, cv.CV_BGR2GRAY )   
    
    # Use median filter to remove salt and pepper noise.
    cv.cvSmooth(gray, gray, cv.CV_MEDIAN, 15)
    
    # Dilate and the threshold image
    # It adds a border to the object.
    #cv.cvDilate(gray,gray, None, 9)
    
    # Add a bit of Blur to the threshold mask
    cv.cvSmooth(gray, gray, cv.CV_GAUSSIAN, 5)
    
    result  = cv.cvCloneMat( image)
    cv.cvSetZero(result)
    
    cv.cvAnd(image,image, result, gray)
    return result
    def process(self, take_new_image=True):
        """We will take a snapshot, optionally do some arbitrary process (eg in numpy/scipy)
        then display it. If a frame is given use that instead of taking a new image.
        """
 
        try:
            if take_new_image:
                logging.debug("capturing an image")
                self.snapshot = cv.cvCloneMat( hg.cvQueryFrame( self.camera) )
       
            if self.processFunction is not None:
                logging.debug("Sending image to process function")
                res = self.processFunction(self.snapshot)
                logging.debug("Received result from processing function")
                assert isinstance(res,cv.CvMat), "Not CvMat"
                self.snapshot = res
                       
            if self.show:
                hg.cvShowImage( self.title, self.snapshot )
        except Exception, e:
            # If something goes wrong make sure we close the window
            logging.error("Error in processing image: %s" % e)
            hg.cvDestroyWindow(self.title)
            raise SystemExit
    def __init__(self, processFunction = None, title = "Video Capture Player", show=True, **argd):
        self.__dict__.update(**argd)
        super(VideoCapturePlayer, self).__init__(**argd)
        t_begin = time.time()
        self.processFunction = processFunction
        self.title = title
        self.show = show

        if self.show is True:
            self.display = hg.cvNamedWindow(self.title)

        try:
            self.camera = hg.cvCreateCameraCapture(0)
        
            # Take a frame to get props and use in testing
            self.snapshot = cv.cvCloneMat( hg.cvQueryFrame( self.camera ) )
            # check that we got an image, otherwise try again.
            for i in xrange(20):
                if self.snapshot is not None: break
                self.snapshot = hg.cvQueryFrame( self.camera )
        except:
            print("Couldn't open camera device, is it connected?")
            hg.cvDestroyWindow(title)
            raise SystemExit
    def process(self, take_new_image=True):
        """We will take a snapshot, optionally do some arbitrary process (eg in numpy/scipy)
        then display it. If a frame is given use that instead of taking a new image.
        """

        try:
            if take_new_image:
                logging.debug("capturing an image")
                self.snapshot = cv.cvCloneMat(hg.cvQueryFrame(self.camera))

            if self.processFunction is not None:
                logging.debug("Sending image to process function")
                res = self.processFunction(self.snapshot)
                logging.debug("Received result from processing function")
                assert isinstance(res, cv.CvMat), "Not CvMat"
                self.snapshot = res

            if self.show:
                hg.cvShowImage(self.title, self.snapshot)
        except Exception, e:
            # If something goes wrong make sure we close the window
            logging.error("Error in processing image: %s" % e)
            hg.cvDestroyWindow(self.title)
            raise SystemExit