Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yuval
Yuval

Posted on • Originally published atthecodingnotebook.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) scriptimport syssys.path
Enter fullscreen modeExit 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 modeExit 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 modeExit 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 modeExit fullscreen mode

To verify, fromterminal 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 modeExit 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 pipinstallopencv-python
Enter fullscreen modeExit 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 pipinstall--upgrade pip setuptools wheel
Enter fullscreen modeExit fullscreen mode

Next we need to find where it was installed, fromterminal run the xcode python and import opencv:

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

And then:

importcv2cv2.__file__
Enter fullscreen modeExit 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 repoCvMatForLLDB (by houqi), I had to make slight adjustments for it to work, my version ishere.

Copy the abovecvmat.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 tosys.path inside our script.
Opencvmat.py in text editor, on the second line there is:

sys.path=sys.path+["/Library/Python/3.7/site-packages"]
Enter fullscreen modeExit 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 namedmyMat then to see it just type:

imshow myMat
Enter fullscreen modeExit 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 modeExit fullscreen mode

The End

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer
  • Joined

More fromYuval

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp