1.读取图片
imge = cv2.imread(IMAGE_PATH)
2.转灰度图
gray = cv2.cvtColor(imge, cv2.COLOR_BGR2GRAY)
3.图像缩放
image = cv2.resize(image, (400, int(400 * image.shape[0] / image.shape[1])))
4.高期模糊
gray = cv2.GaussianBlur(gray, (5, 5), 0)
5.局部自适应阀值二值化
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 0)
6.轮廓检测
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
7.轮廓边长
arc_length = cv2.arcLength(contour, True)
8.轮廓最小包围框
rect = cv2.minAreaRect(contour)
9.轮廓面积
area = cv2.contourArea(contour)
10.在图上画轮廓
cv2.drawContours(img, good_contours, -1, (0, 0, 255), 4)
11.角点检测
corners = cv2.goodFeaturesToTrack(gray, 10000, 0.05, 10, blockSize=5, useHarrisDetector=False, k=0.04)
12.角点亚像素处理
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 40, 0.001)
cv2.cornerSubPix(gray, corners, (5, 5), (-1, 1), criteria)
13.在图上画角点
for epoch in range(len(corners)):
cv2.circle(img, (corners[epoch][0][0], corners[epoch][0][1]), 3, (0, 255, 255), -1)
14.边缘检测
image = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.Canny(image, 100, 200)
15、dnn运行神经网络模型
model = cv2.dnn.readNetFromONNX(ONNX_MODEL_SAVE_PATH)
blob = cv2.dnn.blobFromImage(image,
scalefactor=1,
size=(28, 28),
mean=[0, 0, 0],
swapRB=False,
ddepth=cv2.CV_32F,
crop=False)
model.setInput(blob)
out = model.forward()
st_out = softmax(out, axis=1)
16、SIFT特征
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT().create(nfeatures=1000)
kp1, des1 = sift.detectAndCompute(gray1, None)
17、找到两个平面之间的转换矩阵
H,mask = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
18、透视变换,将图片投影到一个新的视平面
dst = cv2.perspectiveTransform(pts, H)