Day01:安装好了开发环境,计划准备直接上手学习Opencv+Python,之前是没任何Python语法基础的,听了网课,照着敲了一遍代码,第一遍的代码总是会漏洞百出,好在照片窗口直接显示了出来,计算机摄像头被调用,仅有的一点测试,内心是欣喜的,道路是漫长的。
import cv2 as cv
def get_image_info(image):
print(type(image))
print(image.shape)
print(image.size)
print(image.dtype)
def video_demo():
capture=cv.VideoCapture(0)
while(True):
ret,frame=capture.read()
cv.flip(frame,1)
cv.imshow(“video”,frame)
c=cv.waitKey(50)
if c==27:
break
print(“”)hello Python
src=cv.imread(“E:/OpenCVTests/IMG_4482.JPG”)
cv.namedWindow(“input image”,cv.WINDOW_AUTOSIZE)
cv.imshow(“input image”,src)
get_image_info(src)
video_demo()
cv.waitKey(0)
cv.destroyAllWindows()
运行结果展示:
Day02:
在前一天的接触上,还是决定先学习一下Python基础语法知识,直接跟的慕课网的北京理工大学的公开课,计划花15天时间学完。
今日入门,各种琐碎的事耽误,只完成了蟒蛇的绘制,并决定将代码记录在CSDN。
import turtle
def drawSnake(rad,angle,len,neckrad):
for i in range(len):
turtle.circle(rad,angle)#rad描述圆形半径轨迹的位置,angle描述圆形半径轨迹的弧度值
turtle.circle(-rad,angle)
turtle.circle(rad,angle/2)
turtle.fd(rad)#即turtle.forward()函数,表示小乌龟沿直线爬行,参数表示爬行的距离
turtle.circle(neckrad+1,180)
turtle.fd(rad*2/3)
def main():
turtle.setup(2400,800,0,0) #the size of window
pythonsize=30
turtle.pensize(pythonsize) #the size of the pen
turtle.pencolor(“pink”)
turtle.seth(-40) #the angle of the start of the drawing
drawSnake(40,80,5,pythonsize/2)
main()
结果展示:
Day03:
今天主要学习了Python的数据类型(数字类型、字符串类型、元组类型、列表类型)及其简单操作,函数笔记主要记录在了笔记本上。同时学习了math、random库的简单使用,以及使用蒙特卡洛随机方法求Pi的值的小程序。
代码如下:
import math
from random import *
import random
from time import clock
#根据数字1-12输出对应的月份缩写,同理可输入数字1-7输出对应‘星期*’
‘’‘months=‘JanFebMarAprMayJunJulAugSepOctNovDec’
data=input(‘Please input the number from 1 to 12:’)
pos=(int(data)-1)*3;
month=months[pos:pos+3]
print(‘It’s\t’+month+’.’)’’’
#math库的使用
‘’‘print(pow(2,3))
print(math.ceil(3.1432))
print(math.sqrt(9))
print(math.radians(360))
print(math.degrees(math.pi2))
print(math.sin(math.pi3/2))
print(math.exp(3))’’’
#random库的使用
num=random.random()
print(num)
print(random.seed(1.32))
print(uniform(2,5))#float
print(randint(2,7))#int
print(randrange(4,6,13))
val=[‘Python ‘,‘is’, ‘the’,’ best’,’ language’]
print(random.choice(val))
print(random.shuffle(val))
print(random.sample(val,2))
#蒙特卡洛法利用随机试验求pi的值
datas=2000
hits=0
clock()
for i in range(1,datas):
x,y=random.random(),random.random()
dist=math.sqrt(x2+y2)
if dist<=1.0:
hits=hits+1
pi=4*(dist/datas)
print(‘the value pf the pi is:%s’%pi)
print(‘it takes about %-5.5ss’%clock)
Day04:
今日主要学习了分支结构和循环结构
import math
#if else的练习
‘’'def main():
a,b,c=eval(input(“Please input three numbers:”))#三个数的输入
delta=bb-4ac
if a0:
x=-c/b
print(‘There is only one soulation:’,x)#输出格式
elif delta0:
x=-b/(2a)
print(‘There is only one soulation:’,x)
elif delta<0:
print(‘There is no real soulation’)
elif delta>0:
x1=-(-b+(math.sqrt(delta)))/(2*a)
x2 = -(-b - (math.sqrt(delta))) / (2 * a)
print(‘There are two real soulations:’,x1,x2)
main()’’’
#差错处理-------else、finally语句报错,无法正常运行
‘’‘def main():
try:
number1,number2=input(‘Pleae input two numbers:’)
result=number1/number2
except ZeroDivisionError:
print(‘Division by zero!’)
except SyntaxError:
print(‘A comma may be missing in the input!’)
‘’‘except:
print(‘Something may be wrong in the input!’)系统报错:异常过于广泛,应该具体化’’’
else:
print(‘No exceptions,the result is:’,result)
finally:
print(‘The procdure have executed .’)
main()’’’
Day05–Day06:
天天学习的力量
#the Power of DayDayUp
‘’'dayUp=pow(1.001,365)
dayDown=pow(0.999,365)
print(“up:{:.2f},down:{:.2f}”.format(dayUp,dayDown))
dayUp01=pow(1.005,365)
dayDown01=pow(0.995,365)
print(“up:{:.2f},down:{:.2f}”.format(dayUp01,dayDown01))
dayUp02=pow(1.01,365)
dayDown02=pow(0.99,365)
print(“up:{:.2f},down:{:.2f}”.format(dayUp02,dayDown02))
#the power of working
day=1.0
dayFactor=0.01
i=1
for i in range(365):
if(i%60 or i%70):
day=day*(1-dayFactor)
else:
day = day * (1 + dayFactor)
print(“The result is:{:.2f}”.format(day))’’’
def Day(df):
dayUp=1
for i in range(365):
if i%7 in[6,0]:
dayUp = dayUp * (1 - 0.01)#休息日是退去1%
else:
dayUp= dayUp * (1 + df)
return dayUp
dayFactor=0.01
while Day(dayFactor)<37.78:
dayFactor=dayFactor+0.001
print(“The dayFactor is {:.3f}”.format(dayFactor))
#十二星座图标的打印
for i in range(12):
print(chr(9800+i),end=" ")#Python中,字符的表示为chr
Time库的应用
import time
print(time.time())#获取自1970起到刺此刻所历史的时间,以秒为单位
print(time.ctime())#获取系统时间
print(time.gmtime())#获取其它程序可利用的时间格式
t=time.gmtime()
print(time.strftime("%Y-%m-%d %H:%M:%S",t))