AI [IX] Note: compiling python OpenCV 4.2.0, SIFT feature extraction Under Windows, the correct perspective transformation of the object and the direction

Compile and install OpenCV

. 0-1 need to uninstall before installing opencv and opencv_contrib pip or conda installation:

pip uninstall opencv-contrib-python

pip uninstall opencv-python

0-2.python environment to install NumPY

pip install numpy

1. Download OpenCV source code: https://github.com/opencv/opencv ,

And change it to the corresponding version: git checkout 4.2.0

2. Download opencv_contrib Source: https://github.com/opencv/opencv_contrib/ ,

And change it to the corresponding version: git checkout 4.2.0

3. Download and install CMake: https://cmake.org/download/

4. Install Visual Studio, C ++ need to check the relevant environmental installation.

5. Open the CMake, opencv source select folders and files to build the project folder.

6. Click the Configure button, select the appropriate version of Visual Studio, to generate configuration information.

7. Locate OPENCV_EXTRA_MODULES_PATH option, select opencv_contrib / modules folder.

8. Locate PYTHON3_EXECUTABLE option, select python.exe file.

9. Locate PYTHON3_NUMPY_INCLUDE_DIRS option, select the "python path / lib / site-packages / numpy / core / include" folder.

10. Locate PYTHON3_LIBRARY option, select the "python path /libs/python37.lib" file

11. Locate PYTHON3_PACKAGES_PATH option, select the "python path / Lib / site-packages" folder.

12. Locate PYTHON3_INCLUDE_DIR option, select the "python path / include" folder.

13. Check OPENCV_ENABLE_NONFREE options.

14. Create a new BUILD_opencv_python3 option, and check.

15. Create a new PYTHON3_INCLUDE_PATH options, like PYTHON3_INCLUDE_DIR value.

16. Create a new PYTHON3_LIBRARIES options, like PYTHON3_LIBRARY value.

17. Uncheck BUILD_TESTS option.

18. The multiple clicks Configure, until all the red options become white, pay attention here to download the log exception if abnormalities may need to manually download and put .cache directory folder.

19. Click Generate, build the project, and open the project with Visual Studio.

20. Select Release, Rebuild ALL_BUILD project.

21. INSTALL generation project, i.e. the installation is complete. "Python path / lib / site-packages /" folder will generate cv2 folder, whether note the version number is incorrect.

 

SIFT feature extraction

import cv2
import numpy as np
import time
import math


def fileToOpenCVImage(file_path):
    '''OpenCV读取图片文件(中文路径)'''
    nparr = np.fromfile(file_path, dtype=np.uint8)
    opencv_img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return opencv_img


def openCVImageToFile(file_path, opencv_img):
    '''OpenCV读取图片文件(中文路径)'''
    cv2.imencode('.jpg', opencv_img)[1].tofile(file_path)

# 读取图片
img1 = fileToOpenCVImage('./data/sift_img/正面.jpg')
img2 = fileToOpenCVImage('./data/sift_img/侧面.jpg')
# 调整图片大小
img1 = cv2.resize(img1, (800, 450))
img2 = cv2.resize(img2, (800, 450))
img1 = cv2.GaussianBlur(img1,(0,0),3)
img2 = cv2.GaussianBlur(img2,(0,0),3)

# 创建SIFT对象,检测400个特征点
surf = cv2.xfeatures2d.SIFT_create(400)
# 提取两张图的特征信息
start = time.time()
kp1, des1 = surf.detectAndCompute(img1, None)
kp2, des2 = surf.detectAndCompute(img2, None)
end = time.time()
print(len(kp1), len(kp2))
print('特征耗时:%.5f s' % (end-start))

# 画出特征点
img1 = cv2.drawKeypoints(img1, kp1, img1)
img2 = cv2.drawKeypoints(img2, kp2, img2)

hmerge = np.hstack((img1, img2))  # 水平拼接
cv2.imshow("merge_img", hmerge)  # 拼接显示
cv2.waitKey(0)

# BFMatcher匹配特征
bf = cv2.BFMatcher()
start = time.time()
matches = bf.knnMatch(des1, des2, k=2)
end = time.time()
print('匹配耗时:%.5f s' % (end-start))

# 调整ratio,按照距离比例筛选掉密集的特征点
good = []
for m, n in matches:
    if m.distance/n.distance < 0.5:
        good.append([m])

good.sort(key=lambda m: -m[0].distance)

# 画出未筛选的关系
img_BFmatch = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, flags=2)
cv2.imshow("BFmatch_all", img_BFmatch)
cv2.waitKey(0)
# 画出筛选后的关系
img_BFmatch = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)
cv2.imshow("BFmatch_good", img_BFmatch)
cv2.waitKey(0)
cv2.destroyAllWindows()

Direction to correct perspective transformation of the object

# 正面坐标
org = np.float32([[kp1[good[0][0].queryIdx].pt[0], kp1[good[0][0].queryIdx].pt[1]],
                  [kp1[good[1][0].queryIdx].pt[0], kp1[good[1][0].queryIdx].pt[1]],
                  [kp1[good[2][0].queryIdx].pt[0], kp1[good[2][0].queryIdx].pt[1]],
                  [kp1[good[3][0].queryIdx].pt[0], kp1[good[3][0].queryIdx].pt[1]]])
# 侧面坐标
dst = np.float32([[kp2[good[0][0].trainIdx].pt[0], kp2[good[0][0].trainIdx].pt[1]],
                  [kp2[good[1][0].trainIdx].pt[0], kp2[good[1][0].trainIdx].pt[1]],
                  [kp2[good[2][0].trainIdx].pt[0], kp2[good[2][0].trainIdx].pt[1]],
                  [kp2[good[3][0].trainIdx].pt[0], kp2[good[3][0].trainIdx].pt[1]]])
print([kp1[good[0][0].queryIdx].pt[0], kp1[good[0][0].queryIdx].pt[1]],
      [kp2[good[0][0].trainIdx].pt[0], kp2[good[0][0].trainIdx].pt[1]],
      good[0][0].distance)
print(org)
print(dst)
# 透视变换
warpM = cv2.getPerspectiveTransform(dst, org)
result_img = cv2.warpPerspective(
    img2, warpM, (img2.shape[1], img2.shape[0]), borderValue=(255, 255, 255))
cv2.imshow("perspective_img", result_img)
cv2.waitKey(0)

# openCVImageToFile('./data/sift_img/c(450x800).jpg', result_img)
hmerge = np.hstack((img1, result_img))  # 水平拼接
cv2.imshow("merge_img", hmerge)  # 拼接显示
cv2.waitKey(0)

 

 SIFT feature points

 

Feature point matching

 

Screening match

 

Perspective transformation (left: original front view, right: after a perspective transform view)

 

Published 28 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/highlevels/article/details/104712303