Example #1
0
 def mHullImg(self):
     tl = self.topLeftCorner()
     retVal = np.zeros((self.height(), self.width(), 3), np.uint8)
     npimg = self.image.getNumpy()[tl[1]:tl[1]+self.height(), tl[0]:tl[0]+self.width()]
     mask = self.mHullMask.getGrayNumpy()
     Image._copyNpwithMask(npimg, retVal, mask)
     return Image(retVal)
Example #2
0
    def getImage(self):
        """
        **SUMMARY**

        Retrieve an Image-object from the camera.  If you experience problems
        with stale frames from the camera's hardware buffer, increase the flushcache
        number to dequeue multiple frames before retrieval

        We're working on how to solve this problem.

        **RETURNS**
        
        A SimpleCV Image from the camera.

        **EXAMPLES**
        
        >>> cam = Camera()
        >>> while True:
        >>>    cam.getImage().show()

        """

        if self.pygame_camera:
            return Image(self.pygame_buffer.copy())

        if (not self.threaded):
            cv.GrabFrame(self.capture)
            self.capturetime = time.time()
        else:
            self.capturetime = self._threadcapturetime

        frame = cv.RetrieveFrame(self.capture)
        newimg = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 3)
        cv.Copy(frame, newimg)
        return Image(newimg, self)
 def reconstruct(self,img):
     """
     This is a "just for fun" method as a sanity check for the BOF codeook.
     The method takes in an image, extracts each codebook code, and replaces
     the image at the position with the code. 
     """
     retVal = cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_8U, 1)
     data = self._getPatches(img)
     p = spsd.cdist(data,self.mCodebook)
     foo = p.shape[0]
     codes = np.argmin(p,axis=1)   
     count = 0
     wsteps = img.width/self.mPatchSize[0]
     hsteps = img.height/self.mPatchSize[1]
     w=self.mPatchSize[0]
     h=self.mPatchSize[1]
     length = w*h
     retVal = Image(retVal)
     for widx in range(wsteps):
         for hidx in range(hsteps):
             x = (widx*self.mPatchSize[0])
             y = (hidx*self.mPatchSize[1])
             p = codes[count]
             temp = Image(self.mCodebook[p,:].reshape(self.mPatchSize[0],self.mPatchSize[1]))    
             retVal.blit(temp,pos=(x,y))
             count = count + 1
     return retVal
Example #4
0
    def circleDistance(self):
        """
        **SUMMARY**

        Compare the hull mask to an ideal circle and count the number of pixels
        that deviate as a fraction of total area of the ideal circle.

        **RETURNS**

        The difference, as a percentage, between the hull of our blob and an idealized
        circle of our blob. 

        """
        w = self.mHullMask.width
        h = self.mHullMask.height
        
        idealcircle = Image((w,h))
        radius = min(w,h) / 2
        idealcircle.dl().circle((w/2, h/2), radius, filled= True, color=Color.WHITE)
        idealcircle = idealcircle.applyLayers()
        print self.mHullMask
        print idealcircle
        print self.mHullMask.width
        print self.mHullMask.height
        print idealcircle.width
        print idealcircle.height
        netdiff = (idealcircle - self.mHullMask) + (self.mHullMask - idealcircle)
        numblack, numwhite = netdiff.histogram(2)
        return float(numwhite) / (radius * radius * np.pi)
    def draw(self, color=Color.RED, width=2, alpha=255):
        """
        **SUMMARY**

        Draw all the superpixels, in the given color, to the appropriate layer

        By default, this draws the superpixels boundary. If you
        provide a width, an outline of the exterior and interior contours is drawn.

        **PARAMETERS**

        * *color* -The color to render the blob as a color tuple.
        * *width* - The width of the drawn blob in pixels, if -1 then filled then the polygon is filled.
        * *alpha* - The alpha value of the rendered blob 0=transparent 255=opaque.
        
        **RETURNS**

        Image with superpixels drawn on it.

        **EXAMPLE**

        >>> image = Image("lenna")
        >>> sp = image.segmentSuperpixels(300, 20)
        >>> sp.draw(color=(255, 0, 255), width=5, alpha=128).show()
        """
        img = self.image.copy()
        self._drawingImage = Image(self.image.getEmpty(3))
        _mLayers = []
        for sp in self:
            sp.draw(color=color, width=width, alpha=alpha)
            self._drawingImage += sp.image.copy()
            for layer in sp.image._mLayers:
                _mLayers.append(layer)
        self._drawingImage._mLayers = copy(_mLayers)
        return self._drawingImage.copy()
Example #6
0
    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if (img is None):
            return
        if (self.mLastImg == None):
            if (self.mGrayOnlyMode):
                self.mLastImg = img.toGray()
                self.mDiffImg = Image(self.mLastImg.getEmpty(1))
                self.mCurrImg = None
            else:
                self.mLastImg = img
                self.mDiffImg = Image(self.mLastImg.getEmpty(3))
                self.mCurrImg = None
        else:
            if (self.mCurrImg is not None):  #catch the first step
                self.mLastImg = self.mCurrImg

            if (self.mGrayOnlyMode):
                self.mColorImg = img
                self.mCurrImg = img.toGray()
            else:
                self.mColorImg = img
                self.mCurrImg = img

            cv.AbsDiff(self.mCurrImg.getBitmap(), self.mLastImg.getBitmap(),
                       self.mDiffImg.getBitmap())

        return
Example #7
0
    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if( img is None ):
            return
        if( self.mLastImg == None ):
            if( self.mGrayOnlyMode ):
                self.mLastImg = img.toGray()
                self.mDiffImg = Image(self.mLastImg.getEmpty(1))
                self.mCurrImg = None
            else:
                self.mLastImg = img
                self.mDiffImg = Image(self.mLastImg.getEmpty(3))
                self.mCurrImg = None
        else:
            if( self.mCurrImg is not None ): #catch the first step
                self.mLastImg = self.mCurrImg

            if( self.mGrayOnlyMode ):
                self.mColorImg = img
                self.mCurrImg = img.toGray()
            else:
                self.mColorImg = img
                self.mCurrImg = img

            self.mDiffImg = Image(cv2.absdiff(self.mCurrImg.getNumpy(), self.mLastImg.getNumpy()))
        return
Example #8
0
 def load(self, datafile):
     """
     Load a codebook from file using the datafile. The datafile
     should point to a local image for the source patch image. 
     """
     myFile = open(datafile, 'r')
     temp = myFile.readline()
     #print(temp)
     self.mNumCodes = int(myFile.readline())
     #print(self.mNumCodes)
     w = int(myFile.readline())
     h = int(myFile.readline())
     self.mPatchSize = (w, h)
     #print(self.mPatchSize)
     self.mPadding = int(myFile.readline())
     #print(self.mPadding)
     w = int(myFile.readline())
     h = int(myFile.readline())
     self.mLayout = (w, h)
     #print(self.mLayout)
     imgfname = myFile.readline().strip()
     #print(imgfname)
     self.mCodebookImg = Image(imgfname)
     self.mCodebook = self._img2Codebook(self.mCodebookImg, self.mPatchSize,
                                         self.mNumCodes, self.mLayout,
                                         self.mPadding)
     #print(self.mCodebook)
     return
Example #9
0
 def reconstruct(self, img):
     """
     This is a "just for fun" method as a sanity check for the BOF codeook.
     The method takes in an image, extracts each codebook code, and replaces
     the image at the position with the code. 
     """
     retVal = cv.CreateImage((img.width, img.height), cv.IPL_DEPTH_8U, 1)
     data = self._getPatches(img)
     p = spsd.cdist(data, self.mCodebook)
     foo = p.shape[0]
     codes = np.argmin(p, axis=1)
     count = 0
     wsteps = img.width / self.mPatchSize[0]
     hsteps = img.height / self.mPatchSize[1]
     w = self.mPatchSize[0]
     h = self.mPatchSize[1]
     length = w * h
     retVal = Image(retVal)
     for widx in range(wsteps):
         for hidx in range(hsteps):
             x = (widx * self.mPatchSize[0])
             y = (hidx * self.mPatchSize[1])
             p = codes[count]
             temp = Image(self.mCodebook[p, :].reshape(
                 self.mPatchSize[0], self.mPatchSize[1]))
             retVal = retVal.blit(temp, pos=(x, y))
             count = count + 1
     return retVal
Example #10
0
    def circleDistance(self):
        """
        **SUMMARY**

        Compare the hull mask to an ideal circle and count the number of pixels
        that deviate as a fraction of total area of the ideal circle.

        **RETURNS**

        The difference, as a percentage, between the hull of our blob and an idealized
        circle of our blob. 

        """
        w = self.mHullMask.width
        h = self.mHullMask.height

        idealcircle = Image((w, h))
        radius = min(w, h) / 2
        idealcircle.dl().circle((w / 2, h / 2), radius, filled=True, color=Color.WHITE)
        idealcircle = idealcircle.applyLayers()
        print self.mHullMask
        print idealcircle
        print self.mHullMask.width
        print self.mHullMask.height
        print idealcircle.width
        print idealcircle.height
        netdiff = (idealcircle - self.mHullMask) + (self.mHullMask - idealcircle)
        numblack, numwhite = netdiff.histogram(2)
        return float(numwhite) / (radius * radius * np.pi)
Example #11
0
    def getImage(self):
        """
        **SUMMARY**

        Retrieve an Image-object from the virtual camera.
        **RETURNS**
        
        A SimpleCV Image from the camera.

        **EXAMPLES**
        
        >>> cam = VirtualCamera()
        >>> while True:
        >>>    cam.getImage().show()

        """
        if (self.sourcetype == 'image'):
            return Image(self.source, self)

        if (self.sourcetype == 'imageset'):
            img = self.source[self.counter % len(self.source)]
            self.counter = self.counter + 1
            return img

        if (self.sourcetype == 'video'):
            return Image(cv.QueryFrame(self.capture), self)
Example #12
0
 def mHullImg(self):
     tl = self.topLeftCorner()
     retVal = np.zeros((self.height(), self.width(), 3), np.uint8)
     npimg = self.image.getNumpy()[tl[1]:tl[1] + self.height(),
                                   tl[0]:tl[0] + self.width()]
     mask = self.mHullMask.getGrayNumpy()
     Image._copyNpwithMask(npimg, retVal, mask)
     return Image(retVal)
Example #13
0
 def getImage(self):
     """
     Retrieve the next frame of the video, or just a copy of the image
     """
     if (self.sourcetype == 'image'):
         return Image(self.source, self)
     
     if (self.sourcetype == 'video'):
         return Image(cv.QueryFrame(self.capture), self)
