27

Is there a way to use and plot with opencv2 with ipython notebook?

I am fairly new to python image analysis. I decided to go with the notebook work flow to make nice record as I process and it has been working out quite well using matplotlib/pylab to plot things.

An initial hurdle I had was how to plot things within the notebook. Easy, just use magic:

%matplotlib inline

Later, I wanted to perform manipulations with interactive plots but plotting in a dedicated window would always freeze. Fine, I learnt again that you need to use magic. Instead of just importing the modules:

%pylab

Now I have moved onto working with opencv. I am now back to the same problem, where I either want to plot inline or use dedicated, interactive windows depending on the task at hand. Is there similar magic to use? Is there another way to get things working? Or am I stuck and need to just go back to running a program from IDLE?

As a side note: I know that opencv has installed correctly. Firstly, because I got no errors either installing or importing the cv2 module. Secondly, because I can read in images with cv2 and then plot them with something else.

3
  • 1
    I have found that the command "cv2.waitKey()" after the "cv2.imshow()" command gets around the freezing issue for external windows - but do not know why. I have also seen a few other commands mentioned here: txt.arboreus.com/2012/07/11/… Commented Jan 7, 2016 at 14:33
  • I am not sure if you can embed cv2 namedWindow in IPython notebook as it is a C++ frame. Probably there is no backend written for cv2.imshow. I would use imshow from pylab for embedding. Did this cv2.startWindowThread() work for notebook? I am curious. Commented Jan 14, 2016 at 23:41
  • It didn't work for me. A blank external window pops up and freezes. Commented Jan 18, 2016 at 14:52

3 Answers 3

42

This is my empty template:

import cv2
import matplotlib.pyplot as plt
import numpy as np
import sys
%matplotlib inline

im = cv2.imread('IMG_FILENAME',0)
h,w = im.shape[:2]
print(im.shape)
plt.imshow(im,cmap='gray')
plt.show()

See online sample

Sign up to request clarification or add additional context in comments.

4 Comments

What can h,w be used for?
h and w corresponding to Height and Width image size
Be careful if you are reading images using cv2 and displaying the image using plt. cv2 reads images as BGR by default, where as plt uses RGB format. So your Blue and Red color will get flipped. Here, you're reading the image in grayscale with 0 parameter in cv2.imread, so you won't see this issue.
You're right, we must use cv2.cvtColor(image, cv2.COLOR_BGR2RGB) to display color image
7

For a Jupyter notebook running on Python 3.5 I had to modify this to:

import io
import cv2
import numpy as np
from IPython.display import clear_output, Image, display
import PIL.Image

def showarray(a, fmt='jpeg'):
    a = np.uint8(np.clip(a, 0, 255))
    f = io.BytesIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))

1 Comment

Works but may need to use cv2.cvtColor depending on how your image was read.
2

There is also that little function that was used into the Google Deepdream Notebook:

import cv2
import numpy as np
from IPython.display import clear_output, Image, display
from cStringIO import StringIO
import PIL.Image

def showarray(a, fmt='jpeg'):
    a = np.uint8(np.clip(a, 0, 255))
    f = StringIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))

Then you can do :

img = cv2.imread("an_image.jpg") 

And simply :

showarray(img)

Each time you need to render the image in a cell

1 Comment

this is a great way to avoid problems in pyplots rendering. Just be mindful of Python 3 compatibility

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.