Ipython notebook (jupyter)、opencv (cv2) 和绘图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34643747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:18:23  来源:igfitidea点击:

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

pythonopencvplotipython

提问by Fire

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

有没有办法在 ipython notebook 上使用 opencv2 和绘图?

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.

我对 python 图像分析相当陌生。我决定使用 notebook 工作流程来在我处理过程中做出很好的记录,并且使用 matplotlib/pylab 绘制事物的效果非常好。

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?

现在我已经开始使用 opencv。我现在又回到了同样的问题,我要么想内联绘图,要么根据手头的任务使用专用的交互式窗口。有没有类似的魔法可以使用?有没有另一种方法可以让事情发挥作用?还是我卡住了,需要从 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.

附带说明:我知道 opencv 已正确安装。首先,因为我没有在安装或导入 cv2 模块时出错。其次,因为我可以用 cv2 读取图像,然后用其他东西绘制它们。

采纳答案by themadmax

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

查看在线示例

回答by snoob dogg

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

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

每次需要在单元格中渲染图像时

回答by Darren Gallagher

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

对于在 Python 3.5 上运行的 Jupyter 笔记本,我必须将其修改为:

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()))