Example #14
0
    def createButterworthFilter(self, dia=400, size=(64, 64), order=2, highpass=False):
        """
        **SUMMARY**

        Creates a butterworth filter of given size and order.

        **PARAMETERS**

        * *dia*       - int - diameter of Gaussian filter
                      - list - provide a list of three diameters to create
                               a 3 channel filter
        * *size*      - size of the filter (width, height)
        * *order*     - order of the filter
        * *highpass*: -  bool 
                         True: highpass filter 
                         False: lowpass filter

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> flt = DFT.createButterworthfilter(100, (512, 512), order=3,
                                             highpass=True)
        >>> flt = DFT.createButterworthfilter([100, 120, 140], (512, 512),
                                             order=3, highpass=False)
        >>> img = Image('lenna')
        >>> flt.applyFilter(img).show()
        """
        if isinstance(dia, list):
            if len(dia) != 3 and len(dia) != 1:
                warnings.warn("diameter list must be of size 1 or 3")
                return None
            stackedfilter = DFT()
            for d in dia:
                stackedfilter = stackedfilter._stackFilters(self.createButterworthFilter(d, size, order, highpass))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         dia=dia, channels = len(dia), size=size,
                         type=stackedfilter._type, order=order,
                         frequency=stackedfilter._freqpass)
            return retVal
        freqpass = "******"
        sz_x, sz_y = size
        x0 = sz_x/2
        y0 = sz_y/2
        X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
        D = np.sqrt((X-x0)**2+(Y-y0)**2)
        flt = 255/(1.0 + (D/dia)**(order*2))
        if highpass:
            frequency = "highpass"
            flt = 255 - flt
        img = Image(flt)
        retVal = DFT(size=size, numpyarray=flt, image=img, dia=dia,
                     type="Butterworth", frequency=freqpass)
        return retVal
Example #15
0
    def mImg(self):
        #NOTE THAT THIS IS NOT PERFECT - ISLAND WITH A LAKE WITH AN ISLAND WITH A LAKE STUFF
        retVal = np.zeros((self.height(), self.width(), 3), np.uint8)
        tl = self.topLeftCorner()

        npimg = self.image.getNumpy()[tl[1]:tl[1]+self.height(), tl[0]:tl[0]+self.width()]
        mask = self.mMask.getGrayNumpy()
        Image._copyNpwithMask(npimg, retVal, mask)

        return Image(retVal)
Example #16
0
 def draw(self, color = Color.GREEN, width = -1, alpha = -1, layer = None):
     """
     **SUMMARY**
     
     Draw the blob, in the given color, to the appropriate layer
     
     By default, this draws the entire blob filled in, with holes.  If you
     provide a width, an outline of the exterior and interior contours is drawn.
     
     **PARAMETERS**
     
     * *color* -The color to render the blob as a color tuple.
     * *alpha* - The alpha value of the rendered blob 0=transparent 255=opaque.
     * *width* - The width of the drawn blob in pixels, if -1 then filled then the polygon is filled.
     * *layer* - A source layer, if layer is not None, the blob is rendered to the layer versus the source image.
     
     **RETURNS**
     
     This method either works on the original source image, or on the drawing layer provided.
     The method does not modify object itself.
     
     **EXAMPLE**
     
     >>> img = Image("lenna")
     >>> blobs = img.findBlobs()
     >>> blobs[-2].draw(color=Color.PUCE,width=-1,alpha=128)
     >>> img.show()
     """
     if not layer:
         layer = self.image.dl()
     
     if width == -1:
         #copy the mask into 3 channels and multiply by the appropriate color
         maskred = cv.CreateImage(cv.GetSize(self.mMask._getGrayscaleBitmap()), cv.IPL_DEPTH_8U, 1)
         maskgrn = cv.CreateImage(cv.GetSize(self.mMask._getGrayscaleBitmap()), cv.IPL_DEPTH_8U, 1)
         maskblu = cv.CreateImage(cv.GetSize(self.mMask._getGrayscaleBitmap()), cv.IPL_DEPTH_8U, 1)
         
         maskbit = cv.CreateImage(cv.GetSize(self.mMask._getGrayscaleBitmap()), cv.IPL_DEPTH_8U, 3)
         
         cv.ConvertScale(self.mMask._getGrayscaleBitmap(), maskred, color[0] / 255.0)
         cv.ConvertScale(self.mMask._getGrayscaleBitmap(), maskgrn, color[1] / 255.0)
         cv.ConvertScale(self.mMask._getGrayscaleBitmap(), maskblu, color[2] / 255.0)
         
         cv.Merge(maskblu, maskgrn, maskred, None, maskbit)
         
         masksurface = Image(maskbit).getPGSurface()
         masksurface.set_colorkey(Color.BLACK)
         
         if alpha != -1:
             masksurface.set_alpha(alpha)
         layer._mSurface.blit(masksurface, self.topLeftCorner()) #KAT HERE
     
     else:
         self.drawOutline(color, alpha, width, layer)
         self.drawHoles(color, alpha, width, layer)
Example #17
0
    def mImg(self):
        #NOTE THAT THIS IS NOT PERFECT - ISLAND WITH A LAKE WITH AN ISLAND WITH A LAKE STUFF
        retVal = np.zeros((self.height(), self.width(), 3), np.uint8)
        tl = self.topLeftCorner()

        npimg = self.image.getNumpy()[tl[1]:tl[1] + self.height(),
                                      tl[0]:tl[0] + self.width()]
        mask = self.mMask.getGrayNumpy()
        Image._copyNpwithMask(npimg, retVal, mask)

        return Image(retVal)
Example #18
0
 def __add__(self, flt):
     if not isinstance(flt, type(self)):
         warnings.warn("Provide SimpleCV.DFT object")
         return None
     if self.size() != flt.size():
         warnings.warn("Both SimpleCV.DFT object must have the same size")
         return None
     flt_numpy = self._numpy + flt._numpy
     flt_image = Image(flt_numpy)
     retVal = DFT(numpyarray=flt_numpy, image=flt_image, size=flt_image.size())
     return retVal
Example #19
0
 def circleDistance(self):
     """
     Compare the hull mask to an ideal circle and count the number of pixels
     that deviate as a fraction of total area of the ideal circle
     """
     idealcircle = Image((self.width(), self.height()))
     radius = min(self.width(), self.height()) / 2
     idealcircle.dl().circle((self.width()/2, self.height()/2), radius, filled= True, color=Color.WHITE)
     idealcircle = idealcircle.applyLayers()
     
     netdiff = (idealcircle - self.mHullMask) + (self.mHullMask - idealcircle)
     numblack, numwhite = netdiff.histogram(2)
     return float(numwhite) / (radius * radius * np.pi)
Example #20
0
 def _segmentSuperpixels(self):
     img = self.new_clusters
     limit = np.max(img)
     superpixels = Superpixels()
     for label in range(limit+1):
         clusterimg = Image(255*(img == label).astype(np.uint8))
         blobs = clusterimg.findBlobs()
         if blobs is None:
             continue
         blob = blobs[-1]
         blob.image = self.image & clusterimg
         superpixels.append(blob)
     return superpixels
    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if (img is None):
            return

        self.mColorImg = img
        if (self.mModelImg == None):
            self.mModelImg = Image(
                np.zeros((img.height, img.width, 3)).astype(np.float32))
            self.mDiffImg = Image(
                np.zeros((img.height, img.width, 3)).astype(np.float32))

        else:
            # do the difference
            self.mDiffImg = Image(
                cv2.absdiff(self.mModelImg.getNumpy(),
                            self.mDiffImg.getNumpy()))
            #update the model
            npimg = np.zeros((img.height, img.width, 3)).astype(np.float32)
            npimg = self.mModelImg.getFPNumpy()
            cv2.accumulateWeighted(img.getFPNumpy(), npimg, self.mAlpha)
            print npimg
            self.mModelImg = Image(npimg)
            #cv.RunningAvg(img.getFPMatrix(),self.mModelImg.getBitmap(),self.mAlpha)
            self.mReady = True
        return
Example #22
0
    def getFullHullMaskedImage(self):
        """
        Get the full size image with the masked to the blob
        """
        tl = self.topLeftCorner()
        retVal = np.zeros((self.image.height, self.image.width, 3), np.uint8)
        npimgcrop = self.image.getNumpy()[tl[1]:tl[1]+self.height(), tl[0]:tl[0]+self.width()]
        mask = self.mHullMask.getGrayNumpy()
        retValcrop = retVal[tl[1]:tl[1]+self.height(), tl[0]:tl[0]+self.width()]

        Image._copyNpwithMask(npimgcrop, retValcrop, mask)

        retVal[tl[1]:tl[1]+self.height(), tl[0]:tl[0]+self.width()] = retValcrop

        return Image(retVal)
Example #23
0
 def getFullEdgeImage(self):
     """
     Get the edge image within the full size image.
     """
     retVal = np.zeros((self.image.height, self.image.width, 3), np.uint8)
     cv2.polylines(retVal, self.mContour, 1, (255, 255, 255))
     return Image(retVal)
Example #24
0
 def getDepth(self):
     depth = freenect.sync_get_depth()[0]
     np.clip(depth, 0, 2**10 - 1, depth)
     depth >>= 2
     depth = depth.astype(np.uint8).transpose()
 
     return Image(depth, self) 
Example #25
0
    def _trainPath(self, path, className, subset, disp, verbose):
        count = 0
        files = []
        for ext in IMAGE_FORMATS:
            files.extend(glob.glob(os.path.join(path, ext)))
        if (subset > 0):
            nfiles = min(subset, len(files))
        else:
            nfiles = len(files)
        badFeat = False
        for i in range(nfiles):
            infile = files[i]
            if verbose:
                print "Opening file: " + infile
            img = Image(infile)
            featureVector = []
            for extractor in self.mFeatureExtractors:
                feats = extractor.extract(img)
                if (feats is not None):
                    featureVector.extend(feats)
                else:
                    badFeat = True

            if (badFeat):
                badFeat = False
                continue

            featureVector.extend([className])
            self.mDataSetRaw.append(featureVector)
            text = 'Training: ' + className
            self._WriteText(disp, img, text, Color.WHITE)
            count = count + 1
            del img
        return count
Example #26
0
 def undistort(self, image_or_2darray):
     """
     If given an image, apply the undistortion given my the camera's matrix and return the result
     
     If given a 1xN 2D cvmat or a 2xN numpy array, it will un-distort points of
     measurement and return them in the original coordinate system.
     
     """
     if(type(self._calibMat) != cv.cvmat or type(self._distCoeff) != cv.cvmat ):
         warnings.warn("FrameSource.undistort: This operation requires calibration, please load the calibration matrix")
         return None
        
     if (type(image_or_2darray) == InstanceType and image_or_2darray.__class__ == Image):
         inImg = image_or_2darray # we have an image
         retVal = inImg.getEmpty()
         cv.Undistort2(inImg.getBitmap(), retVal, self._calibMat, self._distCoeff)
         return Image(retVal)
     else:
         mat = ''
         if (type(image_or_2darray) == cv.cvmat):
             mat = image_or_2darray
         else:
             arr = cv.fromarray(np.array(image_or_2darray))
             mat = cv.CreateMat(cv.GetSize(arr)[1], 1, cv.CV_64FC2)
             cv.Merge(arr[:, 0], arr[:, 1], None, None, mat)
          
         upoints = cv.CreateMat(cv.GetSize(mat)[1], 1, cv.CV_64FC2)
         cv.UndistortPoints(mat, upoints, self._calibMat, self._distCoeff)
         
         #undistorted.x = (x* focalX + principalX);  
         #undistorted.y = (y* focalY + principalY);  
         return (np.array(upoints[:, 0]) *\
             [self.getCameraMatrix()[0, 0], self.getCameraMatrix()[1, 1]] +\
             [self.getCameraMatrix()[0, 2], self.getCameraMatrix()[1, 2]])[:, 0]
