Real Time Image Processing Using Python & OpenCV
OpenCV is a library of cross platform programming functions aimed at real time Computer Vision. IT was designed for computational efficiency and with a strong focus on real-time applications, video and image processing. Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C.
The language provides constructs intended to enable clear programs on both a small and large scale. The growing demand of integrating OpenCV with python promises clear cut solutions to image processing problems. Since the tools are open source, researchers can exploit the freedom and possibilities of expansion. Wide spread applications in the field of robotics underlines the scope of OpenCV for image processing. Image classification, segmentation, feature extraction etc are made with suitable libraries and it can be invoked through many of the programming languages.
Here in Raspberry Pi Opencv is invoked through Python. So we need the updated version of both Python and Opencv.
Click here to get the detailed instructions to install Opencv and Python in Raspberry Pi 2.
For an example let us move on to a python program that runs a real time video from webcam using Opencv interface. After the installation of opencv a new folder will be created in the home. In this folder there are some python sample codes based on image processing.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#!/usr/bin/env python # import the necessary packages import numpy as np import cv2 import time import sys, getopt from video import create_capture from common import clock, draw_str args, video_src = getopt.getopt(sys.argv[1:], '', 'shotdir=') args = dict(args) shotdir = args.get('--shotdir', '.') try: video_src = video_src[0] except: video_src = 0 shot_idx = 0 cam = create_capture(video_src) cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320) cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240) while True: ret, img = cam.read() imgs = [] vis = img.copy() imgs.append(img) cv2.imshow('Video', vis) ch = 0xFF & cv2.waitKey(1) ## Check the Keyboard input if 0xFF & cv2.waitKey(5) == 27: break ## Key Press Actions if ch == ord('1'): for i, img in enumerate(imgs): fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx) cv2.imwrite(fn, img) print fn, 'saved' shot_idx += 1 if ch == ord('2'): cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640) cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480) if ch == ord('3'): cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320) cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240) if ch == ord('4'): cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 160) cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 120) cv2.destroyAllWindows() |
By running this code we can see the continuous capturing of frames from the webcam. The Opencv should be imported using the import cv2 command at the beginning of the program. at the end of the program there are four options interfaced with the keyboard commands. if ‘1’ is pressed the corresponding frame is saved and by pressing continuously alternate images will be saved. For ‘2’, ‘3’ and ‘4’ keys the resolution of the frame will be changed.
Opencv and RPi Camera
A sample code is given to access continuous frames from the RPi camera based on Python and Opencv.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import time import cv2 # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) # allow the camera to warmup time.sleep(0.1) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array # show the frame cv2.imshow("Frame", image) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) # if the `q` key was pressed, break from the loop if key == ord("q"): break |
Color, face edge detection processes are there in the Opencv library and it could be easily interfaced with the python to perform heavy applications.
Shop With Us
![]()
- Click here to buy Raspberry Pi 2 from RhydoLabz.
- Click here to buy Raspberry Pi 2 Starter Kit from RhydoLabz.
- Click here to buy Raspberry Accessories from RhydoLabz.
Resources![]()
Leave a Reply
You must be logged in to post a comment.