Python - 计算图像中不同像素的数量

时间:2016-06-29 01:07:09

标签: python image

我正在研究一个Python脚本,它计算图像中唯一像素的数量,然后将类似像素(如100,100, 101 )和(100,100,100)聚类成单个像素说(100,100,100)。我的脚本似乎工作得很好。我拍摄了一张50 x 50像素(总共2500张)的图像,并在其上运行了大约17000个独特像素的脚本,然后脚本将其缩小到大约750像素并写入输出文件。他们似乎是对的。但是,当我再次使用该输出文件作为输入时。我得到的是,唯一像素的数量实际上高于输入(大约1800)。怎么会这样? Image.fromarray()方法最终会修改我作为参数传递的numpy数组吗?

import numpy
from numpy import linalg as LA
from PIL import Image

pixel_mapping = {}

def array_in_twod(one, two):
    for i in range(len(two)):
        if (numpy.array_equal(one, two[i])): #print 'found duplicate'
            return True
    return False

def close_enough(pixel, pixels, threshold): #for i in range(len(pixels)): #if (man_norm(pixel, pixels[i]) < threshold): #return pixels[i]#
    for i in range(len(pixels)):
        if(man_norm(pixels[i], pixel) < threshold):
            return pixels[i]
    return pixel

def man_norm(v1, v2):
    #print 'V1: ', v1, ' V2: ', v2
    if v1.shape[0] != v2.shape[0]: #Raise new ValueError('Lengths are not the same')
        print 'do nothing'
    else:
        length = v1.shape[0]
        diff = 0
        for i in range(length):
            #prevent overflow by subtracting larger number from smaller in
            #unsigned 8 bit integer
            if v1[i] > v2[i]:
                diff += v1[i] - v2[i]
            else:
                diff += v2[i] - v1[i]
    return diff
def compress_array(array):
    length = array.shape[0]
    width = array.shape[1]
    numPixels = array.shape[2]
    pixelMash = []
    for i in range(length):
        for j in range(width):
            arr = array[i][j]
            pixelMash.append(arr)
    return pixelMash
def distinct_pixels(array):
    no_dup = []
    for i in range(len(array)):
        curr_arr = array[i]# print curr_arr
        if array_in_twod(curr_arr, no_dup) == False:
            no_dup.append(curr_arr)
    #print 'Final result: ', len(no_dup)
    return no_dup

def totuple(a):
    try:
        return tuple(totuple(i) for i in a)
    except TypeError:
        return a

def cluster_pixels(array, threshold):
    clustered = []
    for i in range(len(array)):
        curr_pixel = array[i]
        output_pixel = close_enough(curr_pixel, array, threshold)
        if numpy.array_equal(curr_pixel, output_pixel):
            clustered.append(curr_pixel) #found no close enough matches
            curr_pixel = totuple(curr_pixel)
            if pixel_mapping.get(totuple(curr_pixel)) == None:
                pixel_mapping[curr_pixel] = curr_pixel
        else:
            if pixel_mapping.get(totuple(curr_pixel)) == None:
                curr_pixel = totuple(curr_pixel)
                pixel_mapping[curr_pixel] = output_pixel
            clustered.append(output_pixel)
    return numpy.array(clustered)



def tabulate_final_image(raw_picture):
    length = raw_picture.shape[0]
    width = raw_picture.shape[1]
    numElem = raw_picture.shape[2]

    result_picture = numpy.zeros((length, width, numElem))

    for i in range(length):
        for j in range(width):
            curr_pixel = raw_picture[i][j]
            mapped_pixel = pixel_mapping[totuple(curr_pixel)]
            result_picture[i][j] = numpy.array(mapped_pixel).astype('uint8')
    #print result_picture
    return result_picture


def fun():
    raw_picture = numpy.asarray(Image.open('poster_normaliza_jj.jpg'))
    unfiltered_pixels = compress_array(raw_picture)
    #print len(unfiltered_pixels)
    distinct_pixels = distinct_pixels(unfiltered_pixels)
    #print len(distinct_pixels)
    clustered_pixels = cluster_pixels(distinct_pixels, 7)
    #print len(cluster_pixels)
    no_duplicate_final =  distinct_pixels(clustered_pixels)
    print no_duplicate_final

'''
test out helper functions
print man_norm(numpy.array([1, 2, 1]), numpy.array([2, 2, 4]))

bank = numpy.array([[1, 2, 2], [1, 2, 5], [1, 4, 6]])
src = numpy.array([1, 6, 2])

print close_enough(src, bank, 5)
'''

fileName = 'out_out_out_out_out_out_poster_jj.jpg'

raw_picture = numpy.asarray(Image.open(fileName))
unfiltered_pixels = compress_array(raw_picture)
distinct_pixels_result = distinct_pixels(unfiltered_pixels)

print 'Number of Distinct Pixels Before', len(distinct_pixels_result)

clustered_pixels = cluster_pixels(distinct_pixels_result, 100)
no_duplicate_final =  distinct_pixels(clustered_pixels)
out_picture = tabulate_final_image(raw_picture)

#print raw_picture.shape

unfiltered_pixels = compress_array(out_picture)
distinct_pixels_result = distinct_pixels(unfiltered_pixels)
print 'Number of Distinct Pixels After', len(distinct_pixels_result)

svimg=Image.fromarray(out_picture.astype('uint8'))
svimg.save('out_' + fileName)

#print no_duplicate_final

0 个答案:

没有答案