Example #27
0
    def getBackground(self):
        """
        **SUMMARY**

        Get Background of the Image. For more info read
        http://opencvpython.blogspot.in/2012/07/background-extraction-using-running.html

        **PARAMETERS**
        No Parameters

        **RETURNS**

        Image - SimpleCV.ImageClass.Image

        **EXAMPLE**

        >>> while (some_condition):
            ... img1 = cam.getImage()
            ... ts = img1.track("camshift", ts1, img, bb)
            ... img = img1
        >>> ts.getBackground().show()
        """
        imgs = self.trackImages(cv2_numpy=True)
        f = imgs[0]
        avg = np.float32(f)
        for img in imgs[1:]:
            f = img
            cv2.accumulateWeighted(f,avg,0.01)
            res = cv2.convertScaleAbs(avg)
        return Image(res, cv2image=True)
Example #28
0
 def generate(self,
              imgdirs,
              numcodes=128,
              sz=(11, 11),
              imgs_per_dir=50,
              img_layout=(8, 16),
              padding=0,
              verbose=True):
     """
     This method builds the bag of features codebook from a list of directories
     with images in them. Each directory should be broken down by image class.
     imgdirs = This list of directories.
     patchsz = the dimensions of each codebook patch
     numcodes = the number of different patches in the codebook.
     imglayout = the shape of the resulting image in terms of patches
     padding = the pixel padding of each patch in the resulting image.
     imgs_per_dir = this method can use a specified number of images per directory
                     i.e. min(#imgs_in_dir,imgs_per_dir)
     verbose = print output
                     
     Once the method has completed it will save the results to a local file
     using the file name codebook.png 
     
     
     WARNING: THIS METHOD WILL TAKE FOREVER
     """
     self.mPadding = padding
     self.mLayout = img_layout
     self.mNumCodes = numcodes
     self.mPatchSize = sz
     rawFeatures = np.zeros(sz[0] *
                            sz[1])  #fakeout numpy so we can use vstack
     for path in imgdirs:
         fcount = 0
         files = []
         for ext in IMAGE_FORMATS:
             files.extend(glob.glob(os.path.join(path, ext)))
         nimgs = min(len(files), imgs_per_dir)
         for i in range(nimgs):
             infile = files[i]
             if verbose:
                 print(path + " " + str(i) + " of " + str(imgs_per_dir))
                 print "Opening file: " + infile
             img = Image(infile)
             newFeat = self._getPatches(img, sz)
             if verbose:
                 print "     Got " + str(len(newFeat)) + " features."
             rawFeatures = np.vstack((rawFeatures, newFeat))
             del img
     rawFeatures = rawFeatures[
         1:, :]  # pop the fake value we put on the top
     if verbose:
         print "=================================="
         print "Got " + str(len(rawFeatures)) + " features "
         print "Doing K-Means .... this will take a long time"
     self.mCodebook = self._makeCodebook(rawFeatures, self.mNumCodes)
     self.mCodebookImg = self._codebook2Img(self.mCodebook, self.mPatchSize,
                                            self.mNumCodes, self.mLayout,
                                            self.mPadding)
     self.mCodebookImg.save('codebook.png')
Example #29
0
    def mMask(self):
        #NOTE THAT THIS IS NOT PERFECT - ISLAND WITH A LAKE WITH AN ISLAND WITH A LAKE STUFF
        #cv.FillPoly(bmp,[[(0,0),(100,0),(100,100),(0,100)],[(10,10),(90,10),(90,90),(10,90)]], (0,0,0),8)
        #gettin fancy
        # cv.FillPoly(bmp,[[(0,0),(100,0),(100,100),(0,100)],[(10,10),(40,10),(40,90),(10,90)],[(20,20),(30,20),(30,80),(20,80)],[(50,10),(90,10),(90,90),(50,90)]], (0,0,0),8)
        
        # TODO: FIX THIS SO THAT THE INTERIOR CONTOURS GET SHIFTED AND DRAWN

        #Alas - OpenCV does not provide an offset in the fillpoly method for 
        #the cv bindings (only cv2 -- which I am trying to avoid). Have to
        #manually do the offset for the ROI shift. 

        retVal = cv.CreateImage((self.width(),self.height()),cv.IPL_DEPTH_8U,1)
        cv.Zero(retVal)
        l,t = self.topLeftCorner()

        # construct the exterior contour - these are tuples 
        
        cv.FillPoly(retVal,[[(p[0] - l, p[1] - t) for p in self.mContour]],(255,255,255),8)

        #construct the hole contoursb
        holes = []
        if self.mHoleContour is not None:
            for h in self.mHoleContour: # -- these are lists
                holes.append([(h2[0]-l,h2[1]-t) for h2 in h])
                    
            cv.FillPoly(retVal,holes,(0,0,0),8)
        return Image(retVal)
Example #30
0
    def invert(self):
        """
        **SUMMARY**

        Invert the filter. All values will be subtracted from 255.

        **RETURNS**

        Inverted Filter

        **EXAMPLE**

        >>> flt = DFT.createGaussianFilter()
        >>> invertflt = flt.invert()
        """

        flt = self._numpy
        flt = 255 - flt
        img = Image(flt)
        invertedfilter = DFT(numpyarray=flt,
                             image=img,
                             size=self.size(),
                             type=self._type)
        invertedfilter._updateParams(self)
        return invertedfilter
Example #31
0
    def getDepth(self):
        """
        **SUMMARY**
        
        This method returns the Kinect depth image. 

        **RETURNS**
        
        The Kinect's depth camera image as a grayscale image. 

        **EXAMPLE**

        >>> k = Kinect()
        >>> while True:
        >>>   d = k.getDepth()
        >>>   img = k.getImage()
        >>>   result = img.sideBySide(d)
        >>>   result.show()
        """

        depth = freenect.sync_get_depth()[0]
        self.capturetime = time.time()
        np.clip(depth, 0, 2**10 - 1, depth)
        depth >>= 2
        depth = depth.astype(np.uint8).transpose()

        return Image(depth, self)
Example #32
0
 def load(self,datafile):
     """
     Load a codebook from file using the datafile. The datafile
     should point to a local image for the source patch image. 
     """
     myFile = open(datafile, 'r')
     temp = myFile.readline()
     #print(temp)
     self.mNumCodes = int(myFile.readline())
     #print(self.mNumCodes)
     w = int(myFile.readline())
     h = int(myFile.readline())
     self.mPatchSize = (w,h)
     #print(self.mPatchSize)
     self.mPadding = int(myFile.readline())
     #print(self.mPadding)
     w = int(myFile.readline())
     h = int(myFile.readline())
     self.mLayout = (w,h)
     #print(self.mLayout)
     imgfname = myFile.readline().strip()
     #print(imgfname)
     self.mCodebookImg = Image(imgfname)        
     self.mCodebook = self._img2Codebook(self.mCodebookImg,
                                         self.mPatchSize,
                                         self.mNumCodes,
                                         self.mLayout,
                                         self.mPadding)
     #print(self.mCodebook)        
     return
Example #33
0
    def colorWithClusterMeans(self):
        """
        **SUMMARY**

        This function colors each superpixel with its mean color and
        return an image.

        **RETURNS**
        Image with superpixles drawn in its mean color.

        **EXAMPLE**
        >>> image = Image("lenna")
        >>> sp = image.segmentSuperpixels(300, 20)
        >>> sp.colorWithClusterMeans().show()
        """
        if type(self.clusterMeanImage) != type(None):
            return self.clusterMeanImage
        self.clusterMeanImage = Image(self.image.getEmpty(3))
        _mLayers = []
        for sp in self:
            color = tuple(reversed(sp.meanColor()))
            sp.draw(color=color, width=-1)
            self.clusterMeanImage += sp.image.copy()
            for layer in sp.image._mLayers:
                _mLayers.append(layer)
        self.clusterMeanImage._mLayers = copy(_mLayers)
        return self.clusterMeanImage
Example #34
0
 def getHullEdgeImage(self):
     retVal = cv.CreateImage((self.width(), self.height()), cv.IPL_DEPTH_8U,
                             3)
     cv.Zero(retVal)
     tl = self.topLeftCorner()
     translate = [(cs[0] - tl[0], cs[1] - tl[1]) for cs in self.mConvexHull]
     cv.PolyLine(retVal, [translate], 1, (255, 255, 255))
     return Image(retVal)
  def _floatToInt(self,input):
      """
      convert a 32bit floating point cv array to an int array
      """
      temp = cv.CreateImage((input.width,input.height), cv.IPL_DEPTH_8U, 3) 
      cv.Convert(input.getBitmap(),temp)
 
      return Image(temp)
Example #36
0
 def rectangleDistance(self):
     """
     This compares the hull mask to the bounding rectangle.  Returns the area
     of the blob's hull as a fraction of the bounding rectangle
     """
     blackcount, whitecount = Image(self.mHullMask).histogram(2)
     return abs(1.0 - float(whitecount) /
                (self.minRectWidth() * self.minRectHeight()))
def write_images(model_path, fgvc_data_dir, images_nos, options):
    if options.crop:
        # read box information
        box_info = {}
        with open(path.join(fgvc_data_dir, 'images_box.txt')) as ibf:
            for line in ibf:
                result = line.split()
                img_no, coords = result[0], map(int, result[1:])
                box_info[img_no] = coords

    for image_no in images_nos:
        image_file = '%s.%s' % (image_no, FGVC_IMAGE_SUFFIX)
        img = Image(path.join(fgvc_data_dir, 'images', image_file))

        if options.crop:
            # although FGVC pages says the orgin is (1,1)
            # still there's some coordinates in box have 0 value
            # so don't do adjustment - 1px difference should be OK
            x1, y1, x2, y2 = box_info[image_no]
            img = img.crop(x1, y1, x2, y2)
        else:
            # remove out banner
            img = img.crop(0, 0, img.width, img.height-FGVC_BANNER_HEIGHT)

        if options.reduce_color_space:
            img = reduce_color_space(img, fast=True)

        if options.grayscale:
            img = img.grayscale()

        # normalize to width
        img = img.resize(NORMALIZED_WIDTH, int(img.height*1.0*NORMALIZED_WIDTH/img.width))
        img.save(path.join(model_path, image_file))
Example #38
0
 def getEdgeImage(self):
     """
     Get the edge image for the outer contour (no inner holes)
     """
     retVal = np.zeros((self.height(), self.width(), 3), np.uint8)
     l, t = self.topLeftCorner()
     translate = [cs - (l, t) for cs in self.mContour]
     cv2.polylines(retVal, np.array(translate), 1, (255, 255, 255))
     return Image(retVal)
Example #39
0
 def getFullHullMask(self):
     """
     Get the full sized image hull mask
     """
     tl = self.topLeftCorner()
     retVal = np.zeros((self.image.height, self.image.width), np.uint8)
     mask = self.mHullMask.getGrayNumpy()
     retVal[tl[1]:tl[1] + self.height(), tl[0]:tl[0] + self.width()] = mask
     return Image(retVal)
