第七节 筛选和聚类
作者:互联网
一、哈希算法筛选图片,在网页展示
from flask import Flask,render_template
import cv2
import os
app=Flask(__name__)
def aHash(img):
# 缩放为8*8
img = cv2.resize(img, (8, 8))
# 转化为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# s为像素和初值为0,hash_str为hash值初值为''
s = 0
hash_str = ''
# 遍历累加求像素和
for i in range(8):
for j in range(8):
s = s + gray[i, j]
# 求平均灰度
avg = s / 64
# 灰度大于平均值为1相反为0生成图片的hash值
for i in range(8):
for j in range(8):
if gray[i, j] > avg:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
# Hash值对比
def cmpHash(hash1, hash2):
n = 0
print(hash1)
print(hash2)
# hash长度不同则返回-1代表传参出错
if len(hash1) != len(hash2):
return -1
# 遍历判断
for i in range(len(hash1)):
# 不相等则n计数+1,n最终为相似度
if hash1[i] != hash2[i]:
n = n + 1
sim=1-n/64
return sim
def genFrame():
v_path='static/video.mp4'
image_save='static/pic'
if not(os.path.exists(image_save)):
os.mkdir(image_save)
cap=cv2.VideoCapture(v_path)
fc=cap.get(cv2.CAP_PROP_FRAME_COUNT)
_,img1=cap.read()
cv2.imwrite('static/Hash2021/image{}.jpg'.format(0),img1)
for i in range(int(fc-1)):
_,img2=cap.read()
hash1=aHash(img1)
hash2=aHash(img2)
n=cmpHash(hash1,hash2)
if n<0.6:
cv2.imwrite('static/Hash2021/image{}.jpg'.format(i),img2)
img1=img2
genFrame()
htlml中写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<br>
均值哈希帧数:{{framecount}}<br>
{% for i in range(framecount)%}
<img height="40" src="static/Hash2021/{{filename[i]}}"/>
{{filename[i]}}
{% endfor%}
</body>
</html>
为加入一个路由器程序,在main加上
@app.route('/shot')
def shot():
path='static/Hash2021'
filename=os.listdir(path)
framecount=len(filename)
print(filename)
print(type(filename))
print(filename)
return render_template('hash.html',filename=filename,framecount=framecount)
得到
二、聚类
1、通过颜色聚类
import numpy as np
import os
from PIL import Image
import matplotlib.pyplot as plt
im=np.array(Image.open('IMG_7433.JPG'))
def colorz(filename,n=3):
img=Image.open(filename)
img=img.rotate(-90)
img.thumbnail((200,200))
w,h=img.size
print(w,h)
print('w*h=',w*h)
plt.axis('off')
plt.imshow(img)
plt.show()
points=[]
for count,color in img.getcolors(w*h):
points.append(color)
return points
colorz('IMG_7433.JPG',3)
2、通过缩略聚类
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
im=np.array(Image.open('hk7.jpg'))
#用缩略图聚类
def colorz(filename,n=3):
img=Image.open(filename)
img=img.rotate(-90)
img.thumbnail((200,200))
w,h=img.size
print(w,h)
print('w*h=',w*h)
plt.axis('off')
plt.imshow(img)
plt.show()
points=[]
for count,color in img.getcolors(w*h):
points.append(color)
return points
colorz('hk7.jpg',3)
标签:plt,hash,img,filename,第七节,聚类,print,import,筛选 来源: https://blog.csdn.net/princesshu/article/details/122415580