opencv常用函数

# import cv2
# import numpy as np
# img = cv2.imread('e:\\car.png')
# # cv2.imshow('img',img)
# # cv2.waitKey(0)
# # print(img.shape)
# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# # print(gray.shape)
# # cv2.imshow('gray_img',gray)
# # cv2.waitKey(0)
# #二值化
# ret,binary = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)

# #轮廓查找
# # contours,hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# contours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#绘制轮廓
# cv2.drawContours(img,contours,-1,(255,0,0),-1)#第一个-1表示所有物体,第二个-1表示填充,颜色顺序为蓝,绿,红。

# print(contours)

#计算面积
# area = cv2.contourArea(contours[0])
# print("area=%d"%(area))

#计算周长
# len = cv2.arcLength(contours[0],True)#Ture or false 决定是否闭环
# print("len=%d"%(len))

#加载指定位置
# for_img = cv2.imread('e:\\ture.png',cv2.IMREAD_GRAYSCALE)
# mask = np.zeros_like(for_img)
# msk = cv2.fillPoly(mask,np.array([[[0,451],[0,306],[356,219],[381,219],[742,284],[742,451]]]),color=255)#坐标为像素点的位置
# masked_edge_img = cv2.bitwise_and(for_img,mask)

#多边形逼近
# e=500
# approx = cv2.approxPolyDP(contours[0],e,True)
# drawShape(img,approx)

#凸包
# hull = cv2.convexHull(contours[0])
# drawShape(img,hull)

# def drawShape(src,points):
#     i = 0
#     while i < len(points):
#         if(i == len(points) - 1):
#             x,y = points[i][0]
#             x1,y1 = points[0][0]
#             cv2.line(src,(x,y),(x1,y1),(0,0,255),1)
#         else:
#             x, y = points[i][0]
#             x1, y1 = points[i+1][0]
#             cv2.line(src, (x, y), (x1, y1), (0, 0, 255), 1)
#         i=i+1


#边缘检测
# dst = cv2.Canny(frame,300,350)#边缘检测


#harris角点检测
# blockSize = 2
# ksize = 3
# k= 0.04#经验值0.02-0.04之间
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#灰度化
# dst = cv2.cornerHarris(gray, blockSize, ksize, k)#Harris角点检测
# frame[dst > 0.01 * dst.max()] = [0, 0, 255]


#shi-Tomasi角点检测
# maxCorners = 1000#角点的最大数,值为0表示无限制
# ql = 0.01#小于1.0的正数,表示角点的质量
# minDistance = 10#两个角点之间的最小距离
# #mask:感兴趣的区域
#  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#灰度化
#         corners = cv2.goodFeaturesToTrack(gray, maxCorners, ql, minDistance)
#         corners = np.int0(corners)#浮点型转化为整型
#       # dst = cv2.Canny(frame,300,350)#边缘检测
#         for i in corners:
#             x, y = i.ravel()#多维数组转化为一维数组
#             cv2.circle(frame, (x, y), 3, (255, 0, 0), -1)#在原始图片上绘制角点
#         cv2.imshow('video', frame)

#sift特征检测
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#灰度化
#         sift = cv2.xfeatures2d.SIFT_create()#创建对象
#         kp,des = sift.detectAndCompute(gray,None)#第二个参数为检测区域,None为全部
#         print(des)
#         cv2.drawKeypoints(gray,kp,frame)#绘制keypoints
#         cv2.imshow('video', frame)

#暴力匹配
# img1 = cv2.imread('e:\\1.png')#打开两个图片
# img2 = cv2.imread('e:\\111.png')#打开两个图片

# gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)#灰度化
# gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)  # 灰度化

# sift = cv2.xfeatures2d.SIFT_create()

# kp1,des1 = sift.detectAndCompute(gray1,None)#第二个参数为检测区域,None为全部
# kp2, des2 = sift.detectAndCompute(gray2, None)  # 第二个参数为检测区域,None为全部

# bf = cv2.BFMatcher(cv2.NORM_L1)#创建匹配器
# match = bf.match(des1,des2)#调用match方法获得两幅图匹配的点
# img3 = cv2.drawMatches(img1,kp1,img2,kp2,match,None)#绘制出来

# cv2.imshow('video', img3)
# cv2.waitKey(0)
# cv2.destroyWindow()


# def xielv(line):#斜率计算
#     x_1,y_1,x_2,y_2 = line[0]
#     return (y_2-y_1)/(x_2-x_1)

# def reject_abnormal_lines(lines,threshold):#剔除斜率偏离较大的直线
#     slopes = [xielv(lines) for line in lines]
#     while len(lines) > 0:
#         mean = np.mean(slopes)
#         diff = [abs(s - mean) for s in slopes ]
#         idx = np.argmax(diff)
#         if diff[idx] > threshold:
#             slopes.pop(idx)
#             lines.pop(idx)
#         else:
#             break
#     return lines

# def least_squares_fit(lines):#最小二乘法拟合直线
#     #取出所有坐标点
#     x_coords = np.ravel([[line[0][0], line[0][2]] for line in lines])
#     y_coords = np.ravel([[line[0][1], line[0][3]] for line in lines])
#     #进行直线拟合,得到多项式系数
#     poly = np.polyfit(x_coords, y_coords, deg = 1)
#     #根据多项式系数,计算两个直线上的点,用于唯一确定这条直线
#     point_min = (np.min(x_coords), np.polyval(poly, np.min(x_coords)))
#     point_max= (np.max(x_coords), np.polyval(poly, np.max(x_coords)))
#     return np.array([point_min, point_max], dtype=np.int)

# mask = np.zeros_like(frame)
# mask = cv2.fillPoly(mask,np.array([[[0,969],[0,500],[100,250],[1490,250],[1490,969]]]),color=(0,200,0))#画出感兴趣的区域
# masked = cv2.bitwise_and(frame,mask)#与原图进行布尔运算


# lines = cv2.HoughLinesP(frame, 1, np.pi / 180, 15, minLineLength=40, maxLineGap=20) #求出所有直线的斜率
# left_lines = [line for line in lines if xielv(line) > 0]  #按照斜率进行分类
# right_lines = [line for line in lines if xielv(line) < 0]  #按照斜率进行分类
#
#
# left_line = least_squares_fit(left_lines)
# right_line = least_squares_fit(right_lines)
# cv2.line(frame,tuple(left_line[0]),tuple(left_line[1]), color=(0,255,255),thickness=5)
# cv2.line(frame,tuple(right_line[0]),tuple(right_line[1]), color=(0,255,255),thickness=5)




# print(binary.shape)
# cv2.imshow('img',img)
# cv2.imshow('bin',binary)
# cv2.waitKey(0)


使用步骤

1.创建使用对象
orb = cv2.ORB_create()#创建orb对象
2.进行特征点的检测和描述子的计算
kp,des = orb.detectAndCompute(gray,None)#第二个参数为检测区域,None为全部
cv2.drawKeypoints(gray,kp,frame)#绘制keypoints

你可能感兴趣的:(opencv,计算机视觉,python)