DEV Community

Yuval
Yuval

Posted on • Originally published at thecodingnotebook.com on

Debuggin OpenCV Mat in XCode

Did you know you can easily visualize OpenCV Mat when debugging in xcode?

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
Enter fullscreen mode Exit fullscreen mode

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',
'.']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And the python executable is under the "bin" folder

/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/bin/python3
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And then:

import cv2
cv2. __file__
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The End

It's quite a work indeed, but it well worth it if you work a lot with OpenCV.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more