Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ipython notebook (jupyter),opencv (cv2) and plotting?

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.

like image 844
Fire Avatar asked Jan 06 '16 22:01

Fire


People also ask

Can we use cv2 in Jupyter Notebook?

You will now be able to import OpenCV to your jupyter notebook. Show activity on this post. One of possibility is that you could have written import cv2 and its utilisation in separate cells of jupyter notebook.

How do I use opencv in python Jupyter Notebook?

Type the command “pip install opencv-python” to install python lib You should see 'Successfully installed' to finish installing opencv-python. 7. Use Jupyter notebook to run python code Open the Windows Start menu in your Desktop, click “Anaconda3 (64-bit)”, and then click “Jupyter Notebook(anaconda3)”.


2 Answers

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

like image 118
themadmax Avatar answered Sep 23 '22 12:09

themadmax


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())) 
like image 43
Darren Gallagher Avatar answered Sep 23 '22 12:09

Darren Gallagher