Python - 基础教程学习(第三章 & 第四章)

 第三章:使用字符串

基本字符串操作

>>> format = "Hello,%s,%s enough for ya?"
>>> values = ('world','Hot')
>>> print format % values
Hello,world,Hot enough for ya?
>>> format = "Pi with three decimals: %.3f"
>>> from math import pi
>>> print format % pi
Pi with three decimals: 3.142
简单转换

>>> 'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>> 'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs: 2a'
>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' % 42L
'Using str: 42'
>>> 'Using repr: %r' % 42L
'Using repr: 42L'

字段宽度和精度

>>> '%10f' % pi
'  3.141593'
>>> '%10.2f' % pi
'      3.14'
>>> '%.5s' % 'Guido van Rossum'
'Guido'
>>> '%.*s' % (5,'Guido van Rossum')    #可以通过表达式来自定义数字
'Guido'
符号、对齐和0填充

>>> '%010.2f' % pi
'0000003.14'
>>> 010
8
>>> '%-10.2f' % pi
'3.14      '
>>> print('%5d' % 10) + '\n' + ('%5d' % -10)
   10
  -10
>>> print('%+5d' % 10) + '\n' + ('%+5d' % -10)
  +10
  -10
3-1

# Print a formatted price list with a given width

width = input('Please enter width: ')

price_width = 10
item_width = width - price_width

header_format = '%-*s%*s'
format       = '%-*s%*.2f'

print '=' * width

print header_format % (item_width, 'Item', price_width, 'Price')

print '-' * width

print format % (item_width, 'Apples', price_width, 0.4)
print format % (item_width, 'Pears', price_width, 0.5)
print format % (item_width, 'Cantaloupes', price_width, 1.92)
print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8)
print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)

print '=' * width


字符串方法

find

>>> 'With a moo-moo here, and a moo-moo there'.find('moo')
7
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('Python')
6
>>> title.find('Flying')
15
>>> title.find('Xircus')
-1

>>> subject = '$$$ Get rich now!! $$$'
>>> subject.find('$$$')
0
>>> subject.find('$$$',1)
19
>>> subject.find('!!')
16
>>> subject.find('!!',0,16)
-1
join

>>> seq = ['1','2','3','4','5']
>>> sep.join(seq)
'1+2+3+4+5'

>>> dirs = '','usr','bin','env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> ''.join(seq)
'12345'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env
lower

>>> name = 'Gumby'
>>> names = ['gumby','smith','jones']
>>> if name.lower() in names: print 'Found it!'

Found it!
replace

>>> 'This is a test'.replace('is','eez')
'Theez eez a test'
split

>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '/user/bin/env'.split('/')
['', 'user', 'bin', 'env']
>>> 'Using the default'.split()
['Using', 'the', 'default']
strip(去除两侧的空格,或是符号)

>>> '        internal whitespace is kept        '.strip()
'internal whitespace is kept'
>>> names = ['gumby','smith','jones']
>>> name = 'gumby'
>>> if name.strip() in names: print 'Found it!'

Found it!
>>> '***SPAM * for * everyone!!! ***'.strip(' *!')
'SPAM * for * everyone'

 第四章:字典:当索引不好用时

创建和使用字典

>>> phonebook = {'Alice':'2341','Beth':'9102','Cecil':'3258'}
>>> phonebook['Alice']
'2341'
dict函数

>>> items = [('name','Gumby'),('age',42)]
>>> d = dict(items)
>>> d
{'age': 42, 'name': 'Gumby'}

>>> d = dict(name = 'Gumby' , age = 32)
>>> d
{'age' : 32, 'name' : 'Gumby'}

基本字典操作


>>> x = {}
>>> x[42] = 'Foobar'
>>> x
{42: 'Foobar'}

4-1 字典示例

# A simple database

# A dictionary with person names as keys. Each person is represented as
# another dictionary with the keys 'phone' and 'addr' referring to their phone
# number and address, respectively.

people = {

    'Alice': {
        'phone': '2341',
        'addr': 'Foo drive 23'
    },

    'Beth': {
        'phone': '9102',
        'addr': 'Bar street 42'
    },

    'Cecil': {
        'phone': '3158',
        'addr': 'Baz avenue 90'
    }

}

# Descriptive labels for the phone number and address. These will be used
# when printing the output.
labels = {
    'phone': 'phone number',
    'addr': 'address'
}

name = raw_input('Name: ')

# Are we looking for a phone number or an address?
request = raw_input('Phone number (p) or address (a)? ')

# Use the correct key:
if request == 'p': key = 'phone'
if request == 'a': key = 'addr'

# Only try to print information if the name is a valid key in
# our dictionary:
if name in people: print "%s's %s is %s." % \
    (name, labels[key], people[name][key])

>>> phonebook = {'Beth':'9102','Alice':'2341','Cecil':'3258'}
>>> phonebook
{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}
>>> phonebook['Alice']
'2341'
>>> "Cecil's phone number is %(Cecil)s." % phonebook
"Cecil's phone number is 3258."

字典方法

clear

>>> d = {}
>>> d['name'] = 'Gumby'
>>> d['age'] = 42
>>> d
{'age': 42, 'name': 'Gumby'}
>>> returned_value = d.clear()
>>> d
{}
>>> print returned_value
None

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key': 'value'}
>>> x = {}
>>> y
{'key': 'value'}
>>> x
{}

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key': 'value'}
>>> x.clear()
>>> y
{}

>>> x = {}
>>> x['key'] = 'value'
>>> x
{'key': 'value'}
>>> x.clear()
>>> x
{}
copy(浅复制)(引用)

>>> x = {'username':'admin','machines':['foo','bar','baz']}
>>> y = x.copy()
>>> y['username'] = 'mlh'
>>> y
{'username': 'mlh', 'machines': ['foo', 'bar', 'baz']}
>>> y['machines'].remove('bar')
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz']}
deepcopy深复制(副本)

>>> from copy import deepcopy
>>> d = {}
>>> d['names'] = ['Alfred','Bertrand']
>>> c = d.copy()
>>> dc = deepcopy(d)
>>> d['names'].append('Clive')
>>> c
{'names': ['Alfred', 'Bertrand', 'Clive']}
>>> dc
{'names': ['Alfred', 'Bertrand']}
fromkeys

>>> {}.fromkeys(['name','age'])
{'age': None, 'name': None}
>>> dict.fromkeys(['name','age'],'Unknow')
{'age': 'Unknow', 'name': 'Unknow'}
get

>>> d = {}
>>> print d['name']

Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    print d['name']
KeyError: 'name'
>>> print d.get('name')
None
>>> d.get('name','N/A')
'N/A'
>>> d['name'] = 'Eric'
>>> d.get('name')
'Eric'
has_key

>>> d = {}
>>> d.has_key('name')
>>> d = {'title':'Python Web Site','url':'http://www.python.org','spam':0}
>>> d.items()
[('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')]
>>> it = d.iteritems()
>>> it
<dictionary-itemiterator object at 0x01F526F0>
>>> list(it)
[('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')]
>>> 

False>>> d['name'] = 'Eric'>>> d.has_key('name')True>>>


items和iteritems
>>> d = {'title':'Python Web Site','url':'http://www.python.org','spam':0}
>>> d.items()
[('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')]
>>> it = d.iteritems()
>>> it
<dictionary-itemiterator object at 0x01F526F0>
>>> list(it)
[('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')]
>>> 

keys和iterkeys

pop

>>> d = {'x':1,'y':2}
>>> d.pop('x')
1
>>> d
{'y': 2}
popitem

>>> d = {'url':'http://www.python.org','spam':0,'title':'Python Web Site'}
>>> d.popitem()
('url', 'http://www.python.org')
>>> 
setdefault

>>> d = {}
>>> d.setdefault('name','N/A')
'N/A'
>>> d
{'name': 'N/A'}
>>> d['name'] = 'Gumby'
>>> d.setdefault('name','N/A')
'Gumby'
>>> d
{'name': 'Gumby'}
>>> 
update

>>> d = {'title':'Python Web Site','url':'http://www.python.org','changed':'Mar 14 22:09:15 MET 2008'}
>>> x = {'title':'Python Language Website'}
>>> d.update(x)
>>> d
{'url': 'http://www.python.org', 'changed': 'Mar 14 22:09:15 MET 2008', 'title': 'Python Language Website'}
>>> 
values和itervalues

>>> d = {}
>>> d[1] = 1
>>> d[2] = 2
>>> d[3] = 3
>>> d[4] = 1
>>> d.values()
[1, 2, 3, 1]
>>> d
{1: 1, 2: 2, 3: 3, 4: 1}
>>> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答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坐标的变换,从而实现更准确的跟踪和测量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值