python 我的工作涉及计算机视觉,使用PIL在谷歌地图中查找屏幕截图上的模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 我的工作涉及计算机视觉,使用PIL在谷歌地图中查找屏幕截图上的模式相关的知识,希望对你有一定的参考价值。

"""

This is a scratch demo I made when I was feeling out image
recognition in python. Anyone is free to use it, just know
it's scratchpad worthy work.

    - Cody Kochmann

"""

from collections import deque
import itertools
from PIL import Image
from os import system

def moving_average(iterable,n=3):
    # moving_average([1,3,5,2,4,6]) -- [3,3,3,4]
    # by: codykochmann
    tmp=[]
    for i in iterable:
        tmp.append(i)
        if len(tmp)==n:
            yield sum(tmp)/n
            tmp.pop(0)

def plot_list(input_list_or_generator):
    # easily plot a list in python
    # by: codykochmann
    import numpy as np
    import matplotlib.pyplot as plt
    x = []
    y = []
    for i in input_list_or_generator:
        x.append(len(x)+1)
        y.append(i)
    plt.plot(x,y)
    plt.show()

def prettyfy(input_json, desired_indent=4):
    """ returns a prettyfied string of a json object """
    from json import dumps
    return dumps(input_json, sort_keys=True, indent=desired_indent, separators=(',', ': '))

def show_image(image_path):
    system('nohup eog {} &'.format(image_path))

def load_image(image_path):
    return Image.open(image_path)

def save_image(image_object,image_path):
    print 'saving: {}'.format(image_path)
    image_object.save(image_path)

def rgb_black_and_white(r,g,b):
    return tuple([int((r+g+b)/3)]*3)

def list_pixels(image_object):
    # for faster and cleaner looping
    height,width = image_object.size
    for h in range(height):
        for w in range(width):
            yield(h,w)

def list_pixel_values(image_object):
    # for faster and cleaner looping
    height,width = image_object.size
    pix = image_object.load()
    for h in range(height):
        for w in range(width):
            r,g,b=pix[h,w]
            yield(r,g,b)

def to_black_and_white(image_object):
    #return image_object.convert('1')
    pix=image_object.load()
    for h,w in list_pixels(image_object):
        pix[h,w] = rgb_black_and_white(pix[h,w][0],pix[h,w][1],pix[h,w][2])
    return image_object

def map_image_colors(image_object, black_and_white=True):
    # yeilds all colors found in an image
    if black_and_white:
        image_object=to_black_and_white(image_object)
    pix=image_object.load()
    colors_found={}
    for h,w in list_pixels(image_object):
        r,g,b = pix[h,w]
        color = str(tuple((r,g,b)))
        if color not in colors_found:
            colors_found[color]=deque()
            colors_found[color].append([h,w])
        else:
            colors_found[color].append([h,w])
    for c in colors_found:
        colors_found[c]=list(colors_found[c])
    return colors_found

def blackout_image(image_object):
    pix = image_object.load()
    for h,w in list_pixels(image_object):
        pix[h,w]=(0,0,0)
    return image_object

def list_json_keys_by_val_size(input_json):
    longest_len=0
    for k in input_json:
        if len(input_json[k])>longest_len:
            longest_len=len(input_json[k])
    while longest_len>=0:
        for k in input_json:
            if len(input_json[k]) is longest_len:
                yield k,len(input_json[k])
        longest_len-=1

def plot_colors(image_object):
    color_map = map_image_colors(image_object)
    tmp = [0]*255
    for k,l in list_json_keys_by_val_size(color_map):
        tmp[int(eval(k)[0])]=l
    plot_list(tmp)
    plot_list((i for i in moving_average(tmp,10)))

def find_grouped_colors(image_object):
    color_map = map_image_colors(image_object)
    for k in color_map:
        color = eval(k)
        single_color_image=blackout_image(image_object)
        pix = single_color_image.load()
        for px in color_map[k]:
            pix[px[0],px[1]]=color
        file_name="processesing/{}.{}.{}.png".format(color[0],color[1],color[2])
        save_image(single_color_image, file_name)


def gen_to_list(input_generator):
    # basically runs through a generator and returns
    # its outputs as a list
    out = deque()
    for i in input_generator:
        out.append(i)
    return list(out)


image_path = 'scratch.png'
output_path = 'output.png'
input_image=load_image(image_path)
find_grouped_colors(input_image)
plot_colors(input_image)

im = to_black_and_white(input_image)
save_image(im, output_path)
show_image(output_path)
print 'done'

以上是关于python 我的工作涉及计算机视觉,使用PIL在谷歌地图中查找屏幕截图上的模式的主要内容,如果未能解决你的问题,请参考以下文章

Python计算机视觉编程-第一章 图像处理基础

《计算机视觉和图像处理简介 - 中英双语版》:使用 PIL (Python Image Library)对图像进行滤波Spatial_Filtering

《计算机视觉和图像处理简介 - 中英双语版》:使用 PIL (Python Image Library)对图像进行几何变换及数学变换Geometric Operations

python计算机视觉

使用 PIL 在 OpenCV Python 中更改字体系列

Python 计算机视觉—— OpevCV进行直方图统计