使用Python 制作对比图片相似度的程序怎么比较

使用Python 制作对比图片相似度的程序怎么比较,第1张

需要使用Python Imaging Library,下代是python2x的代码:

from itertools import izip

import Image

 

i1 = Imageopen("image1jpg")

i2 = Imageopen("image2jpg")

assert i1mode == i2mode, "Different kinds of images"

assert i1size == i2size, "Different sizes"

 

pairs = izip(i1getdata(), i2getdata())

if len(i1getbands()) == 1:

    # for gray-scale jpegs

    dif = sum(abs(p1-p2) for p1,p2 in pairs)

else:

    dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

 

ncomponents = i1size[0]  i1size[1]  3

print "Difference (percentage):", (dif / 2550  100) / ncomponents

用BLAST程序查出的序列,不知道你要查的是什么序列,氨基酸或者是核酸序列,要看一下5个关于BLAST的选项对号入座。至于想查相似度差一些的序列,将你的目的序列输入后,基本所有的同源序列都出来了,相似度为60%~70%的也应该有的。可能是你在选择种属时出错了。

1对于标量

标量也就是无方向意义的数字,也叫标度变量。现在先考虑元素的所有特征属性都是标量的情况。例如,计算X={2,1,102}和Y={1,3,2}的相异度。一种很自然的想法是用两者的欧几里得距离来作为相异度,欧几里得距离的定义如下:

其意义就是两个元素在欧氏空间中的集合距离,因为其直观易懂且可解释性强,被广泛用于标识两个标量元素的相异度。将上面两个示例数据代入公式,可得两者的欧氏距离为:

除欧氏距离外,常用作度量标量相异度的还有曼哈顿距离和闵可夫斯基距离,两者定义如下:

欧氏距离和曼哈顿距离可以看做是闵可夫斯基距离在p=2和p=1下的特例。另外这三种距离都可以加权,这个很容易理解,不再赘述。

下面要说一下标量的规格化问题。上面这样计算相异度的方式有一点问题,就是 取值范围大的属性对距离的影响高于取值范围小的属性 。例如上述例子中第三个属性的取值跨度远大于前两个,这样不利于真实反映真实的相异度,为了解决这个问题,一般要对属性值进行规格化。所谓规格化就是将各个属性值按比例映射到相同的取值区间,这样是为了平衡各个属性对距离的影响。通常将各个属性均映射到[0,1]区间,映射公式为:

其中max(ai)和min(ai)表示所有元素项中第i个属性的最大值和最小值。例如,将示例中的元素规格化到[0,1]区间后,就变成了X’={1,0,1},Y’={0,1,0},重新计算欧氏距离约为1732。

2对于二元变量

所谓二元变量是只能取0和1两种值变量,有点类似布尔值,通常用来标识是或不是这种二值属性。对于二元变量,上一节提到的距离不能很好标识其相异度,我们需要一种更适合的标识。一种常用的方法是用元素相同序位同值属性的比例来标识其相异度。

设有X={1,0,0,0,1,0,1,1},Y={0,0,0,1,1,1,1,1},可以看到,两个元素第2、3、5、7和8个属性取值相同,而第1、4和6个取值不同,那么相异度可以标识为3/8=0375。一般的,对于二元变量,相异度可用“取值不同的同位属性数/单个元素的属性位数”标识。

上面所说的相异度应该叫做对称二元相异度。现实中还有一种情况,就是我们只关心两者都取1的情况,而认为两者都取0的属性并不意味着两者更相似。例如在根据病情对病人聚类时,如果两个人都患有肺癌,我们认为两个人增强了相似度,但如果两个人都没患肺癌,并不觉得这加强了两人的相似性,在这种情况下,改用“取值不同的同位属性数/(单个元素的属性位数-同取0的位数)”来标识相异度,这叫做非对称二元相异度。如果用1减去非对称二元相异度,则得到非对称二元相似度,也叫Jaccard系数,是一个非常重要的概念。

3对于分类变量

分类变量是二元变量的推广,类似于程序中的枚举变量,但各个值没有数字或序数意义,如颜色、民族等等,对于分类变量,用“取值不同的同位属性数/单个元素的全部属性数”来标识其相异度。

4对于序数变量

序数变量是具有序数意义的分类变量,通常可以按照一定顺序意义排列,如冠军、亚军和季军。对于序数变量,一般为每个值分配一个数,叫做这个值的秩,然后以秩代替原值当做标量属性计算相异度。

5、向量

其中||X||表示X的欧几里得范数。

要注意,余弦度量度量的不是两者的相异度,而是相似度!

参考文献:

算法杂货铺——k均值聚类(K-means) - T2噬菌体 - 博客园 >

import media

def red_average(pic):

'''Return an integer that represents the average red of the picture

'''

total=0

for pixel in pic:

total = total + mediaget_red(pixel)

red_average = total / (mediaget_width(pic)mediaget_height(pic))

return red_average

def green_average(pic):

'''Return an integer that represents the average green of the picture

'''

total = 0

for pixel in pic:

total = total + mediaget_green(pixel)

green_average = total / (mediaget_width(pic)mediaget_height(pic))

return green_average

def blue_average(pic):

