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:
- I'm aware I can indicate the figSize of the plt.figure, but how can I set a different subplot size for each subplot (fig.add_subplot)?
- The frame from opencv is with pixels, how can I make a subplot show the exact same image (in size), especially since matplotlib uses inches for dimensions?
Getting frame:
import cv2
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
Building figure and subplots:
fig = plt.figure()
img = fig.add_subplot(121)
img2 = fig.add_subplot(122)
Then putting frame into subplot
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)
Cheers!
------ EDIT ------
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
The code:
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()
Original 256x256 picture, read opened with opencv
Smaller image, if I leave out colspan and rowspan (=1) plt.subplot2grid((10, 10), (0, 0))
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?




