Beispiel #1
0
    def generateTransforms(self, detection):
        # Transform the face
        affine = pv.AffineFromRect(detection, self.tile_size)

        w, h = self.tile_size

        if self.perturbations:
            # Randomly rotate, translate and scale the images
            center = pv.AffineTranslate(-0.5 * w, -0.5 * h, self.tile_size)
            rotate = pv.AffineRotate(random.uniform(-pi / 8, pi / 8),
                                     self.tile_size)
            scale = pv.AffineScale(random.uniform(0.9, 1.1), self.tile_size)
            translate = pv.AffineTranslate(random.uniform(-0.05 * w, 0.05 * w),
                                           random.uniform(-0.05 * h, 0.05 * h),
                                           self.tile_size)
            inv_center = pv.AffineTranslate(0.5 * w, 0.5 * h, self.tile_size)

            affine = inv_center * translate * scale * rotate * center * affine
            #affine = affine*center*rotate*scale*translate*inv_center

        lx = self.left_center.X() - self.subtile_size[0] / 2
        ly = self.left_center.Y() - self.subtile_size[1] / 2
        rx = self.right_center.X() - self.subtile_size[0] / 2
        ry = self.right_center.Y() - self.subtile_size[1] / 2

        laffine = pv.AffineFromRect(
            pv.Rect(lx, ly, self.subtile_size[0], self.subtile_size[1]),
            self.subtile_size) * affine
        raffine = pv.AffineFromRect(
            pv.Rect(rx, ry, self.subtile_size[0], self.subtile_size[1]),
            self.subtile_size) * affine
        return laffine, raffine
Beispiel #2
0
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''

        # determine the face rect
        true_rect = face_from_eyes(left_eye, right_eye)

        # run the face detector
        rects = self.face_detector.detect(im)

        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect, true_rect):
                # Transform the face
                affine = pv.AffineFromRect(pred_rect, self.tile_size)

                w, h = self.tile_size

                if self.perturbations:
                    # Randomly rotate, translate and scale the images
                    center = pv.AffineTranslate(-0.5 * w, -0.5 * h,
                                                self.tile_size)
                    rotate = pv.AffineRotate(random.uniform(-pi / 8, pi / 8),
                                             self.tile_size)
                    scale = pv.AffineScale(random.uniform(0.9, 1.1),
                                           self.tile_size)
                    translate = pv.AffineTranslate(
                        random.uniform(-0.05 * w, 0.05 * w),
                        random.uniform(-0.05 * h, 0.05 * h), self.tile_size)
                    inv_center = pv.AffineTranslate(0.5 * w, 0.5 * h,
                                                    self.tile_size)

                    affine = inv_center * translate * scale * rotate * center * affine
                    #affine = affine*center*rotate*scale*translate*inv_center

                cropped = affine.transformImage(im)
                cropped = pv.meanStd(cropped)

                # Mark the eyes
                leye = affine.transformPoint(left_eye)
                reye = affine.transformPoint(right_eye)

                # Add training data to locators
                self.training_labels.append((leye, reye))

                self.normalize.addTraining(0.0, cropped)
                #self.left_locator.addTraining(cropped,leye)
                #self.right_locator.addTraining(cropped,reye)

                # Just use the first success
                return

        # The face was not detected
        self.detection_failures += 1
Beispiel #3
0
    def test_translate(self):
        transform = pv.AffineTranslate(10., 15., (640, 480))
        _ = transform.transformImage(self.test_image)
        #im_a.show()

        pt = transform.transformPoint(pv.Point(320, 240))
        self.assertAlmostEqual(pt.X(), 330.)
        self.assertAlmostEqual(pt.Y(), 255.)

        pt = transform.invertPoint(pv.Point(320, 240))
        self.assertAlmostEqual(pt.X(), 310.)
        self.assertAlmostEqual(pt.Y(), 225.)