Example #40
0
 def _getBlobAsImage(self, colornp, bb, mask):
     """
     Return an image that contains just pixels defined by the blob sequence.
     """
     #print "need to do mask and copy too. sigh. check"
     img = colornp[bb[0]:bb[0] + bb[2], bb[1]:bb[1] + bb[3]]
     retVal = np.zeros((bb[3], bb[2], 3), np.uint8)
     Image._copyNpWithMask(img, retVal, mask)
     return (Image(retVal))
    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if( img is None ):
            return

        self.mColorImg = img
        if( self.mModelImg == None ):
            self.mModelImg = Image(cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_32F, 3))
            self.mDiffImg = Image(cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_32F, 3))
        else:
            # do the difference
            cv.AbsDiff(self.mModelImg.getBitmap(),img.getFPMatrix(),self.mDiffImg.getBitmap())
            #update the model
            cv.RunningAvg(img.getFPMatrix(),self.mModelImg.getBitmap(),self.mAlpha)
            self.mReady = True
        return
Example #42
0
    def draw(self, color = Color.GREEN, alpha=-1, width=-1, layer=None):
        """
        Draw the blob, in the given color, to the appropriate layer
        
        By default, this draws the entire blob filled in, with holes.  If you
        provide a width, an outline of the exterior and interior contours is drawn.
        
        color = The color to render the blob.
        alpha = The alpha value of the rendered blob.
        width = The width of the drawn blob in pixels, if -1 then filled then the polygon is filled.
        layer = if layer is not None, the blob is rendered to the layer versus the source image.

        Parameters:
            color - Color object or Color tuple
            alpha - Int
            width - Int
            layer - DrawingLayer
        """
        if not layer:
            layer = self.image.dl()
            
        if width == -1:
            #copy the mask into 3 channels and multiply by the appropriate color
            maskred = cv.CreateImage(cv.GetSize(self.mMask), cv.IPL_DEPTH_8U, 1)
            maskgrn = cv.CreateImage(cv.GetSize(self.mMask), cv.IPL_DEPTH_8U, 1)
            maskblu = cv.CreateImage(cv.GetSize(self.mMask), cv.IPL_DEPTH_8U, 1)
            
            maskbit = cv.CreateImage(cv.GetSize(self.mMask), cv.IPL_DEPTH_8U, 3) 

            cv.ConvertScale(self.mMask, maskred, color[0] / 255.0)
            cv.ConvertScale(self.mMask, maskgrn, color[1] / 255.0)
            cv.ConvertScale(self.mMask, maskblu, color[2] / 255.0)
            
            cv.Merge(maskblu, maskgrn, maskred, None, maskbit)    
            
            masksurface = Image(maskbit).getPGSurface()
            masksurface.set_colorkey(Color.BLACK)
            if alpha != -1:
                masksurface.set_alpha(alpha)
            layer._mSurface.blit(masksurface, self.points[0])
        else:
            self.drawOutline(color, alpha, width, layer)
            self.drawHoles(color, alpha, width, layer)
Example #43
0
 def _codebook2Img(self, cb, patchsize, count, patch_arrangement, spacersz):
     """
     cb = the codebook
     patchsize = the patch size (ususally 11x11)
     count = total codes
     patch_arrangement = how are the patches grided in the image (eg 128 = (8x16) 256=(16x16) )
     spacersz = the number of pixels between patches
     """
     w = (patchsize[0]*patch_arrangement[0])+((patch_arrangement[0]+1)*spacersz)
     h = (patchsize[1]*patch_arrangement[1])+((patch_arrangement[1]+1)*spacersz)
     bm = np.zeros((h, w), np.uint8)
     img = Image(bm)
     count = 0
     for widx in range(patch_arrangement[0]):
         for hidx in range(patch_arrangement[1]):
             x = (widx*patchsize[0])+((widx+1)*spacersz)
             y = (hidx*patchsize[1])+((hidx+1)*spacersz)
             temp = Image(cb[count,:].reshape(patchsize[0],patchsize[1]))
             img.blit(temp,pos=(x,y))
             count = count + 1
     return img
Example #44
0
    def rotate(self,angle):
        """
        Rotate the blob given the angle in degrees most of the blob elements will
        be rotated, not however this will "break" drawing back to the original image.
        To draw the blob create a new layer and draw to that layer.

        Parameters:
            angle - Int or Float
            
        """
        #FIXME: This function should return a blob
        theta = 2*np.pi*(angle/360.0)
        mode = ""
        point =(self.x,self.y)
        self.mImg = self.mImg.rotate(angle,mode,point)
        #this is a bit of a hack, but it saves a lot of code
        #I left masks as bitmaps grrrr
        tempMask = Image(self.mMask)
        self.mMask = tempMask.rotate(angle,mode,point).getBitmap()
        
        tempMask = Image(self.mHullMask)
        self.mHullMask = tempMask.rotate(angle,mode,point).getBitmap()
        
        #self.mMask.rotate(theta,"",(self.x,self.y))
        #self.mHullMask.rotate(theta,"",(self.x,self.y))
        self.mContour = map(lambda x:
                            (x[0]*np.cos(theta)-x[1]*np.sin(theta),
                             x[0]*np.sin(theta)+x[1]*np.cos(theta)),
                             self.mContour)
        self.mConvexHull = map(lambda x:
                               (x[0]*np.cos(theta)-x[1]*np.sin(theta),
                                x[0]*np.sin(theta)+x[1]*np.cos(theta)),
                               self.mConvexHull)

        if( self.mHoleContour is not None):
            for h in self.mHoleContour:
                h = map(lambda x:
                    (x[0]*np.cos(theta)-x[1]*np.sin(theta),
                     x[0]*np.sin(theta)+x[1]*np.cos(theta)),
                     h)
Example #45
0
    def applyFilter(self, image, grayscale=False):
        """
        **SUMMARY**

        Apply the DFT filter to given image.

        **PARAMETERS**

        * *image*     - SimpleCV.Image image
        * *grayscale* - if this value is True we perfrom the operation on the 
                        DFT of the gray version of the image and the result is
                        gray image. If grayscale is true we perform the 
                        operation on each channel and the recombine them to
                        create the result.

        **RETURNS**

        Filtered Image.

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> img = Image('lenna')
        >>> notch.applyFilter(img).show()
        """

        if self.width == 0 or self.height == 0:
            warnings.warn("Empty Filter. Returning the image.")
            return image
        w, h = image.size()
        if grayscale:
            image = image.toGray()
            print self._numpy.dtype, "gray"
        fltImg = Image(self._numpy)
        if fltImg.size() != image.size():
            fltImg = fltImg.resize(w, h)
        filteredImage = image.applyDFTFilter(fltImg, grayscale)
        return filteredImage
Example #46
0
    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if( img is None ):
            return

        self.mColorImg = img
        if( self.mModelImg == None ):
            self.mModelImg = Image(np.zeros((img.height, img.width, 3)).astype(np.float32))
            self.mDiffImg = Image(np.zeros((img.height, img.width, 3)).astype(np.float32))

        else:
            # do the difference
            self.mDiffImg = Image(cv2.absdiff(self.mModelImg.getNumpy(), self.mDiffImg.getNumpy()))
            #update the model
            npimg = np.zeros((img.height, img.width, 3)).astype(np.float32)
            npimg = self.mModelImg.getFPNumpy()
            cv2.accumulateWeighted(img.getFPNumpy(), npimg, self.mAlpha)
            print npimg
            self.mModelImg = Image(npimg)
            #cv.RunningAvg(img.getFPMatrix(),self.mModelImg.getBitmap(),self.mAlpha)
            self.mReady = True
        return
Example #47
0
    def getImage(self):
        """
        **SUMMARY**

        Get the SimpleCV Image of the filter

        **RETURNS**

        Image of the filter.

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> notch.getImage().show()
        """
        if isinstance(self._image, type(None)):
            if isinstance(self._numpy, type(None)):
                warnings.warn("Filter doesn't contain any image")
            self._image = Image(self._numpy)
        return self._image
Example #48
0
class RunningSegmentation(SegmentationBase):
    """
    RunningSegmentation performs segmentation using a running background model.
    This model uses an accumulator which performs a running average of previous frames
    where:
    accumulator = ((1-alpha)input_image)+((alpha)accumulator)
    """

    mError = False
    mAlpha = 0.1
    mThresh = 10
    mModelImg = None
    mDiffImg = None
    mCurrImg = None
    mBlobMaker = None
    mGrayOnly = True
    mReady = False

    def __init__(self, alpha=0.7, thresh=(20,20,20)):
        """
        Create an running background difference.
        alpha - the update weighting where:
        accumulator = ((1-alpha)input_image)+((alpha)accumulator)

        threshold - the foreground background difference threshold.
        """
        self.mError = False
        self.mReady = False
        self.mAlpha = alpha
        self.mThresh = thresh
        self.mModelImg = None
        self.mDiffImg = None
        self.mColorImg = None
        self.mBlobMaker = BlobMaker()

    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if( img is None ):
            return

        self.mColorImg = img
        if( self.mModelImg == None ):
            self.mModelImg = Image(np.zeros((img.height, img.width, 3)).astype(np.float32))
            self.mDiffImg = Image(np.zeros((img.height, img.width, 3)).astype(np.float32))

        else:
            # do the difference
            self.mDiffImg = Image(cv2.absdiff(self.mModelImg.getNumpy(), self.mDiffImg.getNumpy()))
            #update the model
            npimg = np.zeros((img.height, img.width, 3)).astype(np.float32)
            npimg = self.mModelImg.getFPNumpy()
            cv2.accumulateWeighted(img.getFPNumpy(), npimg, self.mAlpha)
            print npimg
            self.mModelImg = Image(npimg)
            #cv.RunningAvg(img.getFPMatrix(),self.mModelImg.getBitmap(),self.mAlpha)
            self.mReady = True
        return


    def isReady(self):
        """
        Returns true if the camera has a segmented image ready.
        """
        return self.mReady


    def isError(self):
        """
        Returns true if the segmentation system has detected an error.
        Eventually we'll consruct a syntax of errors so this becomes
        more expressive
        """
        return self.mError #need to make a generic error checker

    def resetError(self):
        """
        Clear the previous error.
        """
        self.mError = false
        return

    def reset(self):
        """
        Perform a reset of the segmentation systems underlying data.
        """
        self.mModelImg = None
        self.mDiffImg = None

    def getRawImage(self):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        return self._floatToInt(self.mDiffImg)

    def getSegmentedImage(self, whiteFG=True):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        retVal = None
        img = self._floatToInt(self.mDiffImg)
        if( whiteFG ):
            retVal = img.binarize(thresh=self.mThresh)
        else:
            retVal = img.binarize(thresh=self.mThresh).invert()
        return retVal

    def getSegmentedBlobs(self):
        """
        return the segmented blobs from the fg/bg image
        """
        retVal = []
        if( self.mColorImg is not None and self.mDiffImg is not None ):

            eightBit = self._floatToInt(self.mDiffImg)
            retVal = self.mBlobMaker.extractFromBinary(eightBit.binarize(thresh=self.mThresh),self.mColorImg)

        return retVal


    def _floatToInt(self,inputimg):
        """
        convert a 32bit floating point cv array to an int array
        """
        temp = inputimg.getNumpy()
        temp = temp * 255.0
        temp = temp.astype(np.uint8)
        #temp = cv.CreateImage((input.width,input.height), cv.IPL_DEPTH_8U, 3)
        #cv.Convert(input.getBitmap(),temp)

        return Image(temp)

    def __getstate__(self):
        mydict = self.__dict__.copy()
        self.mBlobMaker = None
        self.mModelImg = None
        self.mDiffImg = None
        del mydict['mBlobMaker']
        del mydict['mModelImg']
        del mydict['mDiffImg']
        return mydict

    def __setstate__(self, mydict):
        self.__dict__ = mydict
        self.mBlobMaker = BlobMaker()
