Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Conda Python 3.5 and OpenCV 3 With Matplotlib and QT5 Backend

DZone's Guide to

Conda Python 3.5 and OpenCV 3 With Matplotlib and QT5 Backend

We'll be working with the Conda Python library and OpenCV 3 as well as Matplotlib and QT5. Let's get to work on creating our backend!

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

As the title suggests, we'll be working with the Conda Python library and OpenCV 3 as well as Matplotlib and QT5. Let's get to work:

Create the Conda Environment With Python 3.5

$ conda create -n python35 python=35
$ conda activate python35

Inside the Conda environment, we need to install pyqt5, pyside, pyobj-core, pyobjc-framework-cocoa packages:

Installing QT5 Required Packages Inside Conda:

$ conda install -c dsdale24 pyqt5
$ conda install -c conda-forge pyside
## Note: I couldn;t find these with conda on conda-forge so used pip
$ pip install pyobjc-core
$ pip install pyobjc-framework-cocoa

Verifying Python 3.5:

$ python

Python 3.5.4 |Anaconda, Inc.| (default, Feb 19 2018, 11:51:41)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Checking Backend Used by Matplotlib:

import matplotlib
matplotlib.get_backend()

If you see 'MacOSX,' this means that it is using a MacOSX backend and we need to change it to QT as demonstrated below:

Changing Matplotlib Backend to Use QT5:

matplotlib.use('qt5agg')
matplotlib.get_backend()

This will result with qt5agg being used as the backend of our app, along with CV2.

Sample Code to Show an Image Using OpenCV3

Let's try some sample OpenCV3 code to show an image:

import cv2
image = cv2.imread("/work/src/github/aiprojects/avkash_cv/matrix.png")
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(image)
plt.show()

This is how the image rendered with the QT5 backend:

matrix

That’s it, enjoy!!

@avkashchauhan

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:
python 3 ,opencv ,conda ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}