Installing OpenCV 3 on macOS with Homebrew
I am following this pyimagesearch tutorial to help me install OpenCV 3 on macOS Sierra 10.12.6 with Homebrew. I am using Python 2.7.15 & 3.6.4 installed with conda, instead of Python installed with Homebrew (like in the tutorial).
Contents
1. Major blog updates2. Installation steps: Homebrew install command
3. cv2 import error
4. Skipped brew tap
5. --HEAD flag error
6. Flags ignored: --with-contrib and --with-python3
7. OpenCV installation test code
8. SIFT and SURF test code
Major blog updates
- May 29, 2018: added webcam test code + references, explained more the "sym-link" part, and added update of ~/.bash_profile
Installation steps: Homebrew install command
These were the prerequisites I had on my computer before installing OpenCV 3 via Homebrew:
- Operating system: macOS Sierra 10.12.6
- Homebrew
- Two conda environments
Second virtual environment: Python 3.6.4 + numpy 1.15.0
These are the 4 steps I followed to install and test OpenCV 3 on macOS Sierra 10.12.6 with Homebrew (not all of the steps from the tutorial were followed):
1. Before running the brew install command, I updated the ~/.bash_profile with
2. This is the command I used to install OpenCV 3 with both Python 2.7 and Python 3 bindings via Homebrew:
3. As explained in the pyimagesearch tutorial, you have to "sym-link" your Python 2.7 + OpenCV 3 bindings so your conda's Python 2.7 installation will look for additional packages in Homebrew OpenCV compiled's site-packages:
Do the same for your Python 3.6 + OpenCV 3 bindings:NOTE 1: /usr/local/Cellar/opencv/3.4.1_5/lib/python{2.7,3.6}/site-packages/ is the Homebrew path to the OpenCV compile
NOTE 2: ~/miniconda3/envs/cv3_py3/lib/python{2.7,3.6}/site-packages/ is the path to the site-packages directory for conda's Python 2.7 & 3.6
4. Don't forget to test your OpenCV 3's Python 2.7 & 3.6 installations and make sure that you have access to SIFT and SURF algorithms and other keypoint detectors and local invariant descriptors.
Questions you might have about the brew install command and their corresponding answers:
cv2 import error
When you activate your conda environment and try to import cv2, if you get the error: "ImportError: numpy.core.multiarray failed to import", either your numpy is too old (I tried with numpy 1.13.3 and I got this error) or your numpy is not found in your environment. Thus, either install numpy via:
or install your numpy with:
Skipped brew tap
Thus I just skipped the brew tap command which was recommended in the pyimagesearch's tutorial.
--HEAD flag error
When using the pyimagesearch tutorial's brew install command, I got an error with the --HEAD flag
Flags ignored: --with-contrib and --with-python3
Homebrew seems to not recognize --with-contrib and --with-python3 flags
However, python 3 + OpenCV 3 bindings are installed on the system which I confirmed. Also, the
OpenCV's extra modules (features such as SIFT, SURF) are also installed which I tested.
OpenCV installation test code
We will test our OpenCV 3 installation by loading an image or streaming from a webcam, and displaying the image or webcam feed:
Test code @ gist
Reference: test code for webcam is from Adrian Rosebrock's tutorial
# Code for showing webcam feed is from Adrian Rosebrock's tutorial @ # https://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/ import cv2 print('OpenCV version: {}'.format(cv2.__version__)) # Read test image and show it img = cv2.imread('test.png') cv2.imshow('Window', img) cv2.waitKey(0) # Read from the video webcam camera = cv2.VideoCapture(0) # Loop over the frames of the video while True: # Grab the current frame _, frame = camera.read() # Show the frame and record if the user presses a key cv2.imshow("Webcam Feed", frame) key = cv2.waitKey(1) & 0xFF # If the `q` key is pressed, break from the loop if key == ord("q"): break # Cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows()
Reference: test code for webcam is from Adrian Rosebrock's tutorial
SIFT and SURF test code
We will test that we can access the SIFT, SURF, and other keypoint detectors and local invariant descriptors:
Test code @ gist
Reference: test code is from Adrian Rosebrock's blog post (Where did SIFT and SURF go in OpenCV 3?).
# Test code is from Adrian Rosebrock's blog post @ https://www.pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/ import cv2 image = cv2.imread("test.png") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # SIFT features sift = cv2.xfeatures2d.SIFT_create() (kps, descs) = sift.detectAndCompute(gray, None) print("# kps: {}, descriptors: {}".format(len(kps), descs.shape)) # SURF features surf = cv2.xfeatures2d.SURF_create() (kps, descs) = surf.detectAndCompute(gray, None) print("# kps: {}, descriptors: {}".format(len(kps), descs.shape)) # Other keypoint detectors and local invariant descriptors # KAZE kaze = cv2.KAZE_create() (kps, descs) = kaze.detectAndCompute(gray, None) print("# kps: {}, descriptors: {}".format(len(kps), descs.shape)) # AKAZE akaze = cv2.AKAZE_create() (kps, descs) = akaze.detectAndCompute(gray, None) print("# kps: {}, descriptors: {}".format(len(kps), descs.shape)) # BRISK brisk = cv2.BRISK_create() (kps, descs) = brisk.detectAndCompute(gray, None) print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))
Reference: test code is from Adrian Rosebrock's blog post (Where did SIFT and SURF go in OpenCV 3?).
Comments
Post a Comment