Example #49
0
class BOFFeatureExtractor(object):
    """
    For a discussion of bag of features please see:
    http://en.wikipedia.org/wiki/Bag_of_words_model_in_computer_vision
    
    Initialize the bag of features extractor. This assumes you don't have
    the feature codebook pre-computed.
    patchsz = the dimensions of each codebook patch
    numcodes = the number of different patches in the codebook.
    imglayout = the shape of the resulting image in terms of patches
    padding = the pixel padding of each patch in the resulting image.
    
    """    
    mPatchSize = (11,11)
    mNumCodes = 128
    mPadding = 0
    mLayout = (8,16)
    mCodebookImg = None
    mCodebook = None
    
    def __init__(self,patchsz=(11,11),numcodes=128,imglayout=(8,16),padding=0):

        self.mPadding = padding
        self.mLayout = imglayout
        self.mPatchSize = patchsz
        self.mNumCodes = numcodes
    
    def generate(self,imgdirs,numcodes=128,sz=(11,11),imgs_per_dir=50,img_layout=(8,16),padding=0, verbose=True):
        """
        This method builds the bag of features codebook from a list of directories
        with images in them. Each directory should be broken down by image class.
        imgdirs = This list of directories.
        patchsz = the dimensions of each codebook patch
        numcodes = the number of different patches in the codebook.
        imglayout = the shape of the resulting image in terms of patches
        padding = the pixel padding of each patch in the resulting image.
        imgs_per_dir = this method can use a specified number of images per directory
                        i.e. min(#imgs_in_dir,imgs_per_dir)
        verbose = print output
                        
        Once the method has completed it will save the results to a local file
        using the file name codebook.png 
        
        
        WARNING: THIS METHOD WILL TAKE FOREVER
        """
        self.mPadding = padding
        self.mLayout = img_layout
        self.mNumCodes = numcodes
        self.mPatchSize = sz
        rawFeatures = np.zeros(sz[0]*sz[1])#fakeout numpy so we can use vstack
        for path in imgdirs:
            fcount = 0
            files = [] 
            for ext in IMAGE_FORMATS:
                files.extend(glob.glob( os.path.join(path, ext)))
            nimgs = min(len(files),imgs_per_dir)
            for i in range(nimgs):
                infile = files[i]
                if verbose:
                    print(path+" "+str(i)+" of "+str(imgs_per_dir))
                    print "Opening file: " + infile
                img = Image(infile)
                newFeat = self._getPatches(img,sz)
                if verbose:
                    print "     Got " + str(len(newFeat)) + " features."
                rawFeatures = np.vstack((rawFeatures,newFeat))
                del img
        rawFeatures = rawFeatures[1:,:] # pop the fake value we put on the top
        if verbose:
            print "=================================="
            print "Got " + str(len(rawFeatures)) + " features "
            print "Doing K-Means .... this will take a long time"
        self.mCodebook = self._makeCodebook(rawFeatures,self.mNumCodes)
        self.mCodebookImg = self._codebook2Img(self.mCodebook,self.mPatchSize,self.mNumCodes,self.mLayout,self.mPadding)
        self.mCodebookImg.save('codebook.png')
    
    def extractPatches(self, img, sz=(11,11) ):
        """
        Get patches from a single images. This is an external access method. The
        user will need to maintain the list of features. See the generate method
        as a guide to doing this by hand. Sz is the image patch size. 
        """
        return self._getPatches(img,sz)

    def makeCodebook(self, featureStack,ncodes=128):
        """
        This method will return the centroids of the k-means analysis of a large
        number of images. Ncodes is the number of centroids to find.
        """
        return self._makeCodebook(featureStack,ncodes)
        
    def _makeCodebook(self,data,ncodes=128):
        """
        Do the k-means ... this is slow as as shit
        """
        [centroids, membership] = cluster.kmeans2(data,ncodes, minit='points')
        return(centroids)
        
    def _img2Codebook(self, img, patchsize, count, patch_arrangement, spacersz):
        """
        img = the image
        patchsize = the patch size (ususally 11x11)
        count = total codes
        patch_arrangement = how are the patches grided in the image (eg 128 = (8x16) 256=(16x16) )
        spacersz = the number of pixels between patches
        """
        img = img.toHLS()
        lmat = cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_8U, 1)
        patch = cv.CreateImage(patchsize,cv.IPL_DEPTH_8U,1)
        cv.Split(img.getBitmap(),None,lmat,None,None)
        w = patchsize[0]
        h = patchsize[1]
        length = w*h
        retVal = np.zeros(length)
        for widx in range(patch_arrangement[0]):
            for hidx in range(patch_arrangement[1]):
                x = (widx*patchsize[0])+((widx+1)*spacersz)
                y = (hidx*patchsize[1])+((hidx+1)*spacersz)
                cv.SetImageROI(lmat,(x,y,w,h))
                cv.Copy(lmat,patch)
                cv.ResetImageROI(lmat)
                retVal = np.vstack((retVal,np.array(patch[:,:]).reshape(length)))
        retVal = retVal[1:,:]
        return retVal                

        
        
    def _codebook2Img(self, cb, patchsize, count, patch_arrangement, spacersz):
        """
        cb = the codebook
        patchsize = the patch size (ususally 11x11)
        count = total codes
        patch_arrangement = how are the patches grided in the image (eg 128 = (8x16) 256=(16x16) )
        spacersz = the number of pixels between patches
        """
        w = (patchsize[0]*patch_arrangement[0])+((patch_arrangement[0]+1)*spacersz)
        h = (patchsize[1]*patch_arrangement[1])+((patch_arrangement[1]+1)*spacersz)
        bm = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1)
        cv.Zero(bm)
        img = Image(bm)
        count = 0
        for widx in range(patch_arrangement[0]):
            for hidx in range(patch_arrangement[1]):
                x = (widx*patchsize[0])+((widx+1)*spacersz)
                y = (hidx*patchsize[1])+((hidx+1)*spacersz)
                temp = Image(cb[count,:].reshape(patchsize[0],patchsize[1]))    
                img.blit(temp,pos=(x,y))
                count = count + 1
        return img
        
    def _getPatches(self,img,sz):
        #retVal = [] # may need to go to np.array
        img2 = img.toHLS()
        lmat = cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_8U, 1)
        patch = cv.CreateImage(self.mPatchSize,cv.IPL_DEPTH_8U,1)
        cv.Split(img2.getBitmap(),None,lmat,None,None)
        wsteps = img2.width/sz[0]
        hsteps = img2.height/sz[1]
        w=sz[0]
        h=sz[1]
        length = w*h
        retVal = np.zeros(length)
        for widx in range(wsteps):
            for hidx in range(hsteps):
                x = (widx*sz[0])
                y = (hidx*sz[1])
                cv.SetImageROI(lmat,(x,y,w,h)) 
                cv.EqualizeHist(lmat,patch)
                #cv.Copy(lmat,patch)
                cv.ResetImageROI(lmat)
                
                retVal = np.vstack((retVal,np.array(patch[:,:]).reshape(length)))
                #retVal.append()
        retVal = retVal[1:,:] # pop the fake value we put on top of the stack
        return retVal

        
    
    def load(self,datafile):
        """
        Load a codebook from file using the datafile. The datafile
        should point to a local image for the source patch image. 
        """
        myFile = open(datafile, 'r')
        temp = myFile.readline()
        #print(temp)
        self.mNumCodes = int(myFile.readline())
        #print(self.mNumCodes)
        w = int(myFile.readline())
        h = int(myFile.readline())
        self.mPatchSize = (w,h)
        #print(self.mPatchSize)
        self.mPadding = int(myFile.readline())
        #print(self.mPadding)
        w = int(myFile.readline())
        h = int(myFile.readline())
        self.mLayout = (w,h)
        #print(self.mLayout)
        imgfname = myFile.readline().strip()
        #print(imgfname)
        self.mCodebookImg = Image(imgfname)        
        self.mCodebook = self._img2Codebook(self.mCodebookImg,
                                            self.mPatchSize,
                                            self.mNumCodes,
                                            self.mLayout,
                                            self.mPadding)
        #print(self.mCodebook)        
        return
    
    def save(self,imgfname,datafname):
        """
        Save the bag of features codebook and data set to a local file. 
        """
        myFile = open(datafname,'w')
        myFile.write("BOF Codebook Data\n")
        myFile.write(str(self.mNumCodes)+"\n")
        myFile.write(str(self.mPatchSize[0])+"\n")
        myFile.write(str(self.mPatchSize[1])+"\n")
        myFile.write(str(self.mPadding)+"\n")
        myFile.write(str(self.mLayout[0])+"\n")
        myFile.write(str(self.mLayout[1])+"\n")
        myFile.write(imgfname+"\n")
        myFile.close()
        if(self.mCodebookImg is None):
            self._codebook2Img(self.mCodebook,self.mPatchSize,self.mNumCodes,self.mLayout,self.mPadding)
        self.mCodebookImg.save(imgfname)
        return
        
    def __getstate__(self):
        if(self.mCodebookImg is None):
            self._codebook2Img(self.mCodebook,self.mPatchSize,self.mNumCodes,self.mLayout,self.mPadding)
        mydict = self.__dict__.copy()
        del mydict['mCodebook']
        return mydict
    
    def __setstate__(self, mydict):
        self.__dict__ = mydict
        self.mCodebook = self._img2Codebook(self.mCodebookImg,
                                            self.mPatchSize,
                                            self.mNumCodes,
                                            self.mLayout,
                                            self.mPadding)
    
    def extract(self, img):
        """
        This method extracts a bag of features histogram for the input image using
        the provided codebook. The result are the bin counts for each codebook code.
        """
        data = self._getPatches(img)
        p = spsd.cdist(data,self.mCodebook)
        codes = np.argmin(p,axis=1)
        [retVal,foo] = np.histogram(codes,self.mNumCodes,normed=True,range=(0,self.mNumCodes-1))
        return retVal
        
    def reconstruct(self,img):
        """
        This is a "just for fun" method as a sanity check for the BOF codeook.
        The method takes in an image, extracts each codebook code, and replaces
        the image at the position with the code. 
        """
        retVal = cv.CreateImage((img.width,img.height), cv.IPL_DEPTH_8U, 1)
        data = self._getPatches(img)
        p = spsd.cdist(data,self.mCodebook)
        foo = p.shape[0]
        codes = np.argmin(p,axis=1)   
        count = 0
        wsteps = img.width/self.mPatchSize[0]
        hsteps = img.height/self.mPatchSize[1]
        w=self.mPatchSize[0]
        h=self.mPatchSize[1]
        length = w*h
        retVal = Image(retVal)
        for widx in range(wsteps):
            for hidx in range(hsteps):
                x = (widx*self.mPatchSize[0])
                y = (hidx*self.mPatchSize[1])
                p = codes[count]
                temp = Image(self.mCodebook[p,:].reshape(self.mPatchSize[0],self.mPatchSize[1]))    
                retVal.blit(temp,pos=(x,y))
                count = count + 1
        return retVal
        
    def getFieldNames(self):
        """
        This method gives the names of each field in the feature vector in the
        order in which they are returned. For example, 'xpos' or 'width'
        """
        retVal = []
        for widx in range(self.mLayout[0]):
            for hidx in range(self.mLayout[1]):
                temp = "CB_R"+str(widx)+"_C"+str(hidx)
                retVal.append(temp)
        return retVal


    def getNumFields(self):
        """
        This method returns the total number of fields in the feature vector.
        """
        return self.mNumCodes
