Python - 基础教程学习(第五章 & 第六章)

 第五章 条件、循环和其他语句

print和import的更多信息

>>> print 'Age:',42
Age: 42
>>> print 'Age;';42
Age;
42
>>> name = 'Gumby'
>>> salutation = 'Mr'
>>> greeting = 'Hello,'
>>> print greeting,salutation,name
Hello, Mr Gumby

>>> import math as foobar
>>> foobar.sqrt(4)
2.0
>>> from math import sqrt as foobar
>>> foobar(4)
2.0
赋值魔法

>>> x,y,z = 1,2,3
>>> print x,y,z
1 2 3
>>> x,y = y,x
>>> print x,y,z
2 1 3
>>> values = 1,2,3
>>> values
(1, 2, 3)
>>> x,y,z = values
>>> x
1
>>> y
2
>>> 
>>> scoundrel = {'name':'Robin','girlfriend':'Marion'}
>>> key,value = scoundrel.popitem()
>>> key
'girlfriend'
>>> value
'Marion'
链式赋值

>>> x = y = 1
>>> x
1
>>> y
1
>>> 
增量赋值

>>> x = 2
>>> x += 1
>>> x
3
>>> x *= 4
>>> x
12
>>> fnord = 'foo'
>>> fnord += 'bar'
>>> fnord
'foobar'
>>> fnord *=2
>>> fnord
'foobarfoobar'
>>> 
语句块:缩排的乐趣

条件和条件语句

>>> True
True
>>> False
False
>>> True == 1
True
>>> False == 0
True
>>> True + False + 43
44
>>> 
>>> bool('I think, therefore I am')
True
>>> bool(42)
True
>>> bool('')
False
>>> bool("")
False
>>> bool(0)
False
>>> bool([])
False
>>> bool({})
False
>>> bool(False)
False
>>> bool(None)
False
>>> 
条件执行和if语句(if、elif、else)

name = raw_input('What is your name?')
if name.endswith('Gumby'): print 'Hello, Mr.Gumby'

num = input('Enter a number:')
if num > 0:
    print 'The number is positive'
elif num < 0:
    print 'The number is negative'
else:
    print 'The number is zero'
嵌套代码块

name = raw_input('What is your name?')
if name.endswith('Gumby'):
    if name.startswith('Mr'):
        print 'Hello, Mr.Gumby'
    elif name.startswith('Mrs'):
        print 'Hello, Mrs.Gumby'
    else:
        print 'Hello, Gumby'
else:
    print 'Hello, stranger'
更复杂的条件

比较运算符


>>> 'foo' == "foo"
True
>>> 'foo' == 'bar'
False
>>> x = y = [1,2,3]
>>> z = [1,2,3]
>>> x == y
True
>>> x is y
True
>>> x is z
False
>>> x = z
>>> x ==z
True
>>> 
>>> x = [1,2,3]
>>> y = [2,4]
>>> x is not y
True
>>> del x[2]
>>> x
[1, 2]
>>> y[1] = 1
>>> y.reverse()
>>> y
[1, 2]
>>> x == y
True
>>> x is y
False
name = raw_input('What is your name?')
if 's' in name:
    print 'Your name contains the letter "s".'
else:
    print 'Your name doesn\'t contain the letter "s".'
>>> "alpha" < "beta"
True

>>> 'FnOrD'.lower() == 'Fnord'.lower()
True
>>> [1,2] < [2,1]
True
>>> [2,[1,4]] < [2,[1,5]]
True
number = input('Enter a number between 1 and 10:')
if number <= 10 and number >= 1:
    print 'Great'
else:
    print 'Wrong'
断言

循环

while循环

x = 1
while x <= 1000:
    print x
    x += 1
name = ''
while not name:
    name = raw_input('Please enter your name?')
print 'Hello, %s!' % name
for循环

words = ['this','is','an','ex','parrot']
for word in words:
    print word
numbers = [0,1,2,3,4,5,6,7,8,9]
for number in numbers:
    print number
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 
d = {'x':1,'y':2,'z':3}
for key in d:
    print key, 'corresponds to', d[key]
    
y corresponds to 2
x corresponds to 1
z corresponds to 3
一些迭代工具