'''Return an integer that represents the average blue of the picture

'''

total = 0

for pixel in pic:

total = total + mediaget_blue(pixel)

blue_average = total / (mediaget_width(pic)mediaget_height(pic))

return blue_average

def scale_red(pic, value):

'''Return the picture that the average of the red is value which has been set

'''

averaged = red_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_red = min(255, int(factor mediaget_red(pixel)))

mediaset_red(pixel,new_red)

return pic

def scale_green(pic, value):

'''Return the picture that the average of the green is value which has been set

'''

averaged = green_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_green = min(255, int(factor mediaget_green(pixel)))

mediaset_green(pixel,new_green)

return pic

def scale_blue(pic, value):

'''Return the picture that the average of the blue is value which has been set

'''

averaged = blue_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_blue = min(255, int(factor mediaget_blue(pixel)))

mediaset_blue(pixel,new_blue)

return pic

def expand_height(pic, factor):

'''Return a newpicture that has been vertically stretched by the factor which has been set

'''

new_width = picget_width()

new_height = picget_height()factor

newpic = mediacreate_pic(new_width, new_height, mediablack)

for pixel in pic:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

newpixel = mediaget_pixel(newpic, x, yfactor)

for newpixel in newpic:

new_red = mediaget_red(pixel)

new_green = mediaget_green(pixel)

new_blue = mediaget_blue(pixel)

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def expand_width(pic,factor):

'''Return a newpicture that has been horizontally stretched by the factor which has been set

'''

new_width = picget_width() factor

new_height = picget_height()

newpic = mediacreate_pic(new_width,new_height,mediablack)

for newpixel in newpic:

x = mediaget_x(newpixel)

y = mediaget_y(newpixel)

pixel = mediaget_pixel(pic,x / factor, y)

new_red = mediaget_red(pixel)

new_green = mediaget_green(pixel)

new_blue = mediaget_blue(pixel)

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def reduce_height(pic, factor):

'''return a new pic that has been compressed vertically by the factor which has been set

'''

# Create a new, all-black pic with the appropriate new height and

# old width; (all colour components are zero)

new_width = picget_width

new_height = (picget_height() - 1) / factor + 1

newpic = mediacreate_pic(new_width, new_height, mediablack)

# Iterate through all the pixels in the original (large) image, and copy

# a portion of each pixel's colour components into the correct

# pixel position in the smaller image

for pixel in pic:

# Find the corresponding pixel in the new pic

x = mediaget_x(pixel)

y = mediaget_y(pixel)

newpixel = mediaget_pixel(newpic, x, y / factor)

# Add the appropriate fraction of this pixel's colour components

# to the components of the corresponding pixel in the new pic

new_red = newpixelget_red()+pixelget_red()/factor

new_green = newpixelget_green()+pixelget_green()/factor

new_blue = newpixelget_blue()+pixelget_blue()/fctor

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def reduce_width(pic,factor):

'''Return a newpic that has been horizontally compressed by the factor which has been set

'''

new_width = (mediaget_width() - 1) / factor + 1

new_height = mediaget_height()

newpic = mediacreate_pic(new_width, new_height, mediablack)

for pixel in pic:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

new_pixel = mediaget_pixel(newpic, x / factor, y)

new_red = newpixelget_red() + pixelget_red() / factor

new_green = newpixelget_green() + pixelget() / factor

new_blue = newpixelget_blue() + pixelget()/factor

mediaset_red(newpixel, new_red)

mediaset_green(newpixel, new_green)

mediaset_blue(newpixel, new_blue)

return newpic

def distance(pixel1, pixel2):

red1 = mediaget_red(pixel1)

green1 = mediaget_green(pixel1)

blue1 = mediaget_blue(pixel1)

red2 = mediaget_red(pixel2)

green2 = mediaget_green(pixel2)

blue2 = mediaget_blue(pixel2)

sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)

return sum

def simple_difference(pic1, pic2):

for pixel in pic1:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

pixel2 = mediaget_pixel(pic2, x, y)

sum = mediadistance(pixel, pixel2)

return sum

def smart_difference(pic1,pic2):

height1 = mediaget_height(pic1)

height2 = mediaget_height(pic2)

factorh = float(height1 / height2)

if factorh >= 1:

height1 = mediareduce_height(pic1, factorh)

else:

height2 = mediareduce_height(pic2, 1 / factorh)

width1 = mediaget_width(pic1)

width2 = mediaget_width(pic2)

factorw = float(width1 / width2)

if factorw >= 1:

width1 = reduce_width(pic1, factorw)

else:

width2 = reduce_width(pic2, 1 / factorw)

red1 = red_average(pic1)

green1 = green_average(pic1)

blue1 = blue_average(pic1)

red2 = mediascale_red(pic2, red1)

green2 = mediascale_green(pic2, green1)

blue2 = mediascale_blue(pic2, blue1)

#if __name__ == '__main__':

#mediashow(newpic)

以上就是关于使用Python 制作对比图片相似度的程序怎么比较全部的内容,包括:使用Python 制作对比图片相似度的程序怎么比较、怎么在NCBI网站上用BLAST程序查相似度较小一些的序列、(转)各种类型的数据的相异度(相似度)的度量等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9317247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存