Example #50
0
class DFT:
    """
    **SUMMARY**

    The DFT class is the refactored class to crate DFT filters which can
    be used to filter images by applying Digital Fourier Transform. This
    is a factory class to create various DFT filters.

    **PARAMETERS**

    Any of the following parameters can be supplied to create 
    a simple DFT object.

    * *width*        - width of the filter
    * *height*       - height of the filter
    * *channels*     - number of channels of the filter
    * *size*         - size of the filter (width, height)
    * *_numpy*       - numpy array of the filter
    * *_image*       - SimpleCV.Image of the filter
    * *_dia*         - diameter of the filter 
                      (applicable for gaussian, butterworth, notch)
    * *_type*        - Type of the filter 
    * *_order*       - order of the butterworth filter 
    * *_freqpass*    - frequency of the filter (lowpass, highpass, bandpass)
    * *_xCutoffLow*  - Lower horizontal cut off frequency for lowpassfilter
    * *_yCutoffLow*  - Lower vertical cut off frequency for lowpassfilter
    * *_xCutoffHigh* - Upper horizontal cut off frequency for highpassfilter
    * *_yCutoffHigh* - Upper vertical cut off frequency for highassfilter
    


    **EXAMPLE**

    >>> gauss = DFT.createGaussianFilter(dia=40, size=(512,512))
    
    >>> dft = DFT()
    >>> butterworth = dft.createButterworthFilter(dia=300, order=2, size=(300, 300))

    """
    width = 0
    height = 0
    channels = 1
    _numpy = None
    _image = None
    _dia = 0
    _type = ""
    _order = 0
    _freqpass = ""
    _xCutoffLow = 0
    _yCutoffLow = 0
    _xCutoffHigh = 0
    _yCutoffHigh = 0

    def __init__(self, **kwargs):
        for key in kwargs:
            if key == 'width':
                self.width = kwargs[key]
            elif key == 'height':
                self.height = kwargs[key]
            elif key == 'channels':
                self.channels = kwargs[key]
            elif key == 'size':
                self.width, self.height = kwargs[key]
            elif key == 'numpyarray':
                self._numpy = kwargs[key]
            elif key == 'image':
                self._image = kwargs[key]
            elif key == 'dia':
                self._dia = kwargs[key]
            elif key == 'type':
                self._type = kwargs[key]
            elif key == 'order':
                self._order = kwargs[key]
            elif key == 'frequency':
                self._freqpass = kwargs[key]
            elif key == 'xCutoffLow':
                self._xCutoffLow = kwargs[key]
            elif key == 'yCutoffLow':
                self._yCutoffLow = kwargs[key]
            elif key == 'xCutoffHigh':
                self._xCutoffHigh = kwargs[key]
            elif key == 'yCutoffHigh':
                self._yCutoffHigh = kwargs[key]

    def __repr__(self):
        return "<SimpleCV.DFT Object: %s %s filter of size:(%d, %d) and channels: %d>" %(self._type, self._freqpass, self.width, self.height, self.channels)

    def __add__(self, flt):
        if not isinstance(flt, type(self)):
            warnings.warn("Provide SimpleCV.DFT object")
            return None
        if self.size() != flt.size():
            warnings.warn("Both SimpleCV.DFT object must have the same size")
            return None
        flt_numpy = self._numpy + flt._numpy
        flt_image = Image(flt_numpy)
        retVal = DFT(numpyarray=flt_numpy, image=flt_image, size=flt_image.size())
        return retVal

    def __invert__(self, flt):
        return self.invert()

    def _updateParams(self, flt):
        self.channels = flt.channels
        self._dia = flt._dia
        self._type = flt._type
        self._order = flt._order
        self._freqpass = flt._freqpass
        self._xCutoffLow = flt._xCutoffLow
        self._yCutoffLow = flt._yCutoffLow
        self._xCutoffHigh = flt._xCutoffHigh
        self._yCutoffHigh = flt._yCutoffHigh

    def invert(self):
        """
        **SUMMARY**

        Invert the filter. All values will be subtracted from 255.

        **RETURNS**

        Inverted Filter

        **EXAMPLE**

        >>> flt = DFT.createGaussianFilter()
        >>> invertflt = flt.invert()
        """

        flt = self._numpy
        flt = 255 - flt
        img = Image(flt)
        invertedfilter = DFT(numpyarray=flt, image=img,
                             size=self.size(), type=self._type)
        invertedfilter._updateParams(self)
        return invertedfilter

    @classmethod
    def createGaussianFilter(self, dia=400, size=(64, 64), highpass=False):
        """
        **SUMMARY**

        Creates a gaussian filter of given size.

        **PARAMETERS**

        * *dia*       -  int - diameter of Gaussian filter
                      - list - provide a list of three diameters to create
                               a 3 channel filter
        * *size*      - size of the filter (width, height)
        * *highpass*: -  bool 
                         True: highpass filter 
                         False: lowpass filter

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> gauss = DFT.createGaussianfilter(200, (512, 512),
                                            highpass=True)
        >>> gauss = DFT.createGaussianfilter([100, 120, 140], (512, 512),
                                             highpass=False)
        >>> img = Image('lenna')
        >>> gauss.applyFilter(img).show()
        """
        if isinstance(dia, list):
            if len(dia) != 3 and len(dia) != 1:
                warnings.warn("diameter list must be of size 1 or 3")
                return None
            stackedfilter = DFT()
            for d in dia:
                stackedfilter = stackedfilter._stackFilters(self.createGaussianFilter(d, size, highpass))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         dia=dia, channels = len(dia), size=size,
                         type="Gaussian", frequency=stackedfilter._freqpass)
            return retVal

        freqpass = "******"
        sz_x, sz_y = size
        x0 = sz_x/2
        y0 = sz_y/2
        X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
        D = np.sqrt((X-x0)**2+(Y-y0)**2)
        flt = 255*np.exp(-0.5*(D/dia)**2) 
        if highpass:
            flt = 255 - flt
            freqpass = "******"
        img = Image(flt)
        retVal = DFT(size=size, numpyarray=flt, image=img, dia=dia,
                     type="Gaussian", frequency=freqpass)
        return retVal

    @classmethod
    def createButterworthFilter(self, dia=400, size=(64, 64), order=2, highpass=False):
        """
        **SUMMARY**

        Creates a butterworth filter of given size and order.

        **PARAMETERS**

        * *dia*       - int - diameter of Gaussian filter
                      - list - provide a list of three diameters to create
                               a 3 channel filter
        * *size*      - size of the filter (width, height)
        * *order*     - order of the filter
        * *highpass*: -  bool 
                         True: highpass filter 
                         False: lowpass filter

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> flt = DFT.createButterworthfilter(100, (512, 512), order=3,
                                             highpass=True)
        >>> flt = DFT.createButterworthfilter([100, 120, 140], (512, 512),
                                             order=3, highpass=False)
        >>> img = Image('lenna')
        >>> flt.applyFilter(img).show()
        """
        if isinstance(dia, list):
            if len(dia) != 3 and len(dia) != 1:
                warnings.warn("diameter list must be of size 1 or 3")
                return None
            stackedfilter = DFT()
            for d in dia:
                stackedfilter = stackedfilter._stackFilters(self.createButterworthFilter(d, size, order, highpass))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         dia=dia, channels = len(dia), size=size,
                         type=stackedfilter._type, order=order,
                         frequency=stackedfilter._freqpass)
            return retVal
        freqpass = "******"
        sz_x, sz_y = size
        x0 = sz_x/2
        y0 = sz_y/2
        X, Y = np.meshgrid(np.arange(sz_x), np.arange(sz_y))
        D = np.sqrt((X-x0)**2+(Y-y0)**2)
        flt = 255/(1.0 + (D/dia)**(order*2))
        if highpass:
            frequency = "highpass"
            flt = 255 - flt
        img = Image(flt)
        retVal = DFT(size=size, numpyarray=flt, image=img, dia=dia,
                     type="Butterworth", frequency=freqpass)
        return retVal

    @classmethod    
    def createLowpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)):
        """
        **SUMMARY**

        Creates a lowpass filter of given size and order.

        **PARAMETERS**

        * *xCutoff*       - int - horizontal cut off frequency
                          - list - provide a list of three cut off frequencies
                                   to create a 3 channel filter
        * *yCutoff*       - int - vertical cut off frequency
                          - list - provide a list of three cut off frequencies
                                   to create a 3 channel filter
        * *size*      - size of the filter (width, height)

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> flt = DFT.createLowpassFilter(xCutoff=75, size=(320, 280))

        >>> flt = DFT.createLowpassFilter(xCutoff=[75], size=(320, 280))

        >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 120],
                                          size=(320, 280))

        >>> flt = DFT.createLowpassFilter(xCutoff=75, yCutoff=35,
                                          size=(320, 280))

        >>> flt = DFT.createLowpassFilter(xCutoff=[75], yCutoff=[35],
                                          size=(320, 280))

        >>> flt = DFT.createLowpassFilter(xCutoff=[75, 100, 125], yCutoff=35,
                                          size=(320, 280))
        >>> # yCutoff will be [35, 35, 35]

        >>> flt = DFT.createLowpassFilter(xCutoff=[75, 113, 124],
                                          yCutoff=[35, 45, 90],
                                          size=(320, 280))
        
        >>> img = Image('lenna')
        >>> flt.applyFilter(img).show()
        """
        if isinstance(xCutoff, list):
            if len(xCutoff) != 3 and len(xCutoff) != 1:
                warnings.warn("xCutoff list must be of size 3 or 1")
                return None
            if isinstance(yCutoff, list):
                if len(yCutoff) != 3 and len(yCutoff) != 1:
                    warnings.warn("yCutoff list must be of size 3 or 1")
                    return None
                if len(yCutoff) == 1:
                    yCutoff = [yCutoff[0]]*len(xCutoff)
            else:
                yCutoff = [yCutoff]*len(xCutoff)
            stackedfilter = DFT()
            for xfreq, yfreq in zip(xCutoff, yCutoff):
                stackedfilter = stackedfilter._stackFilters(self.createLowpassFilter(xfreq, yfreq, size))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         xCutoffLow=xCutoff, yCutoffLow=yCutoff,
                         channels=len(xCutoff), size=size,
                         type=stackedfilter._type, order=self._order,
                         frequency=stackedfilter._freqpass)
            return retVal

        w, h = size
        xCutoff = np.clip(int(xCutoff), 0, w/2)
        if yCutoff is None:
            yCutoff = xCutoff
        yCutoff = np.clip(int(yCutoff), 0, h/2)
        flt = np.zeros((w, h))
        flt[0:xCutoff, 0:yCutoff] = 255
        flt[0:xCutoff, h-yCutoff:h] = 255
        flt[w-xCutoff:w, 0:yCutoff] = 255
        flt[w-xCutoff:w, h-yCutoff:h] = 255
        img = Image(flt)
        lowpassFilter = DFT(size=size, numpyarray=flt, image=img,
                            type="Lowpass", xCutoffLow=xCutoff,
                            yCutoffLow=yCutoff, frequency="lowpass")
        return lowpassFilter

    @classmethod
    def createHighpassFilter(self, xCutoff, yCutoff=None, size=(64, 64)):
        """
        **SUMMARY**

        Creates a highpass filter of given size and order.

        **PARAMETERS**

        * *xCutoff*       - int - horizontal cut off frequency
                          - list - provide a list of three cut off frequencies
                                   to create a 3 channel filter
        * *yCutoff*       - int - vertical cut off frequency
                          - list - provide a list of three cut off frequencies
                                   to create a 3 channel filter
        * *size*      - size of the filter (width, height)

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> flt = DFT.createHighpassFilter(xCutoff=75, size=(320, 280))

        >>> flt = DFT.createHighpassFilter(xCutoff=[75], size=(320, 280))

        >>> flt = DFT.createHighpassFilter(xCutoff=[75, 100, 120],
                                           size=(320, 280))

        >>> flt = DFT.createHighpassFilter(xCutoff=75, yCutoff=35, 
                                           size=(320, 280))

        >>> flt = DFT.createHighpassFilter(xCutoff=[75], yCutoff=[35],
                                           size=(320, 280))

        >>> flt = DFT.createHighpassFilter(xCutoff=[75, 100, 125], yCutoff=35,
                                           size=(320, 280))
        >>> # yCutoff will be [35, 35, 35]

        >>> flt = DFT.createHighpassFilter(xCutoff=[75, 113, 124],
                                           yCutoff=[35, 45, 90],
                                           size=(320, 280))
        
        >>> img = Image('lenna')
        >>> flt.applyFilter(img).show()
        """
        if isinstance(xCutoff, list):
            if len(xCutoff) != 3 and len(xCutoff) != 1:
                warnings.warn("xCutoff list must be of size 3 or 1")
                return None
            if isinstance(yCutoff, list):
                if len(yCutoff) != 3 and len(yCutoff) != 1:
                    warnings.warn("yCutoff list must be of size 3 or 1")
                    return None
                if len(yCutoff) == 1:
                    yCutoff = [yCutoff[0]]*len(xCutoff)
            else:
                yCutoff = [yCutoff]*len(xCutoff)
            stackedfilter = DFT()
            for xfreq, yfreq in zip(xCutoff, yCutoff):
                stackedfilter = stackedfilter._stackFilters(
                                self.createHighpassFilter(xfreq, yfreq, size))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         xCutoffHigh=xCutoff, yCutoffHigh=yCutoff,
                         channels=len(xCutoff), size=size,
                         type=stackedfilter._type, order=self._order,
                         frequency=stackedfilter._freqpass)
            return retVal

        lowpass = self.createLowpassFilter(xCutoff, yCutoff, size)
        w, h = lowpass.size()
        flt = lowpass._numpy
        flt = 255 - flt
        img = Image(flt)
        highpassFilter = DFT(size=size, numpyarray=flt, image=img,
                             type="Highpass", xCutoffHigh=xCutoff,
                             yCutoffHigh=yCutoff, frequency="highpass")
        return highpassFilter

    @classmethod
    def createBandpassFilter(self, xCutoffLow, xCutoffHigh, yCutoffLow=None,
                             yCutoffHigh=None, size=(64, 64)):
        """
        **SUMMARY**

        Creates a banf filter of given size and order.

        **PARAMETERS**

        * *xCutoffLow*    - int - horizontal lower cut off frequency
                          - list - provide a list of three cut off frequencies
        * *xCutoffHigh*   - int - horizontal higher cut off frequency
                          - list - provide a list of three cut off frequencies
        * *yCutoffLow*    - int - vertical lower cut off frequency
                          - list - provide a list of three cut off frequencies
        * *yCutoffHigh*   - int - verical higher cut off frequency
                          - list - provide a list of three cut off frequencies
                                   to create a 3 channel filter
        * *size*      - size of the filter (width, height)

        **RETURNS**

        DFT filter.

        **EXAMPLE**

        >>> flt = DFT.createBandpassFilter(xCutoffLow=75,
                                           xCutoffHigh=190, size=(320, 280))

        >>> flt = DFT.createBandpassFilter(xCutoffLow=[75],
                                           xCutoffHigh=[190], size=(320, 280))

        >>> flt = DFT.createBandpassFilter(xCutoffLow=[75, 120, 132],
                                           xCutoffHigh=[190, 210, 234],
                                           size=(320, 280))

        >>> flt = DFT.createBandpassFilter(xCutoffLow=75, xCutoffHigh=190,
                                           yCutoffLow=60, yCutoffHigh=210,
                                           size=(320, 280))

        >>> flt = DFT.createBandpassFilter(xCutoffLow=[75], xCutoffHigh=[190],
                                           yCutoffLow=[60], yCutoffHigh=[210],
                                           size=(320, 280))

        >>> flt = DFT.createBandpassFilter(xCutoffLow=[75, 120, 132],
                                           xCutoffHigh=[190, 210, 234], 
                                           yCutoffLow=[70, 110, 112], 
                                           yCutoffHigh=[180, 220, 220], 
                                           size=(320, 280))
        
        >>> img = Image('lenna')
        >>> flt.applyFilter(img).show()
        """
        lowpass = self.createLowpassFilter(xCutoffLow, yCutoffLow, size)
        highpass = self.createHighpassFilter(xCutoffHigh, yCutoffHigh, size)
        lowpassnumpy = lowpass._numpy
        highpassnumpy = highpass._numpy
        bandpassnumpy = lowpassnumpy + highpassnumpy
        bandpassnumpy = np.clip(bandpassnumpy, 0, 255)
        img = Image(bandpassnumpy)
        bandpassFilter = DFT(size=size, image=img,
                             numpyarray=bandpassnumpy, type="bandpass",
                             xCutoffLow=xCutoffLow, yCutoffLow=yCutoffLow,
                             xCutoffHigh=xCutoffHigh, yCutoffHigh=yCutoffHigh,
                             frequency="bandpass", channels=lowpass.channels)
        return bandpassFilter

    @classmethod
    def createNotchFilter(self, dia1, dia2=None, cen=None, size=(64, 64), type="lowpass"):
        """
        **SUMMARY**

        Creates a disk shaped notch filter of given diameter at given center.

        **PARAMETERS**

        * *dia1*       -  int - diameter of the disk shaped notch
                       - list - provide a list of three diameters to create
                               a 3 channel filter
        * *dia2*       -  int - outer diameter of the disk shaped notch
                                used for bandpass filter
                       - list - provide a list of three diameters to create
                               a 3 channel filter
        * *cen*        - tuple (x, y) center of the disk shaped notch
                         if not provided, it will be at the center of the 
                         filter
        * *size*       - size of the filter (width, height)
        * *type*:      - lowpass or highpass filter

        **RETURNS**
        DFT notch filter

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> notch = DFT.createNotchFilter(dia1=200, dia2=300, cen=(200, 200),
                                          size=(512, 512))
        >>> img = Image('lenna')
        >>> notch.applyFilter(img).show()
        """
        if isinstance(dia1, list):
            if len(dia1) != 3 and len(dia1) != 1:
                warnings.warn("diameter list must be of size 1 or 3")
                return None

            if isinstance(dia2, list):
                if len(dia2) != 3 and len(dia2) != 1:
                    warnings.warn("diameter list must be of size 3 or 1")
                    return None
                if len(dia2) == 1:
                    dia2 = [dia2[0]]*len(dia1)
            else:
                dia2 = [dia2]*len(dia1)

            if isinstance(cen, list):
                if len(cen) != 3 and len(cen) != 1:
                    warnings.warn("center list must be of size 3 or 1")
                    return None
                if len(cen) == 1:
                    cen = [cen[0]]*len(dia1)
            else:
                cen = [cen]*len(dia1)

            stackedfilter = DFT()
            for d1, d2, c in zip(dia1, dia2, cen):
                stackedfilter = stackedfilter._stackFilters(self.createNotchFilter(d1, d2, c, size, type))
            image = Image(stackedfilter._numpy)
            retVal = DFT(numpyarray=stackedfilter._numpy, image=image,
                         dia=dia1+dia2, channels = len(dia1), size=size,
                         type=stackedfilter._type,
                         frequency=stackedfilter._freqpass)
            return retVal

        w, h = size
        if cen is None:
            cen = (w/2, h/2)
        a, b = cen
        y, x = np.ogrid[-a:w-a, -b:h-b]
        r = dia1/2
        mask = x*x + y*y <= r*r
        flt = np.ones((w, h))
        flt[mask] = 255
        if type == "highpass":
            flt = 255-flt
        if dia2 is not None:
            a, b = cen
            y, x = np.ogrid[-a:w-a, -b:h-b]
            r = dia2/2
            mask = x*x + y*y <= r*r
            flt1 = np.ones((w, h))
            flt1[mask] = 255
            flt1 = 255 - flt1
            flt = flt + flt1
            np.clip(flt, 0, 255)
            type = "bandpass"
        img = Image(flt)
        notchfilter = DFT(size=size, numpyarray=flt, image=img, dia=dia1,
                          type="Notch", frequency=type)
        return notchfilter

    def applyFilter(self, image, grayscale=False):
        """
        **SUMMARY**

        Apply the DFT filter to given image.

        **PARAMETERS**

        * *image*     - SimpleCV.Image image
        * *grayscale* - if this value is True we perfrom the operation on the 
                        DFT of the gray version of the image and the result is
                        gray image. If grayscale is true we perform the 
                        operation on each channel and the recombine them to
                        create the result.

        **RETURNS**

        Filtered Image.

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> img = Image('lenna')
        >>> notch.applyFilter(img).show()
        """

        if self.width == 0 or self.height == 0:
            warnings.warn("Empty Filter. Returning the image.")
            return image
        w, h = image.size()
        if grayscale:
            image = image.toGray()
        fltImg = self._image
        if fltImg.size() != image.size():
            fltImg = fltImg.resize(w, h)
        filteredImage = image.applyDFTFilter(fltImg)
        return filteredImage

    def getImage(self):
        """
        **SUMMARY**

        Get the SimpleCV Image of the filter

        **RETURNS**

        Image of the filter.

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> notch.getImage().show()
        """
        if isinstance(self._image, type(None)):
            if isinstance(self._numpy, type(None)):
                warnings.warn("Filter doesn't contain any image")
            self._image = Image(self._numpy)
        return self._image

    def getNumpy(self):
        """
        **SUMMARY**

        Get the numpy array of the filter

        **RETURNS**

        numpy array of the filter.

        **EXAMPLE**

        >>> notch = DFT.createNotchFilter(dia1=200, cen=(200, 200),
                                          size=(512, 512), type="highpass")
        >>> notch.getNumpy()
        """
        if isinstance(self._numpy, type(None)):
            if isinstance(self._image, type(None)):
                warnings.warn("Filter doesn't contain any image")
            self._numpy = self._image.getNumpy()
        return self._numpy

    def getOrder(self):
        """
        **SUMMARY**

        Get order of the butterworth filter

        **RETURNS**

        order of the butterworth filter

        **EXAMPLE**

        >>> flt = DFT.createButterworthFilter(order=4)
        >>> print flt.getOrder()
        """
        return self._order

    def size(self):
        """
        **SUMMARY**

        Get size of the filter

        **RETURNS**

        tuple of (width, height)

        **EXAMPLE**

        >>> flt = DFT.createGaussianFilter(size=(380, 240))
        >>> print flt.size()
        """
        return (self.width, self.height)

    def getDia(self):
        """
        **SUMMARY**

        Get diameter of the filter

        **RETURNS**

        diameter of the filter

        **EXAMPLE**

        >>> flt = DFT.createGaussianFilter(dia=200, size=(380, 240))
        >>> print flt.getDia()
        """
        return self._dia

    def getType(self):
        """
        **SUMMARY**

        Get type of the filter

        **RETURNS**

        type of the filter

        **EXAMPLE**

        >>> flt = DFT.createGaussianFilter(dia=200, size=(380, 240))
        >>> print flt.getType() # Gaussian
        """
        return self._type

    def stackFilters(self, flt1, flt2):
        """
        **SUMMARY**

        Stack three signle channel filters of the same size to create
        a 3 channel filter.

        **PARAMETERS**

        * *flt1* - second filter to be stacked
        * *flt2* - thrid filter to be stacked

        **RETURNS**

        DFT filter

        **EXAMPLE**

        >>> flt1 = DFT.createGaussianFilter(dia=200, size=(380, 240))
        >>> flt2 = DFT.createGaussianFilter(dia=100, size=(380, 240))
        >>> flt2 = DFT.createGaussianFilter(dia=70, size=(380, 240))
        >>> flt = flt1.stackFilters(flt2, flt3) # 3 channel filter
        """
        if not(self.channels == 1 and flt1.channels == 1 and flt2.channels == 1):
            warnings.warn("Filters must have only 1 channel")
            return None
        if not (self.size() == flt1.size() and self.size() == flt2.size()):
            warnings.warn("All the filters must be of same size")
            return None
        numpyflt = self._numpy
        numpyflt1 = flt1._numpy
        numpyflt2 = flt2._numpy
        flt = np.dstack((numpyflt, numpyflt1, numpyflt2))
        img = Image(flt)
        stackedfilter = DFT(size=self.size(), numpyarray=flt, image=img, channels=3)
        return stackedfilter

    def _stackFilters(self, flt1):
        """
        **SUMMARY**

        stack two filters of same size. channels don't matter.

        **PARAMETERS**

        * *flt1* - second filter to be stacked

        **RETURNS**

        DFT filter

        """
        if isinstance(self._numpy, type(None)):
            return flt1
        if not self.size() == flt1.size():
            warnings.warn("All the filters must be of same size")
            return None
        numpyflt = self._numpy
        numpyflt1 = flt1._numpy
        flt = np.dstack((numpyflt, numpyflt1))
        stackedfilter = DFT(size=self.size(), numpyarray=flt,
                            channels=self.channels+flt1.channels,
                            type=self._type, frequency=self._freqpass)
        return stackedfilter
