Example 1 – Read and Display Image using OpenCVįollowing is an example python program to read and display an image.Ĭv2.waitKey(0) # waits until a key is pressedĬv2.destroyAllWindows() # destroys the window showing image Second argument is the image to be shown in the window. Syntax – cv2.imshow() cv2.imshow(window_name, image)įirst argument is name of the window that is displayed when image is shown in. Hence even if read a png image with transparency, the transparency channel is neglected. Note that the default flag is cv2.IMREAD_COLOR. For colored images, each pixel is represented as an array containing Red, Green and Blue channels. Returns numpy array, containing the pixel values. cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel.cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode.Any transparency of image will be neglected. cv2.IMREAD_COLOR : Loads a color image.
Second argument is an optional flag which could be any one of the following.
In this series of OpenCV Python Examples, you will start to write Python programs to perform basic operations in Image Processing like reading an image, resizing an image, extracting the different color channels of the image and also working around with these color channels. Now compile and run the program: python2 camera_stream.pyīy using this simple python script, we shall be able to access and use the cameras for OpenCV.The syntax of imread() function is cv2.imread(/complete/path/to/image,flag)įirst argument is complete path to the image along with the extension. We can do image processing, machine learning, etc using OpenCV.
Save the sample code as “camera_stream.py”. # When everything done, release the capture Release the camera, then close all of the imshow() windows.#Waits for a user input to quit the application Enter ‘q’ key, to break the loop and exit the application Grab the frame continuously from the camera and show it in the preview window using the while loop.Set the resolution of the camera by using OpenCV camera control propertiesĬap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)Ĭap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480).#Check whether user selected camera is opened successfully. Open the camera video node to access the See3CAM_130.
The following sample OpenCV python code explain how to open the device video node, set the resolution, grab the frame and then display the frame in preview window. Let us look how to prepare a sample Python application to stream the camera using OpenCV We now have built & installed OpenCV in the Ubuntu PC.
The following command will build and install OpenCV libraries in the location – “/usr/local/lib/” # GTK development libraries (to allow creating graphical windows)Īfter installing the dependencies, now we need to build and install OpenCV using the following commands:Ĭmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=OFF -D BUILD_TBB=OFF -D WITH_V4L=ON -D WITH_LIBV4L=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF. # OpenGL development libraries (to allow creating graphical windows)
# Video4Linux camera development libraries $ sudo apt-get install libavformat-dev libavutil-dev libswscale-dev # libav video input/output development libraries Install the dependencies from the following commands: OpenCV requires the following dependencies to work with the USB cameras. Let us start from building the OpenCV and its dependencies in Ubuntu PC.ĭownload the required OpenCV version for Linux from the following command We’ll see step by step procedure on how to access the See3CAM_130 camera from a simple OpenCV-Python application, which will grab the frame from camera and display in the preview window. 圆4 instead of x86) or compiler type, so substitute appropriate value.
These cameras are UVC-compliant that has Plug & Play support on Windows/Linux which does not require to install additional device drivers manually.įor this sample application, we are going to use e-con Systems™ 13MP Auto focus USB 3.0 camera – See3CAM_130. setx -m OPENCVDIR D:\OpenCV\Build\圆4\vc14 (suggested for Visual Studio 2015 - 64 bit Windows) Here the directory is where you have your OpenCV binaries ( extracted or built ). See3CAM is the USB 3.0 camera series from e-con Systems™. This blog is intended to show how to access and use the cameras for OpenCV by using a simple Python script. Open Computer Vision (OpenCV) is an open source BSD licensed image processing bundle that contains functions for all type of image processing functionality from basic image decoding, enhancement, color space conversion, object detection, object tracking and so on. In this blog we explained how a simple python script can be used to stream the color camera with OpenCV Python.įor more advanced features like support for standard UVC controls and Image capture options along with camera streaming, you may refer the following blog: