Exemplo n.º 1
0
def find_rex(mouse, keyboard):
    mouse.click(10, 100)
    time.sleep(0.1)
    keyboard.tap_key(keyboard.up_key)
    time.sleep(0.5)
    img = grab()
    img = img.filter(ImageFilter.MinFilter(3))
    rex_on_ground = np.array(img)[:, :, 0] / 128
    time.sleep(0.1)
    keyboard.tap_key(keyboard.up_key)
    time.sleep(0.05)
    img = grab()
    img = img.filter(ImageFilter.MinFilter(3))
    rex_jump = np.array(img)[:, :, 0] / 128
    diff = zip(*np.where(rex_on_ground != rex_jump))
    km = KMeans(n_clusters=2).fit(diff)
    centers = [list(s) for s in km.cluster_centers_[:, [1, 0]]]
    centers.sort()
    x, y = [int(s) for s in centers[0]]
    time.sleep(1)
    radius = 50
    rex_pixels = np.array(
        zip(*np.where(
            rex_on_ground[y - radius:y + radius, x - radius:x + radius] != 1)))
    rex_center = rex_pixels.mean(axis=0)
    dy, dx = [int(round(s - radius)) for s in rex_center]
    cy = y + dy
    cx = x + dx
    w = min(800, rex_on_ground.shape[1] - cx)
    return cx, cy, w
Exemplo n.º 2
0
def number(url):

    response = requests.get(url)
    img_original = Image.open(BytesIO(response.content))

    # left, up, right, bottom
    borders = [
        (0, 550, 2260, 1350),
        (2300, 400, 2900, 680),
    ]

    img = img_original.crop(borders[0])
    img = img.resize((150, 52), PIL.Image.LANCZOS)  # ratio 0,35
    img = img.filter(ImageFilter.MaxFilter(3))
    img = img.filter(ImageFilter.MinFilter(3))
    img = img.getchannel('R')
    img_badania = img.point(lambda x: 0 if x < 140 else 255)
    # img_badania.show()

    # Negatywne
    img = img_original.crop(borders[1])
    img = img.resize((150, 70), PIL.Image.LANCZOS)
    img = img.filter(ImageFilter.MaxFilter(3))
    img = img.filter(ImageFilter.MinFilter(3))
    img = img.point(lambda x: 0 if x < 200 else 255)
    img_negatywne = img.getchannel('R')
    # img_negatywne.show()

    res_badania = pytesseract.image_to_string(img_badania,
                                              lang='eng',
                                              config='--psm 6 --oem 3')

    try:
        res_badania = int(res_badania)
    except ValueError:
        # raise BaniaOCRException(f'OCR return non digit while processing BADANIA: {res_badania}')
        return None, None

    res_negatywne = pytesseract.image_to_string(img_negatywne,
                                                lang='eng',
                                                config='--psm 6 --oem 3')

    try:
        res_negatywne = int(res_negatywne)
    except ValueError:
        # raise NegatywneOCRException(f'OCR return non digit while processing NEGATYWNE: {res_negatywne}')
        return None, None

    return (res_badania, res_negatywne)
Exemplo n.º 3
0
def random_mask_perturbation(mask, verbose=False):

    degrees = random.uniform(-10, 10)
    translate_h = random.uniform(-0.1, 0.1)
    translate_v = random.uniform(-0.1, 0.1)
    scale = random.uniform(0.8, 1.2)
    shear = random.uniform(-10, 10)
    resample = Image.BICUBIC

    mask = TF.affine(mask, degrees, (translate_h, translate_v), scale, shear, resample)

    if verbose:
        print('Mask pertubation degrees: %.1f, T_h: %.1f, T_v: %.1f, Scale: %.1f, Shear: %.1f' % \
            (degrees, translate_h, translate_v, scale, shear))

    morphologic_times = int(random.random() * 10)
    morphologic_thres = random.random()
    filter_size = 7
    for i in range(morphologic_times):
        if random.random() < morphologic_thres:
            mask = mask.filter(ImageFilter.MinFilter(filter_size))
            if verbose:
                print(i, 'erossion')
        else:
            mask = mask.filter(ImageFilter.MaxFilter(filter_size))
            if verbose:
                print(i, 'dilation')

    mask = mask.convert('1')

    return mask
