Ask Your Question
0

Is there a way to resize an image and a window simultaneously?

asked 2017-12-05 14:29:12 -0600

MoyliePi gravatar image

updated 2017-12-06 14:31:47 -0600

I am a rookie at coding and even with that I am probably being generous. I was wondering if there is a way to resize an image, fed by a USB webcam, and resize a window simultaneously? With the Raspberry Pi3, I am trying to run four USB cameras and display the feed on a monitor. The monitor is a 21.5" so I want to have an even vertical and horizontal split in the screen so that I can view all four cameras at once. I created/cut and pasted a script written in Python and using opencv and numpy. Any ideas?

import cv2

import numpy as np

vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()

else:
    rval = False

while rval:
     cv2.resizeWindow("Left Mold - Cavity 1 & 2", 20, 20)

    cv2.moveWindow("Left Mold - Cavity 1 & 2", 1, 1)

    cv2.imshow("Left Mold - Cavity 1 & 2", frame)

    rval, frame = vc.read()

    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

cv2.destroyWindow("Left Mold - Cavity 1 & 2")

So I revised the code but I did something wrong along the way because I get some kickbacks. Here is the revised code...

import cv2 
import numpy as np

cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
cap4 = cv2.VideoCapture(3)

while(True):
    #Read Camera 1
    ret, frame = cap1.read()
    #Read Camera 2
    ret, frame = cap2.read()
    #Read Camera 3
    ret, frame = cap3.read()
    #Read Camera 4
    ret, frame = cap4.read()

    #Reduce the image size of all camera feeds to half
    cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
    cap2 = cv2.resize(cap2, (0, 0), None, .5, .5)
    cap3 = cv2.resize(cap3, (0, 0), None, .5, .5)
    cap4 = cv2.resize(cap4, (0, 0), None, .5, .5)

    #Stack camera 1 & 3 vertically and camera 2 & 4 horizontally to 1 & 3
    numpy_vertical = np.vstack((cap1, cap3))
    numpy_horizontal = np.hstack((cap2, cap4))
    array([[cap1,cap2],
           [cap3,cap4]])

    #Put the camera feed on the specified axis
    numpy_vertical_concat = np.concatenate((cap1, cap3), axis=0)
    numpy_horizontal_concat = np.concatenate((cap2, cap4), axis=1)

    #Display the video feed
    cv2.imshow('Left Mold - Cavity 1 & 2', cap1)
    cv2.imshow('Left Mold - Cavity 3 & 4', cap2)
    cv2.imshow('Right Mold - Cavity 1 & 2', cap3)
    cv2.imshow('Right Mold - Cavity 3 & 4', cap4)

    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

#When everything is done, release the capture

    cap1.release()
    cap2.release()
    cap3.release()
    cap4.release()
    cv2.destroyAllWindows()

And here are the errors...

VIDIOC_DQBUF: No such device
libv4l2: error turning on stream: Input/output error
VIDIOC_STREAMON: Input/output error
Traceback (most recent call last):
  File "MultipleVid.py", line 20, in <module>
    cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
TypeError: src is not a numpy array, neither a scalar
Unable to stop the stream.: No such device
Unable to stop the stream.: No such device
Unable to stop the stream.: No such ...
(more)
edit retag flag offensive close merge delete

Comments

sidenote: you want to put the iimshow() call after reading the frame, else you always show the previous frame

berak gravatar imageberak ( 2017-12-06 01:19:33 -0600 )edit
1

Thanks @berak! I appreciate the help.

MoyliePi gravatar imageMoyliePi ( 2017-12-06 07:05:44 -0600 )edit

wow, 4 cameras ?

please check cap.isOpened() for all your cams, and also the ret value from cap.read() (they are there for a reason !)

then, you have to apply cv2.resize() on an image, not the capture !

but i guess, you'll soon saturate the usb hub(s) on your box, using 4 cams. it might be a good idea to lower the resolution of the webcams (using cap.set(cv2.CAP_PROP_FRAME_WIDTH,320); cap.set(cv2.CAP_PROP_FRAME_WIDTH,240)), instead of resizing the image

berak gravatar imageberak ( 2017-12-06 10:09:46 -0600 )edit

Yeah I'm not even sure if the RaspberryPi3 can handle the 4 cams but my boss said give it a whirl! Thanks for the info though! I will make some changes. What do you mean by ret value? is this a return value?

MoyliePi gravatar imageMoyliePi ( 2017-12-06 10:50:35 -0600 )edit

yea, good luck with that !

and yes, ret is a return value, it says, if an image was read from the cam (the line might break, the video file might come to an end, so please check it !

berak gravatar imageberak ( 2017-12-06 10:58:10 -0600 )edit

what does "mat is not a numpy array, neither a scalar" mean? - google search isn't pulling anything up for the term "mat"

MoyliePi gravatar imageMoyliePi ( 2017-12-06 13:49:20 -0600 )edit

look at it, again it says: src is not a numpy array (not mat)

again you put the capture object, where an image was required. same thing with your later imshow() calls, btw.

it works like this: you have a VideoCapture instance, then you request to read an image from that, then you process/show the image

berak gravatar imageberak ( 2017-12-06 14:08:05 -0600 )edit

No sir. See below.

pi@MoyliePi:~ $ python CamREV2.py Traceback (most recent call last): File "CamREV2.py", line 12, in <module> cv2.imshow('Left Mold - Cavity 1 & 2', cap1) TypeError: mat is not a numpy array, neither a scalar

I did not post my third revision of the script. You must be looking at the same script you looked at before. I will edit my post and show you the new script with all of your suggestions incorporated (or so I think to the best of my knowledge with coding).

MoyliePi gravatar imageMoyliePi ( 2017-12-06 14:17:17 -0600 )edit

hmm, it gets pretty messy here now. what about starting a new question with you current problems ?

berak gravatar imageberak ( 2017-12-06 14:26:48 -0600 )edit

Haha, okay I will do that.

MoyliePi gravatar imageMoyliePi ( 2017-12-06 14:30:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-12-05 14:55:05 -0600

updated 2017-12-05 14:57:14 -0600

For your application, you do not really need to simultaneously resize both the image and window.

Once you have read the images, you can resize them using cv2.resize() explained here.

Then you have to combine these images together to form one large image then the window dimensions etc will be taken care of. If you feel like the final image is too big, you can either resize the individual frames or the final huge image.

Another way is to read all individual frames together without scaling, combine them then perform the scaling on the final huge frame. (personal favourite)

How do I display multiple images in one window? My previous answer will be helpful.

edit flag offensive delete link more

Comments

That's great! Thank-you for the quick response. I like the method of reading the individual frames and then scale the larger frame. Just another quick question if you don't mind...when I define "image" what do I write? I am using a camera feed opposed to a picture.

image = cv2.imread('pinocchio.png')

i appologize if these are simple questions but you seem to know what you're doing and I enjoy learning.

MoyliePi gravatar imageMoyliePi ( 2017-12-05 15:06:59 -0600 )edit

Anytime, glad to help. This is where I would refer you to the GUI features tutorials for camera and image loading. Once you have that figured out, the general tutorials will guide you throughout.

eshirima gravatar imageeshirima ( 2017-12-05 15:13:38 -0600 )edit

Awesome, thanks! I'll go do some research and see how it works out...I may be back haha.

MoyliePi gravatar imageMoyliePi ( 2017-12-05 15:17:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-05 14:29:12 -0600

Seen: 11,460 times

Last updated: Dec 06 '17