Did you know you can easily visualize OpenCV Mat when debugging in xcode?
Where's Python ?
Apparently Xcode comes with python bundled, we need to find first where it is...
Run your project inside xcode and set a breakpoint somewhere, once it stops on the breakpoint type this in the (lldb) console:
(lldb) script
import sys
sys.path
The output is something like:
['/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A',
'/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python3',
'/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python37.zip',
'/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7',
'/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site-packages',
'.']
From the folders that are in the path try to see where python framework is, in my case it was:
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7
And the python executable is under the "bin" folder
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3
To verify, from terminal you should be able to run python, like:
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3 --version
Install OpenCV for Python
Now we need to install opencv for python, note we install it for the Python distribution that comes with Xcode, we do this using python & pip from above:
sudo /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3 -m pip install opencv-python
NOTE: If this fails it is possible you need to upgrade pip/setuptools/wheel
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3 -m pip install --upgrade pip setuptools wheel
Next we need to find where it was installed, from terminal run the xcode python and import opencv:
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3
And then:
import cv2
cv2. __file__
If it worked with no error, all good! (this can take few seconds on first time), take note of the module path, something like: /Library/Python/3.7/site-packages
OpenCV utils for Xcode
Now that we can use OpenCV inside Xcode's debugger we can read Mat convert it to UIImage and open it in Preview. There is a utils file in the repo CvMatForLLDB (by houqi), I had to make slight adjustments for it to work, my version is here.
Copy the above cvmat.py
to some place on your HD, preferably to your "home" folder.
Remember that in the previous section we took note on where OpenCV was installed? now we need to verify that location is loaded to sys.path
inside our script.
Open cvmat.py
in text editor, on the second line there is:
sys.path=sys.path + ["/Library/Python/3.7/site-packages"]
change the path above to match your path (where OpenCV is installed).
Visualizing when debugging
Now inside Xcode put some breakpoint where you have Mat instance, and from the (lldb) console import our utils file: command script import ~/cvmat.py
This will expose 3 functions:
- printMat - Print info about the mat (size etc)
- imwrite - Write the Mat as image to the tmp folder
- imshow - Write the Mat as image to the tmp folder and open it using the default app (i.e Preview)
For example your Mat variable is named myMat
then to see it just type:
imshow myMat
Auto-import the utils script
It can be quite annoying to import cvmat.py on every debug session, it is possible to import it automatically by doing so inside .lldbinit
, just add the following line to your ~/.lldbinit
file (if it does not exist just create it):
command script import ~/cvmat.py
The End
It's quite a work indeed, but it well worth it if you work a lot with OpenCV.
Top comments (0)