文档扫描OCR识别_积累

opecncv和python的积累

cv2.resize()

功能:对图像尺寸调整为指定大小

dst = cv2.resize(src, dsize, interpolation=None)

各参数释义:

scr: 输入图像

dst: 输出图像

dsize: 输出图像的尺寸,为元组格式(x, y),(300, 500)表示输出图像大小是300*500

interpolation:插值方法,缩小图像选择cv2.INTER_AREA

【例】resized = cv2.resize(image, (x, y), interpolation=cv2.INTER_AREA)

image:输入图像 ,resize成x*y大小,插值方法cv2.INTER_AREA,输出resized

cv2.imwrite()

功能:将图像保存到指定的路径

ret = cv2.imwrite(filename, img)

img : 输入图像

filename: 保存图像的路径和名称,如"cat.jpg"

ret : 返回为True则保存成功,否则返回False

【例】cv2.imwrite(filename, gray)

filename中存的是“3678.png”,将图像gray保存为3678.png

cv2.arcLength()

功能: 返回轮廓长度

ret = cv2.arcLength(contour, closed)

contour: 输入轮廓

closed: 布尔值,True表示轮廓封闭 ret: 返回轮廓长度

【例】peri = cv2.arcLength(c, True)

计算c中封闭轮廓的长度,输出为peri

cv2.approxPolyDP()

功能:函数用于返回轮廓的近似多边形 ret = cv2.approxPolyDP(contour, epsilon, closed)

contour: 输入轮廓

epsilon: 精度, 越小越精确,一般为周长2%

closed: 布尔值, True表示封闭轮廓

ret: 返回的近似多边形,存储坐标点,是三维数据(x,y,z)

【例】approx = cv2.approxPolyDP(c, 0.02 * peri, True)

以2%的周长为精度,计算输入c中封闭轮廓的近似多边形,输出为approx

cv2.medianBlur()

功能:中值滤波

dst=cv2.medianBlur(src,ksize)

src: 输入图像

ksize: 滤波的核尺寸,ksize=3,说明是3*3的核

dst:输出中值滤波后的图像

【例】gray = cv2.medianBlur(gray, 3)

输入gray图像,以3*3的核进行中值滤波,输出为gray

cv2.contourArea()

功能:计算轮廓面积

ret = cv2.contourArea(contour)

contour: 输入轮廓 ret: 返回的面积 【例子】ret_1 = cv2.contourArea(contours[0])

计算contours[0]轮廓面积,输出为ret_1

PIL(Python Image Library)

功能:第三方图像处理库,处理图像

Image类是PIL中的核心类,用于打开、保存、转换、展示图片

img = Image.open(filename)

打开"filename"文件,存到img

cv2.getPerspectiveTransform()

功能:图像投影变化,求出透视变换矩阵

M = cv2.getPerspectiveTransform(src, dst)

src: 输入图像的四个顶点坐标

dst: 变换后图像的四个顶点坐标

M:输出计算出的变换矩阵

【例】M = cv2.getPerspectiveTransform(rect, dst)

输入变化前的坐标rect,变换后的坐标dst,求出变换矩阵M

cv2.warpPerspective()

功能:根据变换矩阵,输出变换后的图像

ret = cv2.warpPerspective(src, M, (width, height))

src: 输入图像

M: 变换矩阵

(width, height): 输出图像大小,width*height

ret: 输出变换后的图像

【例】warped = cv2.warpPerspective(image, M, (300, 500))

使用矩阵M求出image变换后的图像,按照300*500,输出图像warped

np.sqrt()

功能:计算开平方

ret = np.sqrt( num )

num:需要开平方的数,要大于等于0

【例】widthA = np.sqrt(300)

计算300的开平方,赋值给widthA

np.diff()

功能: 计算数组中a[n]-a[n-1],输出一个新的数组

np.argmin()

功能:将array或者list铺平,返回最小值的坐标

ret = argmin(a)

a: 输入的array或者list

ret:最小值的坐标

【例】list_1 = [1, 2, 3, 4, 5]

print( np.argmin( listi_1 ))

定义一个list_1数据是“1,2,3,4,5”,输出list_1中最小值“1”的坐标“0”

shape

image.shape输出h,w的值,image.shape[0]是h, image.shape[1]是w

reshape

功能:将数组结构进行调整,不改变数组内容

a = np.array([1,2,3,4,5,6,7,8])

a.reshape(2,4)) # 将a变换为2行4列的数组

输出:array([[1, 2, 3, 4], [5, 6, 7, 8]])

os

功能:处理文件和目录,与操作系统进行交互

os.getpid() 返回当前进程ID,格式是int类型

os.remove()删除指定路径文件

os.remove(filename) 删除filename文件

sum(axis = 1)

功能:将每一行的数值相加

输入两行三列的矩阵[[0,1,2],[3,4,5]],设置"axis=1",按行相加

得出array([3,12])

【例】s = pts.sum(axis = 1)

将pts 矩阵按行相加,计算出的结果赋给s

sorted

功能:对输入对象排序,不改变输入对象顺序,输出为list格式

sorted(iterable,key=None, reverse=False)

iterable:输入,可迭代对象

key:定义排序逻辑

reverse:True为降序,False为升序

【例】cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

输入轮廓是cnts,cv2.contourArea计算面积,True为降序

按照轮廓面积,从大到小排序,输出为cnts

本人水平有限,暂时积累这些,部分函数只列举了自己使用的部分,错误不足之处请多指正。

感谢:

https://blog.csdn.net/qq_45100200/article/details/120053317

https://blog.csdn.net/youcans/article/details/121169014

https://blog.csdn.net/weixin_42828571/article/details/104121735

https://blog.csdn.net/qq_49478668/article/details/123485382

https://blog.csdn.net/leemboy/article/details/83792729/

https://blog.csdn.net/youcans/article/details/121382552

https://blog.csdn.net/Dontla/article/details/103025813/

https://blog.csdn.net/weixin_48306625/article/details/107669035

https://www.codeleading.com/article/34785915169/

https://blog.csdn.net/happy_wealthy/article/details/111933669

https://blog.csdn.net/weixin_52459924/article/details/122755402

https://blog.csdn.net/qq_37591637/article/details/103385174

https://blog.csdn.net/laobai1015/article/details/85720875

https://blog.csdn.net/likeyou1314918273/article/details/89672517

https://blog.csdn.net/liujingwei8610/article/details/121299626

https://blog.csdn.net/weixin_43843069/article/details/122039788

https://blog.csdn.net/elephant_my/article/details/115622680

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值