opencv和numpy函数使用说明
- cv2.connectedComponentsWithStats()
- cv2.dilation()
- cv2.applyColorMap(img, cv2.COLORMAP_JET)
- cv2.polylines(img,points)
- cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)
- np.roll()
- np.linalg.norm
- np.linalg.inv
- np.linalg.det
- np.matmul()
- torch.permute(dim0,dim1,dim2)
- np.reshape((-1))
- np.c_(a,b)
- np.r_(a,b)
- np.prod()
- np.where()
- np.clip()
cv2.connectedComponentsWithStats()
nLabels, labels, stats, centroids=cv2.connectedComponentsWithStats(img,connectivity=4)
nLabels:标签的数量
labels:标签矩阵
stats:bounding box的信息,每一行对应一个标签区域,每行有5列,分别是[x0,y0,w,h,area]
centroids:每个区域的中心坐标[x,y]
img:单通道图片(8位灰度图)
connectivity:4连通或8连通
代码示例
import numpy as np
import cv2
img=np.array([
[0,255,0,0],
[0,0,0,255],
[0,0,0,255],
[255,0,0,0]
],np.uint8)
nLabels, labels, stats, centroids=cv2.connectedComponentsWithStats(img,connectivity=4)
print(nLabels)
print(labels)
print(stats)
print(centroids)
输出结果:
4
[[0 1 0 0]
[0 0 0 2]
[0 0 0 2]
[3 0 0 0]]
[[ 0 0 4 4 12]
[ 1 0 1 1 1]
[ 3 1 1 2 2]
[ 0 3 1 1 1]]
[[1.41666667 1.5 ]
[1. 0. ]
[3. 1.5 ]
[0. 3. ]]
cv2.dilation()
cv2.dilate(src, kernel, iteration)
src:原图像
kernel:核尺寸,可用cv2.getStructuringElement函数生成或numpy
iteration:迭代次数
膨胀原理:存在一个kernel,在图像上进行从左到右,从上到下的平移,如果方框中存在白色,那么这个方框内所有的颜色都是白色
cv2.applyColorMap(img, cv2.COLORMAP_JET)
给灰度图添加伪彩色,其中COLORMAP_JET就被常用于生成热力图
cv2.polylines(img,points)
绘制多边形
代码示例
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
newImageInfo = (500,500,3)
#多线条的绘制
#先确定连接的点
#在矩阵变换
#最后绘制
dst=cv2.imread('1.jpg',0)
points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32)
points = points.reshape(-1,1,2)
cv2.polylines(dst,[points],True,(0,0,255))
cv2_imshow(dst)
cv2.waitKey(0)
cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)
src:原图
dsize:输出图像尺寸
np.roll()
numpy.roll(a, shift, axis=None)
函数解释:沿着给定轴滚动数组元素。超出最后位置的元素将会滚动到第一个位置
x = np.arange(10) # x例子
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.roll(x, 2) # axis为None,则会先进行扁平化,然后再向水平滚动2个位置
array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
x2 = np.reshape(x, (2,5)) # x2例子
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
np.roll(x2, 1) # axis为None,则会先进行扁平化,然后再向水平滚动1个位置
array([[9, 0, 1, 2, 3],
[4, 5, 6, 7, 8]])
np.roll(x2, 1, axis=0) # 5列同时向垂直方向滚动1个位置
array([[5, 6, 7, 8, 9],
[0, 1, 2, 3, 4]])
np.roll(x2, 1, axis=1) # 2行同时向水平方向滚动1个位置
array([[4, 0, 1, 2, 3],
[9, 5, 6, 7, 8]])
————————————————
原文链接:https://blog.youkuaiyun.com/lxq1997/article/details/83543709
np.linalg.norm
求范数
np.linalg.inv
求逆
np.linalg.det
求行列式
np.matmul()
np.matmul(x1,x2) 矩阵的乘机,不是对应元素相乘,并且x1,x2不能是标量
torch.permute(dim0,dim1,dim2)
用于交换维度
np.reshape((-1))
把矩阵维度变为一行
np.c_(a,b)
将矩阵按行进行拼接,输出结果保证行相等
np.r_(a,b)
将矩阵按列拼接,输出结果保证列相等
np.prod()
用来计算数组每个元素的乘积,对于二维数组,可以指定axis参数,比如axis=1,表示计算每一行的乘积。
np.where()
np.clip()
np.clip(a,
a_min,
a_max,
out=None)
np.clip是一个截取函数,用于截取数组中小于或者大于某值的部分,并使得被截取部分等于固定值