Beispiel #4
0
def lbp(im, pattern=LBP_CLASSIC):
    im = pv.Image(im.asOpenCVBW())  #TODO: Use opencv for speed

    mat = im.asMatrix2D()
    lbp = np.zeros(mat.shape, dtype=np.uint8)

    w, h = mat.shape

    bit = 1
    for dx, dy in pattern:
        affine = pv.AffineTranslate(-dx, -dy, (w, h))
        mat2 = affine.transformImage(im).asMatrix2D()

        lbp += bit * (mat < mat2)

        bit = bit * 2

    return lbp
Beispiel #5
0
    def crop(self, rect, size=None, interpolation=None, return_affine=False):
        '''
        Crops an image to the given rectangle. Rectangle parameters are rounded to nearest 
        integer values.  High quality resampling.  The default behavior is to use cv.GetSubRect
        to crop the image.  This returns a slice the OpenCV image so modifying the resulting
        image data will also modify the data in this image.  If a size is provide a new OpenCV
        image is created for that size and cv.Resize is used to copy the image data. If the 
        bounds of the rectangle are outside the image, an affine transform (pv.AffineFromRect)
        is used to produce the croped image to properly handle regions outside the image.
        In this case the downsampling quality may not be as good. #
        
        @param rect: a Rectangle defining the region to be cropped.
        @param size: a new size for the returned image.  If None the result is not resized.
        @param interpolation: None = Autoselect or one of CV_INTER_AREA, CV_INTER_NN, CV_INTER_LINEAR, CV_INTER_BICUBIC
        @param return_affine: If True, also return an affine transform that can be used to transform points.
        @returns: a cropped version of the image or if return affine a tuple of (image,affine)
        @rtype: pv.Image
        '''
        # Notes: pv.Rect(0,0,w,h) should return the entire image. Since pixel values
        # are indexed by zero this means that upper limits are not inclusive: x from [0,w)
        # and y from [0,h)
        x, y, w, h = rect.asTuple()

        x = int(np.round(x))
        y = int(np.round(y))
        w = int(np.round(w))
        h = int(np.round(h))

        # Check the bounds for cropping
        if x < 0 or y < 0 or x + w > self.size[0] or y + h > self.size[1]:
            if size is None:
                size = (w, h)

            affine = pv.AffineFromRect(pv.Rect(x, y, w, h), size)
            im = affine(self)
            if return_affine:
                return im, affine
            else:
                return im

        # Get the image as opencv
        cvim = self.asOpenCV()

        # Set up ROI
        subim = cv.GetSubRect(cvim, (x, y, w, h))

        affine = pv.AffineTranslate(-x, -y, (w, h))

        if size is None:
            size = (w, h)

        # Copy to new image
        new_image = cv.CreateImage(size, cvim.depth, cvim.nChannels)
        if interpolation is None:

            if size[0] < w or size[1] < y:
                # Downsampling so use area interpolation
                interpolation = cv.CV_INTER_AREA
            else:
                # Upsampling so use linear
                interpolation = cv.CV_INTER_CUBIC

        # Resize to the correct size
        cv.Resize(subim, new_image, interpolation)

        affine = pv.AffineNonUniformScale(
            float(size[0]) / w,
            float(size[1]) / h, size) * affine

        # Return the result as a pv.Image
        if return_affine:
            return pv.Image(new_image), affine
        else:
            return pv.Image(new_image)
Beispiel #6
0
    def test_prev_ref3(self):
        fname = os.path.join(pv.__path__[0], 'data', 'nonface',
                             'NONFACE_13.jpg')
        torig = tprev = im_a = pv.Image(fname)
        #im_a.show()
        w, h = im_a.size

        # Scale
        aff = pv.AffineScale(0.5, (w / 2, h / 2))
        accu = aff
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Translate
        aff = pv.AffineTranslate(20, 20, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Rotate
        aff = pv.AffineRotate(np.pi / 4, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Translate
        aff = pv.AffineTranslate(100, -10, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Scale
        aff = pv.AffineScale(2.0, (w, h))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")