names = ['anne','beth','george','damon']
ages = [12,45,32,102]
for i in range(len(names)):
    print names[i],'is',ages[i],'year old'
翻转和排序迭代

>>> sorted([4,3,6,8,3])
[3, 3, 4, 6, 8]
>>> sorted('Hello, world!')
[' ', '!', ',', 'H', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
跳出循环

break

from math import sqrt
for n in range(99,0,-1):
    root = sqrt(n)
    if root == int(root):
        print n
        break
>>> range(0,10,2)
[0, 2, 4, 6, 8]
>>> 
continue

while True/break

word = 'dummy'
while word:
    word = raw_input('Please enter a word:')
    print 'The word was ' + word
word = raw_input('Please enter a word:')
while word:
    print 'The word was ' + word
    word = raw_input('Please enter a word:')
while True:
    word = raw_input('Please enter a word:')
    if not word:break
    print 'The word was ' + word
from math import sqrt
for n in range(92,99,1):
    root = sqrt(n)
    if root == int(root):
        print n
        break
    else:
        print "Didn't find it!",n
Didn't find it! 92
Didn't find it! 93
Didn't find it! 94
Didn't find it! 95
Didn't find it! 96
Didn't find it! 97
Didn't find it! 98
from math import sqrt
for n in range(99,89,-1):
    root = sqrt(n)
    if root == int(root):
        print n
        break
    else:
        print "Didn't find it!",n
Didn't find it! 99
Didn't find it! 98
Didn't find it! 97
Didn't find it! 96
Didn't find it! 95
Didn't find it! 94
Didn't find it! 93
Didn't find it! 92
Didn't find it! 91
Didn't find it! 90
range函数,就是不包括后面的边界~
列表推导式——轻量级循环

>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [x*x for x in range(10) if x % 3 == 0]
[0, 9, 36, 81]
>>> [(x,y) for x in range(3) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
result = []
for x in range(3):
    for y in range(3):
        result.append((x,y))
print result
>>> girls = ['alice','bernice','clarice']
>>> boys = ['chris','arnold','bob']
>>> [b+'+'+g for b in boys for g in girls if b[0] == g[0]]
['chris+clarice', 'arnold+alice', 'bob+bernice']
比较(Python根据缩进来判断语句关系)

result = []
for x in range(3):
    for y in range(3):
        result.append((x,y))
print result
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
result = []
for x in range(3):
    for y in range(3):
        result.append((x,y))
        print result
[(0, 0)]
[(0, 0), (0, 1)]
[(0, 0), (0, 1), (0, 2)]
[(0, 0), (0, 1), (0, 2), (1, 0)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

 第六章 抽象

fibs = [0,1]
num = input('How many Fibonacci numbers do you want?')
for i in range(num-2):
    fibs.append(fibs[-2] + fibs[-1])
print fibs
How many Fibonacci numbers do you want?10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
函数

def hello(name):
    return 'Hello,'+name+'!'
>>> print hello('world')
Hello,world!
>>> print hello('Gumby')
Hello,Gumby!
def fibs(num):
    result = [0,1]
    for i in range(num-2):
        result.append(result[-2]+result[-1])
    return result
>>> fibs(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> fibs(10)[2]
1
记录函数

def square(x):
    'Calculates the square of the number x.'
    return x*x
def square(x):
    'Calculates the square of the number x.'
    return x*x
并非真正函数的函数

def test():
    print 'This is printed'
    return
    print 'This is not'
>>> x = test()
This is printed
>>> x
>>> print x
None
>>> 
参数魔法

>>> def change(n):
	n = 'Alex'

	
>>> name = 'mr'
>>> change(name)
>>> name
'mr'
>>> def change(n):
	n[0] = 'Mr. Gumby'

	
>>> names = ['Mrs. Entity','Mrs. Thing']
>>> change(names)
>>> names
['Mr. Gumby', 'Mrs. Thing']
>>> 
>>> names = ['Mrs. Entity','Mrs. Thing']
>>> n = names
>>> n[0] = 'Mr. Gumby'
>>> names
['Mr. Gumby', 'Mrs. Thing']
>>> 
>>> names = ['Mrs. Entity','Mrs. Thing']
>>> n = names
>>> n is names
True
>>> n == names
True
>>> n = names[:]
>>> n is names
False
>>> n == names
True
指向同一对象的时候,is就是True,否则就是False



























  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 要进行相机标定的目的是为了去掉相机透镜畸变,使拍摄的图像更加准确,对于使用opencv库的python用户来讲,相机标定也是一项常规操作。以下是python-opencv相机标定的教程: 1. 收集标定图片:准备至少10到20张不同角度和位置的图片,要保证图片中有棋盘格子等模板。 2. 提取角点特征:用cv2.findChessboardCorners()函数提取棋盘格子的角点,这里用到的是cv2自带的提取工具。 3. 标定镜头:用cv2.calibrateCamera()函数对相机进行标定,得出相机内参矩阵等相关参数。 4. 存储标定结果: 使用cv2.FileStorage()函数存储标定参数。 5. 测试标定结果:使用cv2.undistort()函数果进行畸变校正,并观察校正后的图像是否有改善。 6. 应用标定结果:将标定结果应用到实际项目中,在程序中调用标定参数可以有效降低图像畸变,提高图像质量。 以上是python-opencv相机标定的教程,如果有需要的话,还可以使用均匀灰度图像等其他方式进行标定。通常情况下,一次标定的结果可以使用长时间,从而提高整个项目的精确度。 ### 回答2: Python-OpenCV相机标定教程是小型项目的标准。 在机器视觉和计算机视觉中,相机标定非常重要,这是获取全面、准确的数据的基础。相机标定的目的是为了减少照相机视角失真,提高拍摄到的图像质量,从而更好地支持照相机的图像处理。它的主要目的是矫正图像中的畸变并确定相机的内参和外参。 Python-OpenCV相机标定教程可以在Python编程语言中使用OpenCVPython库实现。这个过程包括多个步骤,如获取棋盘格角点、标定相机、计算相机的投影矩阵等。 在相机标定过程中,需要拍摄多张棋盘格图像。首先,必须定义棋盘格行列数量,然后手动测量棋盘格方格大小并加载图像到OpenCVPython中。接下来,寻找图像中棋盘格的角点,这些角点可以被处理以消除任何镜头失真。使用这些图像来标定相机并计算相机的投影矩阵。最后,保存相机内参和外参以对未来的图像应用重新计算。 相机标定的作用是消除由透视等导致的图像质量降低,从而使图像更清晰、更准确。Python-OpenCV相机标定教程为开发者提供了实现相机标定的基础,使他们可以快速构建照相机内参与外参算法并为数据处理提供基础。 ### 回答3: Python-OpenCV相机标定教程 OpenCV是一种非常流行的计算机视觉库,具有许多强大的功能,包括相机标定。相机标定是将相机的内部参数和畸变参数计算出来,以便更好地将2D图像转换为3D场景。在此教程中,我们将介绍使用Python-OpenCV库进行相机标定的步骤。 第一步:获取棋盘格图像 在进行相机标定之前,需要获取一些棋盘格图像。为了获得尽可能准确的结果,您需要将棋盘格图像从不同的角度和位置拍摄,并确保棋盘格图像足够清晰。我们建议至少拍摄10张不同的图像。 第二步:检测棋盘格角点 使用OpenCV中的函数cv2.findChessboardCorners()可以检测棋盘角点。它需要棋盘的大小和图像。如果检测到角点,函数将返回True,并将角点位置存储在一个数组中。 第三步:计算相机内部参数和畸变参数 为了计算相机的内部参数和畸变参数,需要使用OpenCV中的函数cv2.calibrateCamera()。这个函数接受一个由棋盘格图像和对应的角点位置组成的列表,并返回摄像机矩阵,畸变系数和旋转矩阵。 第四步:评估相机标定结果 在评估相机标定结果时,您需要计算误差,这可以通过一个简单的公式完成。误差是指每个棋盘格角点的图像坐标和标准(真实)坐标之间的平均距离。您还可以使用OpenCV可视化函数来显示标定结果。 总结 这就是使用Python-OpenCV进行相机标定的基本步骤。相机标定是一个基本任务,但是它对于实现更复杂的计算机视觉任务非常重要。标定成功后,您可以更准确地进行2D到3D坐标的变换,从而实现更准确的跟踪和测量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值