Ejemplo n.º 1
0
    def tranfun(self, image_and_loc):
        image, left, top, right, bottom = image_and_loc
        w, h = image.size
        left = np.clip(left,0,w-1)
        right = np.clip(right,0,w-1)
        top = np.clip(top, 0, h-1)
        bottom = np.clip(bottom, 0, h-1)
        img = trans_utils.getcvimage(image)
        try:
            # global index
            res = getpilimage(img[top:bottom,left:right])
            # res.save('test_imgs/crop-debug-{}.jpg'.format(index))
            # index+=1
            return res
        except AttributeError as e:
            print('error')
            image.save('test_imgs/t.png')
            print( left, top, right, bottom)

        h = bottom - top
        w = right - left
        org = np.array([[left - np.random.randint(0, self.maxv_w), top + np.random.randint(-self.maxv_h, self.maxv_h//2)],
                        [right + np.random.randint(0, self.maxv_w), top + np.random.randint(-self.maxv_h, self.maxv_h//2)],
                        [left - np.random.randint(0, self.maxv_w), bottom - np.random.randint(-self.maxv_h, self.maxv_h//2)],
                        [right + np.random.randint(0, self.maxv_w), bottom - np.random.randint(-self.maxv_h, self.maxv_h//2)]], np.float32)
        dst = np.array([[0, 0], [w, 0], [0, h], [w, h]], np.float32)
        M = cv2.getPerspectiveTransform(org,dst)
        res = cv2.warpPerspective(img,M,(w,h))
        return getpilimage(res)
Ejemplo n.º 2
0
 def tranfun(self, image):
     image = getpilimage(image)
     w, h = image.size
     rate = np.random.random()*(self.max_rate-self.min_rate)+self.min_rate
     w2 = int(w*rate)
     image = image.resize((w2, h))
     return image
Ejemplo n.º 3
0
 def tranfun(self, image):
     image = getpilimage(image)
     num_noise = int(image.size[1] * image.size[0] * self.rate)
     # assert len(image.split()) == 1
     for k in range(num_noise):
         i = int(np.random.random() * image.size[1])
         j = int(np.random.random() * image.size[0])
         image.putpixel((j, i), int(np.random.random() * 255))
     return image
Ejemplo n.º 4
0
 def tranfun(self, image):
     image = getpilimage(image)
     draw =ImageDraw.Draw(image)
     h=image.height
     w=image.width
     y0=random.randint(h//4,h*3//4)
     y1=np.clip(random.randint(-3,3)+y0,0,h-1)
     color=random.randint(0,30)
     draw.line(((0,y0),(w-1,y1)),fill=(color,color,color),width=2)
     return image
Ejemplo n.º 5
0
 def tranfun(self, image):
     img = trans_utils.getcvimage(image)
     h,w = img.shape[:2]
     org = np.array([[0,np.random.randint(0,self.maxv)],
                     [w,np.random.randint(0,self.maxv)],
                     [0,h-np.random.randint(0,self.maxv)],
                     [w,h-np.random.randint(0,self.maxv)]],np.float32)
     dst = np.array([[0, 0], [w, 0], [0, h], [w, h]], np.float32)
     M = cv2.getPerspectiveTransform(org,dst)
     res = cv2.warpPerspective(img,M,(w,h))
     return getpilimage(res)
Ejemplo n.º 6
0
    def tranfun(self, image):
        image = getpilimage(image)
        angle=random.randint(0,self.angle)
        M = cv2.getRotationMatrix2D((self.degree / 2, self.degree / 2), angle, 1)
        motion_blur_kernel = np.diag(np.ones(self.degree))
        motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (self.degree, self.degree))
        motion_blur_kernel = motion_blur_kernel / self.degree

        image = image.filter(ImageFilter.Kernel(size=(self.degree,self.degree),kernel=motion_blur_kernel.reshape(-1)))
        # blurred_image = image.filter(ImageFilter.Kernel((3,3), (1,1,1,0,0,0,2,0,2)))
        # Kernel
        return image
Ejemplo n.º 7
0
 def tranfun(self, image):
     image = getpilimage(image)
     sha = ImageEnhance.Sharpness(image)
     return sha.enhance(random.uniform(self.lower, self.upper))
Ejemplo n.º 8
0
 def tranfun(self, image):
     image = getpilimage(image)
     col = ImageEnhance.Color(image)
     return col.enhance(random.uniform(self.lower, self.upper))
Ejemplo n.º 9
0
 def tranfun(self, image):
     image = getpilimage(image)
     bri = ImageEnhance.Brightness(image)
     return bri.enhance(random.uniform(self.lower, self.upper))
Ejemplo n.º 10
0
 def tranfun(self, image):
     image = getpilimage(image)
     image = image.filter(ImageFilter.GaussianBlur(radius=1.5))
     # blurred_image = image.filter(ImageFilter.Kernel((3,3), (1,1,1,0,0,0,2,0,2)))
     # Kernel
     return image
Ejemplo n.º 11
0
 def tranfun(self, image):
     image = getpilimage(image)
     rot = random.uniform(self.lower, self.upper)
     trans_img = image.rotate(rot, expand=True)
     # trans_img.show()
     return trans_img