首页 > 编程知识 正文

python图片相似度分析,python比较两张图片相似度

时间:2023-05-04 11:48:29 阅读:12064 作者:684

要使用python进行图像处理,必须读取图像的任意区域,并将其转换为一维数组。 这对于后续的卷积操作的使用很有用。

用以下两种方法处理。

转换函数

来自pil导入图像

import numpy as np

import matplotlib.pyplot as plt

定义矩阵(文件名) :

读取im=image.open(filename ) #图像

im.show(#显示图像

width,height=im.size

打印(width is : ) str (width ) )

打印(高is : ) str ()高() ) ) ) ) ) ) ) ) )。

im=im.convert('l ' ) # pic -- mat变换可以选择不同的模式。 以下是函数源代码的具体说明

数据=im.get数据(

data=NP.matrix (数据,dtype='float ' )/255.0

new_data=NP.reshape(data*255.0,) height,width ) )

new _ im=image.from array (new _ data ) )。

#显示从矩阵数据得到的图像

new_im.show (

返回new _ data

efmatrixtoimage(data ) :

data=data*255

new _ im=image.from array (data.as type (NP.uint8) )

return new_im

''''

转换(自,模式=none,矩阵=none,dither=None,palette=0,colors=256 ) )

| returnsaconvertedcopyofthisimage.for the ' p ' mode,this

| methodtranslatespixelsthroughthepalette.ifmo deis

| omitted,amodeischosensothatallinformationintheimage

| andthepalettecanberepresentedwithoutapalette。

|

| thecurrentversionsupportsallpossibleconversionsbetween

| 'L ',' RGB ' and ' cmyk.' the * * matrix * * argumentonlysupports ' l '

| and 'RGB '。

|

| whentranslatingacolorimagetoblackandwhite (模式' l ),

| thelibraryusestheitu-r601-2 luma transform 33603360

|

| l=r * 299/1000 g * 587/1000 b * 114/1000

|

| thedefaultmethodofconvertingagreyscale (' l ) (or 'RGB ) )。

| imageintoabilevel (模式'1) ) image uses Floyd-Steinberg

| dithertoapproximatetheoriginalimageluminositylevels.if

| dither is NONE,all non-zerovaluesaresetto 255 (white ).to

| use other thresholds,use the : py : meth : `~pil.image.image.point `

|方法

|

| : param mode : therequestedmode.see :3360 ref : `概念- modes `。

| : param matrix 3360 anoptionalconversionmatrix.if given,this

| should be4- or12-tuplecontainingfloatingpointvalues。

| : param dither 3360 dithering method,使用when converting from

| mode ' RGB ' to ' p ' or from ' RGB ' or ' l ' to '1'。

| availablemethodsarenoneorfloydsteinberg (默认)。

| : param palette 3360 palettetousewhenconvertingfrommode ' RGB '

| to ' p '.availablepalettesareweboradaptive。

| : param colors 3360 numberofcolorstousefortheadaptivepalette。

| Defaults to 256。

| : rtype :3360 py : class : `~pil.image.image `

| : returns : an : py : class : `~pil.image.image `对象。

''''

原图:

filepath='./imgs/'

imgdata=imagetomatrix './imgs/0001.jpg ' )

打印(类型) imgdata ) )

打印(imgdata.shape )。

显示PLT.imshow(imgdata )图像

PLT.axis(off ) #坐标轴不显示

plt.show () )

执行结果:

mpimg函数

importmatplotlib.pyplotasplt # PLT用于显示图像

importmatplotlib.imageasmpimg # MP img用于读取图像

import numpy as np

efreadpic(picname,filename ) :

img=MPimg.imread(picname ) )。

#此时img已经是np.array,可以进行任意的处理

weight,height,n=img.shape # (512,512,3 ) ) ) ) ) ) ) )。

打印(the original pic : (n ) str (img ) ) ) ) )

显示PLT.imshow(img ) #图像

PLT.axis(off ) #坐标轴不显示

plt.show () )

获取reshape后矩阵的第一维数据,即所需数据的列表

img_reshape=img.reshape(1,weight*height*n ) [0]

打印(the1-dimage data : (n ) str (img _ reshape ) ) ) ) ) ) )。

(300、300 )区域的小块) 12*12*3),将该区域的图像数据转换为一维排列

img _ cov=NP.random.randint (1,2,) 12,12,3 ) (此处为np.ones ) )初始化数组。 数组元素是浮动类型,使用NP.random.randint验证它是否为id

forjinrange(12 ) :

forIinrange(12 ) :

img_cov[i][j]=img[300 i][300 j]

img _ reshape=img _ cov.reshape (1,12 * 12 *3) [0]

打印((img _ cov ) )

打印(img _ reshape )是

打印此12*12*3区域的图像

PLT.imshow(img_cov ) )。

PLT.axis (关闭) ) )。

plt.show () )

#写文件

#在append中打开# open:文件,如果找不到相应的文件,则创建具有该名称的文件

withopen(filename,' a ' ) as f:

f .写入(str (img _ reshape ) )

return img_reshape

if __name__=='__main__':

picname='./imgs/0001.jpg '

读pic (picname,' data.py ' )。

读出的数据(12*12*3)的各像素点按r、g、b的顺序排列,具有将该区域作为图像显示的效果。

参考: python读取和显示图像的两种方法

希望以上是正文的全部内容,对大家的学习有帮助。 另外,请大家支持。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。