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

Conda Python 3.5 and OpenCV 3 With Matplotlib and QT5 Backend [Code Snippet]

DZone's Guide to

Conda Python 3.5 and OpenCV 3 With Matplotlib and QT5 Backend [Code Snippet]

Learn how to render an image using Conda Python 3.5 and OpenCV3 with a QT5 backend.

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

The title is pretty self-explanatory, so let's get into it.

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, and 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 shown below:

Change matplotlib backend to use QT5:

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

This will result in a qt5agg backend to be used with CV2.

Let's look at the sample code to show the image using OpenCV3.

Trying a sample OpenCV3 code to show 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 looks when rendered with a QT5 backend:

That's it — enjoy!

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

Topics:
python ,big data ,conda ,qt ,matplotlib ,opencv

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}