Exemplo n.º 4
0
def BLUR():
    global img
    im = Image.open(root.filename)
    im1 = im.filter(ImageFilter.BLUR)
    img = im1.filter(ImageFilter.MinFilter(size=3))
    canvas.image = ImageTk.PhotoImage(img.resize((500, 500), Image.ANTIALIAS))
    canvas.create_image(0, 0, image=canvas.image, anchor='nw')
Exemplo n.º 5
0
def create_png_from_tiff():
    names=[]
    Piece_colors=('b','w')
    Piece_type=('K','Q','R','N','B','p')
    for p_t in Piece_type:
        for c_t in Piece_colors:
            names.append(p_t+c_t)
    
    for name in names:        
    
        try:
           im_bw = Image.open(name+'.tif')
        except:
            print('Error read file:'+name+'.tif')
            continue
        im_rgb = im_bw.convert('RGB')
        im_rgb=ImageOps.expand(im_rgb, border=6, fill=(255, 255, 255),)# add white 5px border
        #im_rgb = im_bw.convert('L')
        #im_rgb=ImageOps.expand(im_rgb, border=6, fill=(255),)
        im_mask=im_rgb.copy()
        im_mask=im_mask.filter(ImageFilter.MinFilter(5))# create errode image
        im_mask2=im_mask.copy()
        ImageDraw.floodfill(im_mask, (0,0), value=(0,0,0))# flood fill image
        #ImageDraw.floodfill(im_mask, (0,0), value=(0))
        im_mask = ImageChops.difference(im_mask, im_mask2) # difference between image
        im_mask=ImageOps.invert(im_mask)
        im_mask=im_mask.convert('L')
        im_rgb.putalpha(im_mask)
        left,upper,right,lower = im_mask.getbbox()
        im_rgb = im_rgb.crop((left,upper,right,lower))
        im_rgb.save(name+'.png')
Exemplo n.º 6
0
 def filter_footer(self, img):
     """Filter to remove the hight quality footer for an image."""
     # Some sites like MangaFox add an extra footer in the original
     # image.  This footer remove importan space in the Kindle, and
     # we need to remove it.
     #
     # The algorithm use as a leverage the normal noise present in
     # an scanned image, that is higher than the one in the footer.
     # This means that this filter will only work in medium quality
     # scanners, but possibly not in high quality ones.
     #
     # The process is like this:
     #
     #   1.- Binarize the image, moving the noise at the same level
     #       that the real information.
     #
     #   2.- Use a MinFilter of size 3 to a big mass of pixels that
     #       containg high frequency data.  That usually means
     #       pixels surrounded with blanks.
     #
     #   3.- Do a Gaussian filter to lower more the high frequency
     #       data, moving the mass close arround the pixel.  This
     #       will lower more the pixels surrounded with gaps.
     #
     #   4.- Discard the pixels with low mass.
     #
     _img = ImageOps.invert(img.convert(mode='L'))
     _img = _img.point(lambda x: x and 255)
     _img = _img.filter(ImageFilter.MinFilter(size=3))
     _img = _img.filter(ImageFilter.GaussianBlur(radius=5))
     _img = _img.point(lambda x: (x >= 48) and x)
     # If the image is white, we do not have bbox
     return img.crop(_img.getbbox()) if _img.getbbox() else img
Exemplo n.º 7
0
def erode(image, radius, iterations=1):
    """Apply erosion to an image.

    Parameters
    ----------
    image : :class:`numpy.ndarray` or :class:`Image.Image`
        The image object.
    radius : :class:`int`
        The number of pixels to include in each direction. For example, if
        `radius`=1 then use 1 pixel in each direction from the central pixel,
        i.e., 9 pixels in total.
    iterations : :class:`int`, optional
        The number of times to apply erosion.

    Returns
    -------
    The `image` with erosion applied.
    """
    if radius is None or radius < 1 or iterations < 1:
        return image
    size = 2 * radius + 1
    if isinstance(image, np.ndarray):
        kernel = np.ones((size, size), dtype=np.uint8)
        return cv2.erode(image, kernel, iterations=iterations)
    elif isinstance(image, Image.Image):
        for i in range(iterations):
            image = image.filter(ImageFilter.MinFilter(size=size))
        return image
    else:
        raise TypeError('Expect a PIL or OpenCV image')
Exemplo n.º 8
0
def get_text(im):

    lab_im = rgb_to_lab(im)

    lab_im = rescale(lab_im)
    l, a, b = lab_im.split()

    # Convert to numpy array and apply the threshold to remove lines
    np_a = np.array(a)
    np_a = threshold(np_a, 180, 0, 255)

    # Invert the image: we want black text on a white background
    np_a = 255 - np_a

    a = Image.fromarray(np_a)

    # Expand image to close up "gaps" in letters, shrink to
    # stop letters running together
    a_filtered = a.filter(ImageFilter.MinFilter(11))
    a_filtered = a_filtered.filter(ImageFilter.MaxFilter(5))

    # It's useful to save this pre-OCR step to identify issues
    a_filtered.save('filtered.png')

    # Run OCR and get the result
    result = pytesseract.image_to_string(a_filtered)
    result = apply_corrections(result)

    return result
Exemplo n.º 9
0
def create_mask(img_filepath, parameter_file, angle_file, num):
	'''
	Parameters: img_filepath (string)-- file path of img
		    parameter_file (string)-- camera parameter file path
		    angle_file (string)-- camera angle parameter file path
		    num (int)-- img number, used to refer which line of parameter files contains camera configuration info for snapshot
	Returns: tuple of...
		 1) img-- mask as open3d image
		 2) open3d pinhole camera params object
		 	intrinsic camera matrix (K)
		 	extrinsic camera matrix (R|t) with extra row from identity matrix to make a 4x4 matrix
	'''
	img = Image.open(img_filepath).convert('1').filter(ImageFilter.BLUR).filter(ImageFilter.MinFilter(3)).filter(ImageFilter.MinFilter)
	img = img.resize((int(img.size[0]), int(img.size[1])))
	#plt.imshow(img)
	#plt.show()
	img = np.asarray(img, dtype='float32')
	
	parameter_str = linecache.getline(parameter_file, num + 1).split()[1:]
	angle_str = linecache.getline(angle_file, num).split()[:-1]

	K = np.array([[float(parameter_str[0]), float(parameter_str[1]), float(parameter_str[2])], [float(parameter_str[3]), float(parameter_str[4]), float(parameter_str[5])], [float(parameter_str[6]), float(parameter_str[7]), float(parameter_str[8])]]);
	R = np.array([[float(parameter_str[9]), float(parameter_str[10]), float(parameter_str[11])], [float(parameter_str[12]), float(parameter_str[13]), float(parameter_str[14])], [float(parameter_str[15]), float(parameter_str[16]), float(parameter_str[17])]]);
	t = np.array([[float(parameter_str[18])], [float(parameter_str[19])], [float(parameter_str[20])]]);
	extrinsic = np.vstack((np.hstack((R, t)), np.array([[0.0, 0.0, 0.0, 1.0]], dtype='float32')))
	camera_params = o3d.camera.PinholeCameraParameters()	
	camera_params.extrinsic = extrinsic
	camera_params.intrinsic.set_intrinsics(img.shape[1], img.shape[0], K[0,0], K[1,1], K[0,2], K[1,2])
	return (o3d.geometry.Image(img), camera_params)
Exemplo n.º 10
0
    def downloader(self, url, counter, parser):
        # Check if we have the Download folder
        helper.createFolder(self.downloadfolder)

        imagepath = self.downloadfolder + "/" + str(
            "{0:0=3d}".format(counter)) + ".png"
        tempdl = self.downloadfolder + "/" + str(
            "{0:0=3d}".format(counter)) + ".tmp"

        # Download the image!
        f = open(tempdl, 'wb')
        f.write(requests.get(parser(url)).content)
        f.close()

        # If everything is alright, write image to final name
        os.rename(tempdl, imagepath)

        # Cleanse image, remove footer
        #
        #   I have borrowed this code from the kmanga project.
        #   https://github.com/aplanas/kmanga/blob/master/mobi/mobi.py#L416
        #   Thanks a lot to Alberto Planas for coming up with it!
        #
        if self.origin == "mangafox.me" or self.origin == "mangafox.la" or self.origin == "fanfox.net":
            logging.debug("Cleaning Mangafox Footer")
            img = Image.open(imagepath)
            _img = ImageOps.invert(img.convert(mode='L'))
            _img = _img.point(lambda x: x and 255)
            _img = _img.filter(ImageFilter.MinFilter(size=3))
            _img = _img.filter(ImageFilter.GaussianBlur(radius=5))
            _img = _img.point(lambda x: (x >= 48) and x)

            cleaned = img.crop(_img.getbbox()) if _img.getbbox() else img
            cleaned.save(imagepath)
