#总体图像直方图import cv2
from matplotlib import pyplot as plt
img=cv2.imread('data/data131367/微信图片_20220308140220.jpg',1)
plt.hist(img.reshape([-1]),256,[0,256])#拉平图片
plt.show()#三个通道图像直方图import cv2
from matplotlib import pyplot as plt
img=cv2.imread('data/data131367/微信图片_20220308140220.jpg',1)#图片读到内存中
color=('b','g','r')#使用不同颜色for i,col inenumerate(color):
histr=cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color=col)
plt.xlim([0,256])
plt.show()#红色偏右,图片整体红色强
机器学习-实践四:文本词频分析
#读取本文withopen('test.txt','r',encoding='UTF-8')as novelfile:
novel=novelfile.read()#文本分词import jieba
novellist=list(jieba.lcut(novel))#找到剔除停用词
stopwords=[line.strip()for line inopen('stop.txt','r',encoding='UTF-8').readlines()]
noveldict={}for word in novellist:if word notin stopwords:iflen(word)==1:continue#长度为1的忽略else:
noveldict[word]=noveldict.get(word,0)+1;#根据词频排序
novellistsorted=list(noveldict.items())
novellistsorted.sort(key=lambda e:e[1],reverse=True)#打印前二十的词for topwordup in novellistsorted[:20]:print(topwordup)