Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture single picture with opencv

I have seen several things about capturing frames from a webcam stream using python and opencv, But how do you capture only one picture at a specified resolution with python and opencv?

like image 651
a sandwhich Avatar asked Nov 14 '10 19:11

a sandwhich


People also ask

Can OpenCV take screenshots?

This can be explained by the fact that unlike the windows function, OpenCV was not built for such a basic task. Another limitations is that this code only allows for one screenshot of all screens, which is not always the best option. Some users might want to only capture a specific screen.

Is OpenCV good for image processing?

OpenCV is a pre-built, open-source CPU-only library (package) that is widely used for computer vision, machine learning, and image processing applications. It supports a good variety of programming languages including Python.


2 Answers

You can capture a single frame by using the VideoCapture method of OpenCV.

import cv2

cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop) 
ret,frame = cap.read() # return a single frame in variable `frame`

while(True):
    cv2.imshow('img1',frame) #display the captured image
    if cv2.waitKey(1) & 0xFF == ord('y'): #save on pressing 'y' 
        cv2.imwrite('images/c1.png',frame)
        cv2.destroyAllWindows()
        break

cap.release()

Later you can modify the resolution easily using PIL.

like image 63
akshaynagpal Avatar answered Sep 28 '22 18:09

akshaynagpal


import cv2
cap = cv2.VideoCapture(0)
cap.set(3,640) #width=640
cap.set(4,480) #height=480

if cap.isOpened():
    _,frame = cap.read()
    cap.release() #releasing camera immediately after capturing picture
    if _ and frame is not None:
        cv2.imwrite('img.jpg', frame)
        cv2.imwrite(name, frame)
like image 32
Sreeragh A R Avatar answered Sep 28 '22 19:09

Sreeragh A R