Exemplo n.º 11
0
    def _get_masks(self, size, start_idx, end_idx, fg_dir):
        input_len = end_idx - start_idx
        if self.mask_type == 'fg':
            masks = SegmentationReader(fg_dir)[start_idx:end_idx]
            if self.mask_dilation > 0:
                masks = [
                    m.filter(ImageFilter.MinFilter(self.mask_dilation))
                    for m in masks
                ]
        elif self.mask_type == 'as_video':
            masks = FrameReader(fg_dir)[start_idx:end_idx]
        elif self.mask_type == 'random':
            masks = FrameReader(fg_dir)
            # This is to sample a random clip of the mask video.
            # If gt video is longer than mask video, mask_type="random" may yeild zero masks, then
            # using mask_type='random_segment can solve it'
            max_start = max(0, len(masks) - input_len - 1)
            start = random.randint(0, max_start)
            masks = masks[start:start + input_len]
        elif self.mask_type == 'from_start':
            masks = FrameReader(fg_dir)[0:end_idx - start_idx]
        elif self.mask_type == 'davis':
            masks = DAVISReader(fg_dir)[start_idx:end_idx]
        else:
            raise NotImplementedError(f"Mask type {self.mask_type} not exists")
        if len(masks) != input_len:
            assert len(masks) < input_len
            # when mask video is shorter than input video, repeat last mask frame
            masks += [masks[-1]] * (input_len - len(masks))

        return masks
Exemplo n.º 12
0
 def min_filter(self, kernel):  # aplicar o filtro MINIMO
     im = img.open(self.filename)
     image = im.filter(ImageFilter.MinFilter(kernel))
     new_file_name = "./images/temporarias/" + os.path.basename(
         self.filename)
     image.save(new_file_name)
     return new_file_name
Exemplo n.º 13
0
 def open_image(self):
     """Open an PIL Image from file."""
     self.orig_image = Image.open(self.filename)
     if self.in_rgb:
         self.orig_image = self.orig_image.convert("RGB")
     if self.min_filter:
         self.orig_image.filter(ImageFilter.MinFilter(self.min_filter))
