python期末考试(程序题)词频 opencv批量处理+灰度

  1. 读取下列文件中的内容,将所有文件的文字写至一个*.txt文本文档中,统计词频绘制词云,答题结果请粘贴至答题卡(文件请见附件)。(20分)

见 文件夹1内容如下

It is seen from the results shown in Fig.5 and Fig.6 that all the specimens have better entirety and plastic deformation ability.,inputs, of which j inputs are the outputs of the previous j nodes in the same
skip pathway and the last input is the up-sampled output from the lower skip
pathway. The reason that all prior feature maps accumulate and arrive at the
current node is because we make use of a dense convolution block along each
skip pathway.
,Recent years, with the development of deep learning method, convolutional neural networks (CNNs) have been widely used in medical image analysis pipeline,In addition, counter-based methods extract the objects boundaries by using the characteristics of intensity discontinuities at the edges of organs.  Shi et al. proposed an automatically segmentation method on DCE-MRI images combining K-mean clustering and morphological operations before the 3D level-set model,It is seen from the results shown in Fig.5 and Fig.6 that all the specimens have better entirety and plastic deformation ability.,inputs, of which j inputs are the outputs of the previous j nodes in the same
skip pathway and the last input is the up-sampled output from the lower skip
pathway. The reason that all prior feature maps accumulate and arrive at the
current node is because we make use of a dense convolution block along each
skip pathway.
,Recent years, with the development of deep learning method, convolutional neural networks (CNNs) have been widely used in medical image analysis pipeline,In addition, counter-based methods extract the objects boundaries by using the characteristics of intensity discontinuities at the edges of organs.  Shi et al. proposed an automatically segmentation method on DCE-MRI images combining K-mean clustering and morphological operations before the 3D level-set model
import os

path = r"" #文件夹目录
files= os.listdir(path) #得到文件夹下的所有文件名称
txts = []
for file in files: #遍历文件夹
    position = path+'\\'+ file #构造绝对路径,"\\",其中一个'\'为转义符
    print (position)           
    with open(position, "r",encoding='utf-8') as f:    #打开文件
        data = f.read()   #读取文件
        txts.append(data)
txts = ','.join(txts)#转化为非数组类型  
print (txts)


f = open(r"",'a')  #若文件不存在,系统自动创建。'a'表示可连续写入到文件,保留原内容,在原
                      #内容之后写入。可修改该模式('w+','w','wb'等)
 # 2.1 write 写入
#\n 换行符
f.write(txts)

split = txts.split()

#使用空格替换标点符号
article = txts.replace(",","").replace(".","").replace(":","").replace(";","").replace("?","")

#大写字母转换成小写字母
exchange = article.lower();

#生成单词列表
list = exchange.split()

#生成词频统计
dic = {}
for i in list:
    count = list.count(i)
    dic[i] = count
print(dic)
print(dic)

#导入词云的包
from wordcloud import WordCloud
#导入matplotlib作图的包
import matplotlib.pyplot as plt
#生成一个词云对象
wordcloud = WordCloud(
        background_color="white", #设置背景为白色,默认为黑色
        width=1500,              #设置图片的宽度
        height=960,              #设置图片的高度
        margin=10               #设置图片的边缘
        ).generate(txts)
# 绘制图片
plt.imshow(wordcloud)
# 消除坐标轴
plt.axis("off")
# 展示图片
plt.show()
# 保存图片
wordcloud.to_file('my_test2.png')

 

2.对文件夹中所有文件进行重命名,格式: *.jpg,重命名后,将各个图像转换为灰度图像(需要安装python 版本的opencv实现,注意选择国内的安装源)(文件夹请见附件)(30分)

# -*- coding: utf-8 -*-
import os
import cv2

path_img = r'' #放原图片的路径

dirs = os.listdir(path_img)
os.chdir(path_img)

try:
    os.mkdir('rename') #创建一个文件夹放置这些图片
except:
    pass

count = 0
for img in dirs:
    img = cv2.imread(img, 1)
    Str = ""+"_"+"%06d"% count #更改重命名文件名称
    path = os.path.join('rename', Str + '.jpg')
    img_1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #转灰度图
    cv2.imwrite(path, img_1)
    count +=1

 

2.假设你所在的班级同学来自很多个省份,请绘制一个饼型图,用以表示你所在班级的同学生源地情况(绘制出5个省的同学即满分,数量自拟)(10分)

 

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']='SimHei'#设置中文显示
plt.figure(figsize=(6,6))#将画布设定为正方形,则绘制的饼图是正圆
label=['北京','天津','上海',"广州","厦门"]#定义饼图的标签,标签是列表
explode=[0.01,0.01,0.01,0.01,0.01]#设定各项距离圆心n个半径
#plt.pie(values[-1,3:6],explode=explode,labels=label,autopct='%1.1f%%')#绘制饼图
values=[11,1,1,1,6]
plt.pie(values,explode=explode,labels=label,autopct='%1.1f%%')#绘制饼图
plt.title('学生生源地')#绘制标题
plt.savefig('./饼图')#保存图片
plt.show()

 

  

4.Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

Input: [0,1,0,3,12]

Output: [1,3,12,0,0]

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
k = 0
nums = [0,1,0,3,12]
for num in nums:
    if (num):
        nums[k] = num
        k += 1

while k < len(nums):
    nums[k] = 0
    k += 1

print(nums)

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值