Example #51
0
class DiffSegmentation(SegmentationBase):
    """
    This method will do image segmentation by looking at the difference between
    two frames.

    grayOnly - use only gray images.
    threshold - The value at which we consider the color difference to
    be significant enough to be foreground imagery.

    The general usage is

    >>> segmentor = DiffSegmentation()
    >>> cam = Camera()
    >>> while(1):
    >>>    segmentor.addImage(cam.getImage())
    >>>    if(segmentor.isReady()):
    >>>        img = segmentor.getSegmentedImage()

    """
    mError = False
    mLastImg = None
    mCurrImg = None
    mDiffImg = None
    mColorImg = None
    mGrayOnlyMode = True
    mThreshold = 10
    mBlobMaker = None

    def __init__(self, grayOnly=False, threshold = (10,10,10) ):
        self.mGrayOnlyMode = grayOnly
        self.mThreshold = threshold
        self.mError = False
        self.mCurrImg = None
        self.mLastImg = None
        self.mDiffImg = None
        self.mColorImg = None
        self.mBlobMaker = BlobMaker()

    def addImage(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if( img is None ):
            return
        if( self.mLastImg == None ):
            if( self.mGrayOnlyMode ):
                self.mLastImg = img.toGray()
                self.mDiffImg = Image(self.mLastImg.getEmpty(1))
                self.mCurrImg = None
            else:
                self.mLastImg = img
                self.mDiffImg = Image(self.mLastImg.getEmpty(3))
                self.mCurrImg = None
        else:
            if( self.mCurrImg is not None ): #catch the first step
                self.mLastImg = self.mCurrImg

            if( self.mGrayOnlyMode ):
                self.mColorImg = img
                self.mCurrImg = img.toGray()
            else:
                self.mColorImg = img
                self.mCurrImg = img

            self.mDiffImg = Image(cv2.absdiff(self.mCurrImg.getNumpy(), self.mLastImg.getNumpy()))
        return


    def isReady(self):
        """
        Returns true if the camera has a segmented image ready.
        """
        if( self.mDiffImg is None ):
            return False
        else:
            return True


    def isError(self):
        """
        Returns true if the segmentation system has detected an error.
        Eventually we'll consruct a syntax of errors so this becomes
        more expressive
        """
        return self.mError #need to make a generic error checker

    def resetError(self):
        """
        Clear the previous error.
        """
        self.mError = False
        return

    def reset(self):
        """
        Perform a reset of the segmentation systems underlying data.
        """
        self.mCurrImg = None
        self.mLastImg = None
        self.mDiffImg = None

    def getRawImage(self):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        return self.mDiffImg

    def getSegmentedImage(self, whiteFG=True):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        retVal = None
        if( whiteFG ):
            retVal = self.mDiffImg.binarize(thresh=self.mThreshold)
        else:
            retVal = self.mDiffImg.binarize(thresh=self.mThreshold).invert()
        return retVal

    def getSegmentedBlobs(self):
        """
        return the segmented blobs from the fg/bg image
        """
        retVal = []
        if( self.mColorImg is not None and self.mDiffImg is not None ):
            retVal = self.mBlobMaker.extractFromBinary(self.mDiffImg.binarize(thresh=self.mThreshold),self.mColorImg)
        return retVal

    def __getstate__(self):
        mydict = self.__dict__.copy()
        self.mBlobMaker = None
        del mydict['mBlobMaker']
        return mydict

    def __setstate__(self, mydict):
        self.__dict__ = mydict
        self.mBlobMaker = BlobMaker()
Example #52
0
class Superpixels(FeatureSet):
    """
    ** SUMMARY **
    Superpixels is a class extended from FeatureSet which is a class
    extended from Python's list. So, it has all the properties of a list
    as well as all the properties of FeatureSet.

    Each object of this list is a Blob corresponding to the superpixel.

    ** EXAMPLE **
    >>> image = Image("lenna")
    >>> sp = image.segmentSuperpixels(300, 20)
    >>> sp.show()
    >>> sp.centers()
    """
    def __init__(self):
        self._drawingImage = None
        self.clusterMeanImage = None
        pass

    def append(self, blob):
        list.append(self, blob)
        #if len(self) != 1:
            #self.image += blob.image.copy()

    @LazyProperty
    def image(self):
        img = None
        for sp in self:
            if img is None:
                img = sp.image
            else:
                img += sp.image
        return img

    def draw(self, color=Color.RED, width=2, alpha=255):
        """
        **SUMMARY**

        Draw all the superpixels, in the given color, to the appropriate layer

        By default, this draws the superpixels boundary. If you
        provide a width, an outline of the exterior and interior contours is drawn.

        **PARAMETERS**

        * *color* -The color to render the blob as a color tuple.
        * *width* - The width of the drawn blob in pixels, if -1 then filled then the polygon is filled.
        * *alpha* - The alpha value of the rendered blob 0=transparent 255=opaque.
        
        **RETURNS**

        Image with superpixels drawn on it.

        **EXAMPLE**

        >>> image = Image("lenna")
        >>> sp = image.segmentSuperpixels(300, 20)
        >>> sp.draw(color=(255, 0, 255), width=5, alpha=128).show()
        """
        img = self.image.copy()
        self._drawingImage = Image(self.image.getEmpty(3))
        _mLayers = []
        for sp in self:
            sp.draw(color=color, width=width, alpha=alpha)
            self._drawingImage += sp.image.copy()
            for layer in sp.image._mLayers:
                _mLayers.append(layer)
        self._drawingImage._mLayers = copy(_mLayers)
        return self._drawingImage.copy()

    def show(self, color=Color.RED, width=2, alpha=255):
        """
        **SUMMARY**

        This function automatically draws the superpixels on the drawing image
        and shows it.

        ** RETURNS **

        None

        ** EXAMPLE **
        >>> image = Image("lenna")
        >>> sp = image.segmentSuperpixels(300, 20)
        >>> sp.show(color=(255, 0, 255), width=5, alpha=128)
        """
        if type(self._drawingImage) == type(None):
            self.draw(color=color, width=width, alpha=alpha)
        self._drawingImage.show()

    def colorWithClusterMeans(self):
        """
        **SUMMARY**

        This function colors each superpixel with its mean color and
        return an image.

        **RETURNS**
        Image with superpixles drawn in its mean color.

        **EXAMPLE**
        >>> image = Image("lenna")
        >>> sp = image.segmentSuperpixels(300, 20)
        >>> sp.colorWithClusterMeans().show()
        """
        if type(self.clusterMeanImage) != type(None):
            return self.clusterMeanImage
        self.clusterMeanImage = Image(self.image.getEmpty(3))
        _mLayers = []
        for sp in self:
            color = tuple(reversed(sp.meanColor()))
            sp.draw(color=color, width=-1)
            self.clusterMeanImage += sp.image.copy()
            for layer in sp.image._mLayers:
                _mLayers.append(layer)
        self.clusterMeanImage._mLayers = copy(_mLayers)
        return self.clusterMeanImage