Exemplo n.º 14
0
def filter_image(lst_digits, img_path):
    ii = 0
    for img in lst_digits:
        # filtering
        _ = re.findall(r'(.+).jpg', img_path)
        save_path_img = _[0]

        # rescale the image
        basewidth = 300
        wpercent = (basewidth / float(img.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        img = img.resize((basewidth, hsize), Image.ANTIALIAS)
        # erosion and blur filter
        img = img.filter(ImageFilter.MinFilter(3))
        img = img.filter(ImageFilter.GaussianBlur(3))

        # mask to filter the numbers
        img = img.convert("L")
        th = 155  # the value has to be adjusted for an image of interest
        img = img.point(lambda i: i < th and 255)
        img = PIL.ImageOps.invert(img)  # invert image
        img.save('{}_04_digit_{:.0f}_filtered.png'.format(
            save_path_img, ii + 1))

        ii = ii + 1
Exemplo n.º 15
0
def TextParser(x1,y1,x2,y2):
	boxSize = (x1,y1,x2,y2)
	
	# Infinite While Loop
	while 1:
		
		# Grab Image
		img = pyscreenshot.grab(boxSize)
		
		# PreProcess Image
		newSize = tuple(3*x for x in img.size)
		img = img.resize(newSize, Image.ANTIALIAS)
		#img = img.convert("LA")
		#img= img.point(lambda x: 0 if x < 200 else 255)
		
		## ProvidingFilters
		img = img.filter(ImageFilter.SHARPEN)
		img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
		img = img.filter(ImageFilter.MinFilter(size=3))
		img.show()
		# Parse using Tesseract
		text = pytesseract.image_to_string(img)
		
		# Appending Text
		print(text)
		break
Exemplo n.º 16
0
def Board2coord(name):
    try:
           im_board = Image.open(name)
    except:
            print('Error read file')
    im_board=im_board.convert('L')
    [w,h]=im_board.size
    
    draw_rect=im_board.copy()
    draw_rect = draw_rect.filter(ImageFilter.MinFilter(15)) # morphology closing
    draw_rect = draw_rect.filter(ImageFilter.MaxFilter(15))
    #draw_rect.save('test.png')
    w_w=(3*cell(draw_rect,'3w2b_w')[0]-2*cell(draw_rect,'3b2w_w')[0])//5 
    b_w=(3*cell(draw_rect,'3b2w_w')[0]-2*cell(draw_rect,'3w2b_w')[0])//5
    w_board=cell(draw_rect,'3w2b_w')[0]+cell(draw_rect,'3b2w_w')[0]-w_w-b_w
    x0=cell(draw_rect,'3b2w_w')[1]-w_w 
  
    w_h=(3*cell(draw_rect,'3w2b_h')[0]-2*cell(draw_rect,'3b2w_h')[0])//5 
    b_h=(3*cell(draw_rect, '3b2w_h')[0]-2*cell(draw_rect, '3w2b_h')[0])//5
    h_board=(cell(draw_rect,'3w2b_h')[0]+cell(draw_rect,'3b2w_h')[0])*8//10
    y0=cell(draw_rect,'3b2w_h')[1]-w_h  
    return(x0, y0, w_board, h_board)



    
Exemplo n.º 17
0
    def compDiffScore(self, file):
        self.diffScore = 0
        ref = self._getImage()
        testee = self._getTestee()
        if testee.size != ref.size:
            file.write(
                "WARNING: Reference image from source has different dimension than the testee image"
            )
            #raise ValueError("Reference image from source has different dimension than the testee image")
        # If a difference exists...
        if self.maxDiff != 0:
            # Filter images for min and max pixel (dark and light) values within 5x5 environment.
            refMin = ref.filter(ImageFilter.MinFilter(5))
            refMax = ref.filter(ImageFilter.MaxFilter(5))
            testMin = testee.filter(ImageFilter.MinFilter(5))
            testMax = testee.filter(ImageFilter.MaxFilter(5))

            # make the min and max filter images a bit darker and lighter, respectively.
            refMin = Image.eval(refMin, lambda x: x - 4)
            refMax = Image.eval(refMax, lambda x: x + 4)
            testMin = Image.eval(testMin, lambda x: x - 4)
            testMax = Image.eval(testMax, lambda x: x + 4)

            refRefHist = ref.histogram()
            testRefHist = testee.histogram()

            # Calculate difference score.

            # Check for darkness in reference image.
            # Generate an image of the darkest pixels when comparing the 5x5 max filtered and lightened reference image against the test image.
            # If the pixel colour histogram of the generated image is different from the test image histogram, increase the difference score.
            if (ImageChops.darker(refMax, testee).histogram() != testRefHist):
                self.diffScore += 1

            # Check for lightness in reference image.
            if (ImageChops.lighter(refMin, testee).histogram() != testRefHist):
                self.diffScore += 1

            # Check for darkness in test image.
            if (ImageChops.darker(testMax, ref).histogram() != refRefHist):
                self.diffScore += 1

            #  Check for lightness in test image.
            if (ImageChops.lighter(testMin, ref).histogram() != refRefHist):
                self.diffScore += 1

        print "self.diffScore: ", self.diffScore
Exemplo n.º 18
0
def scanImage(chat_id, file_path):
    im = Image.open(
        file_path)  #NOTE: WITH 500X500 IMG NO PROBLEM TO READ THE TEXT
    text = ''
    enchanceIndex = 1  #per fare una prova ho visto che da 12 in poi legge bene
    w, h = im.size
    im.crop((0, 0, w, h - 250)).save("temp.jpg")
    im2 = Image.open("temp.jpg")  #.convert('L')
    #im2.show()
    ReadedText = []
    enhancer = ImageEnhance.Contrast(im2)
    im2 = im2.filter(ImageFilter.MinFilter(3))
    d = enchant.DictWithPWL("en_US", "MagicCardName.txt")
    while enchanceIndex <= 15:  #Testing
        im2 = enhancer.enhance(enchanceIndex)
        im2 = im2.convert('1')
        #im2.show()
        text = (pytesseract.image_to_string(im2, lang='ita'))
        #print (text)
        print('\nValore contrasto= ', enchanceIndex)
        enchanceIndex += 1
        if text != '':
            #ReadedText.append(text)
            print('\n---------Name of Cards---------\n')
            print('Testo rilevato ', text)
            print('Testi suggeriti ', d.suggest(text))
            suggerimenti = d.suggest(text)
            if (len(suggerimenti) > 0):
                print('Ricerca...')
                for s in suggerimenti:
                    if s == text:
                        cardToSearch = s
                    else:
                        cardToSearch = suggerimenti[
                            0]  #quella con maggior probabilità di essere esatta

                print('Cerca -> ', cardToSearch)
                cards = Card.where(name=cardToSearch).all()
                if (len(cards) > 0):
                    #for c in cards:
                    print(cards[0].name, ' ', cards[0].cmc, cards[0].colors)
                    send_message(
                        str(cards[0].name) + " " + str(cards[0].cmc) + " " +
                        str(cards[0].colors), chat_id)
                    break
                else:
                    cardsITA = Card.where(language="Italian").where(
                        name=cardToSearch).all()
                    if (len(cardsITA) > 0):
                        #for c in cardsITA:
                        print(cardsITA[0].name, ' ', ' costo= ',
                              cardsITA[0].cmc, ' colore= ', cardsITA[0].colors)
                        send_message(
                            str(cardsITA[0].name) + " " +
                            str(cardsITA[0].cmc) + " " +
                            str(cardsITA[0].colors), chat_id)
                        break
    send_message("Mi dispiace ma non sono riuscito a decifrare la foto",
                 chat_id)
Exemplo n.º 19
0
def minfunc(value, img):
    """Applies the min filter to 'img'"""
    try:
        value = np.clip(int(value), 0, 17)
    except:
        raise Exception("Argument error for min")

    return img.filter(ImageFilter.MinFilter(value))
Exemplo n.º 20
0
    def compPyramidDiff(self):
        ref = self._getImage()
        testee = self._getTestee()
        #if testee.size != ref.size:
        #	file.write("WARNING: The reference image has different dimension from the testee image")

        # maskImage is the difference between min and max pixels within a 3x3 pixel environment in the reference image.
        maskImage = ImageChops.difference(ref.filter(ImageFilter.MinFilter(3)),
                                          ref.filter(ImageFilter.MaxFilter(3)))

        # generate low-pass filtered pyramid images.
        refLevels = self._genPyramidLevels(ref)
        refL1 = refLevels[0]
        refL2 = refLevels[1]
        refL3 = refLevels[2]
        testLevels = self._genPyramidLevels(testee)
        testL1 = testLevels[0]
        testL2 = testLevels[1]
        testL3 = testLevels[2]
        maskLevels = self._genPyramidLevels(maskImage)

        # Apply weighting factor to masks at levels 1, 2, and 3.
        maskL1 = Image.eval(maskLevels[0], lambda x: 5 * x)
        maskL2 = Image.eval(maskLevels[1], lambda x: 3 * x)
        maskL3 = Image.eval(maskLevels[2], lambda x: 2 * x)

        # Generate a pixel difference image between reference and test.
        # Multiply the difference image with the inverse of the mask.
        #   Mask inverse (out = MAX - image):
        #     So, areas of regional (3x3) similarity thend to MAX and differences tend to 0x00.
        #   Multiply (out = image1 * image2 / MAX:
        #     Superimposes two images on top of each other. If you multiply an image with a solid black image,
        #     the result is black. If you multiply with a solid white image, the image is unaffected.
        #   This has the effect of accentuating any test/reference differences where there is a small
        #   regional difference in the reference image.
        diffL1 = ImageChops.difference(refL1, testL1)
        diffL1 = ImageChops.multiply(diffL1, ImageChops.invert(maskL1))
        diffL2 = ImageChops.difference(refL2, testL2)
        diffL2 = ImageChops.multiply(diffL2, ImageChops.invert(maskL2))
        diffL3 = ImageChops.difference(refL3, testL3)
        diffL3 = ImageChops.multiply(diffL3, ImageChops.invert(maskL3))

        # So now the difference images are a grey-scale image that are brighter where differences
        # between the reference and test images were detected in regions where there was little
        # variability in the reference image.

        # Get maxima for all bands at each pyramid level, and take the maximum value as the pyramid value.
        # stat.extrema (Get min/max values for each band in the image).

        self.pyramidDiffs = [
            max(map(lambda (x): x[1],
                    ImageStat.Stat(diffL1).extrema)),
            max(map(lambda (x): x[1],
                    ImageStat.Stat(diffL2).extrema)),
            max(map(lambda (x): x[1],
                    ImageStat.Stat(diffL3).extrema))
        ]
        print "self.pyramidDiffs = ", self.pyramidDiffs
Exemplo n.º 21
0
def loadData(size=180, mode='1', rng=0):
    m = {'RGB': 3, 'L': 1, '1': 1}
    inputs = []
    outputs = []
    datasets = [i for i in sets]
    if datasets == []:
        datasets = ['a','b','c','d','e']


    total_num = 0
    for dataset in datasets:
        labels = pd.read_csv(src + "/training-{0}.csv".format(dataset))
        if rng == 0:
            labels = labels[['filename', 'digit']]
        else:
            labels = labels[['filename', 'digit']][rng[0]:rng[1]]

        length = min(len(labels), MAX_LABELS)

        Y = np.array(labels['digit'][range(length)],dtype=np.uint8)

        X = np.zeros((length, size, size))
        num = 0
        # if progressInstalled:
        #     bar = ChargingBar('Loading Training Set {0}'.format(dataset),max = len(labels),
        #                       suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds')
        for i in labels['filename'][range(length)]:
            img = Image.open(src + "/training-{0}/".format(dataset) + i)
            img = trim(img)
            img = img.filter(ImageFilter.GaussianBlur(radius=2))
            threshold = 191  
            img = img.point(lambda p: p > threshold and 255)
            img = img.filter(ImageFilter.MinFilter(size=5))
            # img = invertImageIfnecessary()
            img = img.resize((size, size))
            #img.show()

            c = np.array(img.convert(mode), dtype=np.uint8).reshape((size,size))
            X[num, :, :] = c
            num = num + 1
            # if progressInstalled and (not num % max(1,(length / 200))):
            #     bar.index = num
            #     bar.update()
            # else:
            if (not num % (length / 20)):
                print("Loaded Training Set {0}:. ".format(dataset) + str(num) + "/" + str(length))
        # if progressInstalled:
        #     bar.finish()

        total_num += num

        inputs.append(X)
        outputs.append(Y)

    X = np.concatenate(inputs,axis = 0)
    Y = np.concatenate(outputs,axis = 0)
    return (X.reshape(total_num, -1), Y)
 def noise_removal(self):
     '''
     returns a noise-free image(an image without noise)
     blur an image then apply minimum filter to it
     '''
     im1 = self.original.filter(ImageFilter.BLUR)
     im2 = self.original.filter(ImageFilter.MinFilter(3))
     saved = im2.save('noise-free.png')
     return './noise-free.png'
Exemplo n.º 23
0
def recognize():
    im = Image.open('f**k.png')
    im = im.crop((360, 290, 440, 310))
    width, height = im.size
    im = im.resize((width * 2, height * 2))
    im = im.filter(ImageFilter.MinFilter(3))
    enhancer = ImageEnhance.Contrast(im)
    im = enhancer.enhance(20)
    im = im.convert('1')
    im = im.filter(ImageFilter.MaxFilter(3))
    #im.show()
    ocr_result = pytesseract.image_to_string(
        im, config="-c tessedit_char_whitelist=0123456789+-=?")
    ocr_result = ocr_result.rstrip('-7').rstrip('=?').strip('-').strip(' ')

    print('raw: ', ocr_result)

    if ocr_result[0] == '0':
        ocr_result[0] = 9

    if ocr_result.find('-') != -1:
        a = clean_operator(ocr_result.split('-')[0])
        b = clean_operator(ocr_result.split('-')[-1])
        if a == 'wtf' or b == 'wtf':
            return 'wtf'
        return (a - b)
    if ocr_result.find('+') != -1:
        a = clean_operator(ocr_result.split('+')[0])
        b = clean_operator(ocr_result.split('+')[-1])
        if a == 'wtf' or b == 'wtf':
            return 'wtf'
        return (a + b)

    if ocr_result.find(' ') != -1:
        a = ocr_result.split(' ')[0]
        b = ocr_result.split(' ')[-1]
        if len(b) == 2:
            if b[0] == '7':
                a = clean_operator(a)
                b = clean_operator(b)
                if a == 'wtf' or b == 'wtf':
                    return 'wtf'
                return (a - b)
            else:
                a = clean_operator(a)
                b = clean_operator(b)
                if a == 'wtf' or b == 'wtf':
                    return 'wtf'
                return (a + b)
        else:
            a = clean_operator(a)
            b = clean_operator(b)
            if a == 'wtf' or b == 'wtf':
                return 'wtf'
            return (a - b)

    return 'wtf'
Exemplo n.º 24
0
def dilation(image):
    """This function will perform the dilation image processing technique on the provided image

        args:
            image - A Pillow image object
    """
    filter = ImageFilter.MinFilter(3)  # Should be MaxFilter if white on black
    filtered = image.filter(filter)
    return filtered
Exemplo n.º 25
0
def approach1(img, minFilter, threshold, binary_type, kernelD, n1, kernelE, n2,
              medianFilter, i):
    img = img.filter(ImageFilter.MinFilter(size=minFilter))
    img = np.array(img)
    new_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, new_img = cv2.threshold(new_img, threshold, 255,
                                 binary_type)  #convert to binary image
    new_img = dialation(new_img, kernelD, 1)
    new_img = erosion(new_img, kernelE, 1)
    new_img = cv2.medianBlur(new_img, medianFilter)
Exemplo n.º 26
0
 def grayscale_corrosion(self):
     """
     灰度处理并腐蚀
     :return:
     """
     img_array = np.array(self.image)
     img_array[np.all(img_array != np.array([255, 255, 255]),
                      axis=-1)] = [0, 0, 0]  # 灰度处理
     self.image = Image.fromarray(img_array)
     self.image = self.image.filter(ImageFilter.MinFilter(3))  # 腐蚀
Exemplo n.º 27
0
Arquivo: image.py Projeto: saoula/kcc
 def cropPageNumber(self, power):
     if self.fill != 'white':
         tmpImg = self.image.convert(mode='L')
     else:
         tmpImg = ImageOps.invert(self.image.convert(mode='L'))
     tmpImg = tmpImg.point(lambda x: x and 255)
     tmpImg = tmpImg.filter(ImageFilter.MinFilter(size=3))
     tmpImg = tmpImg.filter(ImageFilter.GaussianBlur(radius=5))
     tmpImg = tmpImg.point(lambda x: (x >= 16 * power) and x)
     self.image = self.image.crop(tmpImg.getbbox()) if tmpImg.getbbox() else self.image
Exemplo n.º 28
0
    def blur(src):
        intensity = 4
        img = Image.open('%s'%(src))
        result = img.filter(ImageFilter.MinFilter(intensity))

        result_path = os.path.dirname(src)
        str(result_path)
        result_path = (result_path+'\\')
        result.save(result_path+'blur.png')
        print('Image saved at : '+result_path+'blur.png')
Exemplo n.º 29
0
def minFilter(sourceImagePath, targetImagePath):  #7th function extra
    fact = 7
    im = Image.open(sourceImagePath)
    new = im.copy()
    new = new.filter(ImageFilter.MinFilter(fact))
    if targetImagePath != "":
        new.save(targetImagePath)
        print("saved")
    else:
        return new
Exemplo n.º 30
0
def filter_regions(region_mks, filter_size=11):

    # sort by size
    sizes = []
    reg_sm = []
    for i, reg_i in enumerate(region_mks):

        # apply min filter
        reg_i_sm = Image.fromarray(reg_i * 255.0)
        reg_i_sm = reg_i_sm.filter(ImageFilter.MinFilter(filter_size))
        reg_i_sm = np.array(reg_i_sm) / 255.0
        reg_sm.append(reg_i_sm)
        sizes.append(reg_i_sm.sum())

    inds = np.argsort(sizes)[::-1]
    sizes_sorted = np.array(sizes)[inds]
    region_mks_sorted = np.array(region_mks)[inds]
    reg_sm_sorted = np.array(reg_sm)[inds]

    # filter zero sized
    inds = np.where(sizes_sorted > 0)
    sizes_sorted = sizes_sorted[inds]
    region_mks_sorted = region_mks_sorted[inds]
    reg_sm_sorted = reg_sm_sorted[inds]

    # suppress regions
    suppressed = np.zeros(region_mks_sorted.shape[0])
    for i, reg_i in enumerate(region_mks_sorted):
        for j, reg_j in enumerate(region_mks_sorted):
            if i != j and not suppressed[i] and not suppressed[j]:
                intersec = np.logical_and(reg_sm_sorted[i], reg_sm_sorted[j])
                intersec = np.array(np.where(intersec > 0))
                if intersec.shape[1] > 0:
                    suppressed[j] = 1

                    # # DEBUG
                    # deb_1 = Image.fromarray(reg_i*255.0)
                    # deb_2 = Image.fromarray(reg_j*255.0)
                    # plt.figure()
                    # plt.imshow(deb_1)
                    # plt.figure()
                    # plt.imshow(deb_2)
                    # plt.show()

    regions_filtered = [
        m for i, m in enumerate(region_mks_sorted) if not suppressed[i]
    ]
    regions_filtered = np.array(regions_filtered)
    sizes_filtered = [
        s for i, s in enumerate(sizes_sorted) if not suppressed[i]
    ]
    sizes_filtered = np.array(sizes_filtered)
    print(sizes_filtered)

    return regions_filtered