matplotlib opencv图像子图 [英] matplotlib opencv image subplot

查看:267
本文介绍了matplotlib opencv图像子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个全局matplotlib图形中,我想要一些不同大小的子图.例如,一个子图应该是从opencv2(网络摄像头)捕获的帧的确切大小,另一个子图应该是从该帧获得的较小图像.

Within a global matplotlib figure, I want to have a few subplots of different sizes. For example, one subplot should be the exact size of the frame captured from opencv2 (webcam), and the other should be a smaller image obtained from that frame.

我遇到两个不同的问题,都与尺寸有关:

I'm having two different issues, both regarding to sizing:

  1. 我知道我可以指出plt.figure的figSize,但是如何为每个子图(fig.add_subplot)设置不同的子图大小?
  2. opencv中的帧带有像素,如何使子图显示完全相同的图像(尺寸),特别是因为matplotlib使用英寸作为尺寸?

获取框架:

import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

建筑图和子图

fig = plt.figure()
img = fig.add_subplot(121)
img2 = fig.add_subplot(122)

然后将框架放入子图

img.imshow(frame) #this should be the original size of captured frame
#take out a square of the frame, and plot with box dimensions
#img2.imshow(box)

干杯!

------编辑------

------ EDIT ------

尽管我将使用网络摄像头来拍摄图像,但问题的核心是以下几点: 1.用opencv打开图像 2.将图像绘制为与Opencv读取图像具有相同尺寸的子图

Although I'll be using a webcam for the images, the core of my problem is the following: 1. Open image with opencv 2. Plot the image into a subplot, having same dimensions as the opencv read image

代码:

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('flower.jpg')
cv2.imshow('img',img)

fig = plt.figure()
video_plot = plt.subplot2grid((10, 10), (0, 0)) #Here I need to specify the same size as original
video = video_plot.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

cv2.waitKey(0)
cv2.destroyAllWindows()

plt.show()

原始256x256图片,使用opencv打开读取

Original 256x256 picture, read opened with opencv

图像更小,如果我忽略了colspan和rowspan(= 1) plt.subplot2grid((10,10),(0,0))

Smaller image, if I leave out colspan and rowspan (=1) plt.subplot2grid((10, 10), (0, 0))

更大的图像,如果我将colspan和rowspan最大化: plt.subplot2grid((10,10),(0,0),colspan = 10,rowspan = 10)

Bigger image, if I max out the colspan and rowspan: plt.subplot2grid((10, 10), (0, 0), colspan=10, rowspan=10)

总而言之,如何绘制相同的图像尺寸?

So to sum up, how can I plot the same image size?

推荐答案

图形尺寸的确以英寸为单位.在matplotlib中也指定了每英寸的点数(dpi).

The figure size is indeed specified in inches. The dots per inch (dpi) are specified as well in matplotlib.

了解图形尺寸(以英寸为单位)和dpi后,可以通过将像素除以dpi和图形尺寸来将轴定位在图形坐标中(从0到1).例如.为了将轴放置在距左下角50像素远的位置,您可以将轴放置在

Knowing the figure size in inches and the dpi allows you to position an axes in figure coordinates (from 0 to 1), by dividing the pixels by the dpi and the figure size. E.g. in order to place an axes 50 pixels away from the lower left corner you'd place the axes at

fig.add_axes([50./dpi/figsize[0], 50./dpi/figsize[1], ...])

宽度和高度类似地由每个方向上的像素数除以dpi和以英寸为单位的图形大小来确定.

The width and height are in analogy determined by the number of pixels in each direction divided by dpi and figure size in inches.

完整示例:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)

ar = np.random.poisson(lam=10, size=(250,300))
cut = ar[50:100, 200:250]; print cut.shape
dpi=100. # dots per inch
figsize=(5, 3.5)
fig = plt.figure(figsize=figsize)

ax = fig.add_axes([50./dpi/figsize[0],50./dpi/figsize[1],
                    ar.shape[1]/dpi/figsize[0],ar.shape[0]/dpi/figsize[1]])
im = ax.imshow(ar)

ax2 = fig.add_axes([100./dpi/figsize[0]+ar.shape[1]/dpi/figsize[0],50./dpi/figsize[1],
                    cut.shape[1]/dpi/figsize[0],cut.shape[0]/dpi/figsize[1]])
im2 = ax2.imshow(cut)

ax.axis("off")

plt.show()

这篇关于matplotlib opencv图像子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